bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | // This tests a Gr class |
| 11 | #if SK_SUPPORT_GPU |
| 12 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | #include "Benchmark.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 14 | #include "GrMemoryPool.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 16 | #include "SkTDArray.h" |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 17 | #include "SkTemplates.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 18 | |
| 19 | // change this to 0 to compare GrMemoryPool to default new / delete |
| 20 | #define OVERRIDE_NEW 1 |
| 21 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 22 | struct A { |
| 23 | int gStuff[10]; |
| 24 | #if OVERRIDE_NEW |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 25 | void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 26 | void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } } |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 27 | #endif |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 28 | static GrMemoryPool gBenchPool; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 29 | }; |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 30 | GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * This benchmark creates and deletes objects in stack order |
| 34 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 35 | class GrMemoryPoolBenchStack : public Benchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 36 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 38 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 39 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 41 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 42 | const char* onGetName() override { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 43 | return "grmemorypool_stack"; |
| 44 | } |
| 45 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 46 | void onDraw(const int loops, SkCanvas*) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 47 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 48 | enum { |
| 49 | kMaxObjects = 4 * (1 << 10), |
| 50 | }; |
| 51 | A* objects[kMaxObjects]; |
| 52 | |
| 53 | // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise, |
| 54 | // we allocate. We start allocate-biased and ping-pong to delete-biased |
| 55 | SkFixed delThresh = -SK_FixedHalf; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 56 | const int kSwitchThreshPeriod = loops / (2 * kMaxObjects); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 57 | int s = 0; |
| 58 | |
| 59 | int count = 0; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 60 | for (int i = 0; i < loops; i++, ++s) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 61 | if (kSwitchThreshPeriod == s) { |
| 62 | delThresh = -delThresh; |
| 63 | s = 0; |
| 64 | } |
| 65 | SkFixed del = r.nextSFixed1(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 66 | if (count && |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 67 | (kMaxObjects == count || del < delThresh)) { |
| 68 | delete objects[count-1]; |
| 69 | --count; |
| 70 | } else { |
| 71 | objects[count] = new A; |
| 72 | ++count; |
| 73 | } |
| 74 | } |
| 75 | for (int i = 0; i < count; ++i) { |
| 76 | delete objects[i]; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 81 | typedef Benchmark INHERITED; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 84 | struct B { |
| 85 | int gStuff[10]; |
| 86 | #if OVERRIDE_NEW |
| 87 | void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 88 | void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } } |
| 89 | #endif |
| 90 | static GrMemoryPool gBenchPool; |
| 91 | }; |
| 92 | GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
| 93 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 94 | /** |
| 95 | * This benchmark creates objects and deletes them in random order |
| 96 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 97 | class GrMemoryPoolBenchRandom : public Benchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 98 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 100 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 101 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 103 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 104 | const char* onGetName() override { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 105 | return "grmemorypool_random"; |
| 106 | } |
| 107 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 108 | void onDraw(const int loops, SkCanvas*) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 109 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 110 | enum { |
| 111 | kMaxObjects = 4 * (1 << 10), |
| 112 | }; |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 113 | SkAutoTDelete<B> objects[kMaxObjects]; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 114 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 115 | for (int i = 0; i < loops; i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 116 | uint32_t idx = r.nextRangeU(0, kMaxObjects-1); |
| 117 | if (NULL == objects[idx].get()) { |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 118 | objects[idx].reset(new B); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 119 | } else { |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 120 | objects[idx].free(); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 126 | typedef Benchmark INHERITED; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 129 | struct C { |
| 130 | int gStuff[10]; |
| 131 | #if OVERRIDE_NEW |
| 132 | void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 133 | void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } } |
| 134 | #endif |
| 135 | static GrMemoryPool gBenchPool; |
| 136 | }; |
| 137 | GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
| 138 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 139 | /** |
| 140 | * This benchmark creates objects and deletes them in queue order |
| 141 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 142 | class GrMemoryPoolBenchQueue : public Benchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 143 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 144 | M = 4 * (1 << 10), |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 145 | }; |
| 146 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 147 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 148 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 149 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 150 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 151 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 152 | const char* onGetName() override { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 153 | return "grmemorypool_queue"; |
| 154 | } |
| 155 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 156 | void onDraw(const int loops, SkCanvas*) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 157 | SkRandom r; |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 158 | C* objects[M]; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 159 | for (int i = 0; i < loops; i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 160 | uint32_t count = r.nextRangeU(0, M-1); |
| 161 | for (uint32_t i = 0; i < count; i++) { |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 162 | objects[i] = new C; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 163 | } |
| 164 | for (uint32_t i = 0; i < count; i++) { |
| 165 | delete objects[i]; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 171 | typedef Benchmark INHERITED; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | /////////////////////////////////////////////////////////////////////////////// |
| 175 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 176 | DEF_BENCH( return new GrMemoryPoolBenchStack(); ) |
| 177 | DEF_BENCH( return new GrMemoryPoolBenchRandom(); ) |
| 178 | DEF_BENCH( return new GrMemoryPoolBenchQueue(); ) |
commit-bot@chromium.org | 97b4b67 | 2013-09-26 19:23:03 +0000 | [diff] [blame] | 179 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 180 | #endif |