blob: 53dbe289822c74fc862147075865be139c766d2a [file] [log] [blame]
rileya@google.com981b33a2012-09-05 18:36:17 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkBenchmark.h"
10#include "SkCanvas.h"
11#include "SkRTree.h"
12#include "SkRandom.h"
13#include "SkString.h"
14
15// confine rectangles to a smallish area, so queries generally hit something, and overlap occurs:
16static const int GENERATE_EXTENTS = 1000;
17static const int NUM_BUILD_RECTS = 500;
18static const int NUM_QUERY_RECTS = 5000;
19static const int NUM_QUERIES = 1000;
20
21typedef SkIRect (*MakeRectProc)(SkRandom&, int, int);
22
23// Time how long it takes to build an R-Tree either bulk-loaded or not
24class BBoxBuildBench : public SkBenchmark {
25public:
26 BBoxBuildBench(void* param, const char* name, MakeRectProc proc, bool bulkLoad,
27 SkBBoxHierarchy* tree)
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000028 : INHERITED(param)
rileya@google.com981b33a2012-09-05 18:36:17 +000029 , fTree(tree)
30 , fProc(proc)
rileya@google.com61348d12012-09-06 13:38:53 +000031 , fBulkLoad(bulkLoad) {
32 fName.append("rtree_");
33 fName.append(name);
34 fName.append("_build");
35 if (fBulkLoad) {
36 fName.append("_bulk");
37 }
tomhudson@google.com9dc27132012-09-13 15:50:24 +000038 fIsRendering = false;
rileya@google.com61348d12012-09-06 13:38:53 +000039 }
robertphillips@google.com07a05d52012-09-11 11:54:07 +000040 virtual ~BBoxBuildBench() {
rileya@google.com48134582012-09-11 15:41:50 +000041 fTree->unref();
robertphillips@google.com07a05d52012-09-11 11:54:07 +000042 }
rileya@google.com981b33a2012-09-05 18:36:17 +000043protected:
44 virtual const char* onGetName() {
rileya@google.com61348d12012-09-06 13:38:53 +000045 return fName.c_str();
rileya@google.com981b33a2012-09-05 18:36:17 +000046 }
47 virtual void onDraw(SkCanvas* canvas) {
48 SkRandom rand;
49 for (int i = 0; i < SkBENCHLOOP(100); ++i) {
50 for (int j = 0; j < NUM_BUILD_RECTS; ++j) {
51 fTree->insert(reinterpret_cast<void*>(j), fProc(rand, j, NUM_BUILD_RECTS),
52 fBulkLoad);
53 }
54 fTree->flushDeferredInserts();
55 fTree->clear();
56 }
57 }
58private:
59 SkBBoxHierarchy* fTree;
60 MakeRectProc fProc;
rileya@google.com61348d12012-09-06 13:38:53 +000061 SkString fName;
rileya@google.com981b33a2012-09-05 18:36:17 +000062 bool fBulkLoad;
63 typedef SkBenchmark INHERITED;
64};
65
66// Time how long it takes to perform queries on an R-Tree, bulk-loaded or not
67class BBoxQueryBench : public SkBenchmark {
68public:
69 enum QueryType {
70 kSmall_QueryType, // small queries
71 kLarge_QueryType, // large queries
72 kRandom_QueryType,// randomly sized queries
73 kFull_QueryType // queries that cover everything
74 };
75
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000076 BBoxQueryBench(void* param, const char* name, MakeRectProc proc, bool bulkLoad,
rileya@google.com981b33a2012-09-05 18:36:17 +000077 QueryType q, SkBBoxHierarchy* tree)
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000078 : INHERITED(param)
rileya@google.com981b33a2012-09-05 18:36:17 +000079 , fTree(tree)
80 , fProc(proc)
rileya@google.com981b33a2012-09-05 18:36:17 +000081 , fBulkLoad(bulkLoad)
82 , fQuery(q) {
rileya@google.com61348d12012-09-06 13:38:53 +000083 fName.append("rtree_");
84 fName.append(name);
85 fName.append("_query");
86 if (fBulkLoad) {
87 fName.append("_bulk");
88 }
rileya@google.com981b33a2012-09-05 18:36:17 +000089 SkRandom rand;
90 for (int j = 0; j < SkBENCHLOOP(NUM_QUERY_RECTS); ++j) {
91 fTree->insert(reinterpret_cast<void*>(j), fProc(rand, j,
92 SkBENCHLOOP(NUM_QUERY_RECTS)), fBulkLoad);
93 }
94 fTree->flushDeferredInserts();
tomhudson@google.com9dc27132012-09-13 15:50:24 +000095 fIsRendering = false;
rileya@google.com981b33a2012-09-05 18:36:17 +000096 }
robertphillips@google.com07a05d52012-09-11 11:54:07 +000097 virtual ~BBoxQueryBench() {
rileya@google.com48134582012-09-11 15:41:50 +000098 fTree->unref();
robertphillips@google.com07a05d52012-09-11 11:54:07 +000099 }
rileya@google.com981b33a2012-09-05 18:36:17 +0000100protected:
101 virtual const char* onGetName() {
rileya@google.com61348d12012-09-06 13:38:53 +0000102 return fName.c_str();
rileya@google.com981b33a2012-09-05 18:36:17 +0000103 }
104 virtual void onDraw(SkCanvas* canvas) {
105 SkRandom rand;
106 for (int i = 0; i < SkBENCHLOOP(NUM_QUERIES); ++i) {
107 SkTDArray<void*> hits;
108 SkIRect query;
109 switch(fQuery) {
110 case kSmall_QueryType:
111 query.fLeft = rand.nextU() % GENERATE_EXTENTS;
112 query.fTop = rand.nextU() % GENERATE_EXTENTS;
113 query.fRight = query.fLeft + (GENERATE_EXTENTS / 20);
114 query.fBottom = query.fTop + (GENERATE_EXTENTS / 20);
115 break;
116 case kLarge_QueryType:
117 query.fLeft = rand.nextU() % GENERATE_EXTENTS;
118 query.fTop = rand.nextU() % GENERATE_EXTENTS;
119 query.fRight = query.fLeft + (GENERATE_EXTENTS / 2);
120 query.fBottom = query.fTop + (GENERATE_EXTENTS / 2);
121 break;
122 case kFull_QueryType:
123 query.fLeft = -GENERATE_EXTENTS;
124 query.fTop = -GENERATE_EXTENTS;
125 query.fRight = 2 * GENERATE_EXTENTS;
126 query.fBottom = 2 * GENERATE_EXTENTS;
127 break;
128 default: // fallthrough
129 case kRandom_QueryType:
130 query.fLeft = rand.nextU() % GENERATE_EXTENTS;
131 query.fTop = rand.nextU() % GENERATE_EXTENTS;
132 query.fRight = query.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 2);
133 query.fBottom = query.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 2);
134 break;
135 };
136 fTree->search(query, &hits);
137 }
138 }
139private:
140 SkBBoxHierarchy* fTree;
141 MakeRectProc fProc;
rileya@google.com61348d12012-09-06 13:38:53 +0000142 SkString fName;
rileya@google.com981b33a2012-09-05 18:36:17 +0000143 bool fBulkLoad;
144 QueryType fQuery;
145 typedef SkBenchmark INHERITED;
146};
147
humper@google.com05af1af2013-01-07 16:47:43 +0000148static inline SkIRect make_simple_rect(SkRandom&, int index, int numRects) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000149 SkIRect out = {0, 0, GENERATE_EXTENTS, GENERATE_EXTENTS};
150 return out;
151}
152
humper@google.com05af1af2013-01-07 16:47:43 +0000153static inline SkIRect make_concentric_rects_increasing(SkRandom&, int index, int numRects) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000154 SkIRect out = {0, 0, index + 1, index + 1};
155 return out;
156}
157
humper@google.com05af1af2013-01-07 16:47:43 +0000158static inline SkIRect make_concentric_rects_decreasing(SkRandom&, int index, int numRects) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000159 SkIRect out = {0, 0, numRects - index, numRects - index};
160 return out;
161}
162
humper@google.com05af1af2013-01-07 16:47:43 +0000163static inline SkIRect make_point_rects(SkRandom& rand, int index, int numRects) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000164 SkIRect out;
165 out.fLeft = rand.nextU() % GENERATE_EXTENTS;
166 out.fTop = rand.nextU() % GENERATE_EXTENTS;
167 out.fRight = out.fLeft + (GENERATE_EXTENTS / 200);
168 out.fBottom = out.fTop + (GENERATE_EXTENTS / 200);
169 return out;
170}
171
humper@google.com05af1af2013-01-07 16:47:43 +0000172static inline SkIRect make_random_rects(SkRandom& rand, int index, int numRects) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000173 SkIRect out;
174 out.fLeft = rand.nextS() % GENERATE_EXTENTS;
175 out.fTop = rand.nextS() % GENERATE_EXTENTS;
176 out.fRight = out.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 5);
177 out.fBottom = out.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 5);
178 return out;
179}
180
humper@google.com05af1af2013-01-07 16:47:43 +0000181static inline SkIRect make_large_rects(SkRandom& rand, int index, int numRects) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000182 SkIRect out;
183 out.fLeft = rand.nextU() % GENERATE_EXTENTS;
184 out.fTop = rand.nextU() % GENERATE_EXTENTS;
185 out.fRight = out.fLeft + (GENERATE_EXTENTS / 3);
186 out.fBottom = out.fTop + (GENERATE_EXTENTS / 3);
187 return out;
188}
189
190///////////////////////////////////////////////////////////////////////////////
191
humper@google.com05af1af2013-01-07 16:47:43 +0000192static inline SkBenchmark* Fact0(void* p) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000193 return SkNEW_ARGS(BBoxBuildBench, (p, "random", &make_random_rects, true,
194 SkRTree::Create(5, 16)));
195}
humper@google.com05af1af2013-01-07 16:47:43 +0000196static inline SkBenchmark* Fact1(void* p) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000197 return SkNEW_ARGS(BBoxBuildBench, (p, "random", &make_random_rects, false,
198 SkRTree::Create(5, 16)));
199}
humper@google.com05af1af2013-01-07 16:47:43 +0000200static inline SkBenchmark* Fact2(void* p) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000201 return SkNEW_ARGS(BBoxBuildBench, (p, "concentric",
202 &make_concentric_rects_increasing, true, SkRTree::Create(5, 16)));
203}
humper@google.com05af1af2013-01-07 16:47:43 +0000204static inline SkBenchmark* Fact3(void* p) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000205 return SkNEW_ARGS(BBoxQueryBench, (p, "random", &make_random_rects, true,
206 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16)));
207}
humper@google.com05af1af2013-01-07 16:47:43 +0000208static inline SkBenchmark* Fact4(void* p) {
rileya@google.com981b33a2012-09-05 18:36:17 +0000209 return SkNEW_ARGS(BBoxQueryBench, (p, "random", &make_random_rects, false,
210 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16)));
211}
212
213static BenchRegistry gReg0(Fact0);
214static BenchRegistry gReg1(Fact1);
215static BenchRegistry gReg2(Fact2);
216static BenchRegistry gReg3(Fact3);
217static BenchRegistry gReg4(Fact4);