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