bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +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 | #include "SkBenchmark.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 8 | #include "SkRefCnt.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 9 | #include "SkThread.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 10 | #include "SkWeakRefCnt.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
| 13 | enum { |
| 14 | N = SkBENCHLOOP(1000000), |
| 15 | M = SkBENCHLOOP(2) |
| 16 | }; |
| 17 | |
| 18 | class RefCntBench_Stack : public SkBenchmark { |
| 19 | public: |
| 20 | RefCntBench_Stack(void* param) : INHERITED(param) { |
| 21 | } |
| 22 | protected: |
| 23 | virtual const char* onGetName() { |
| 24 | return "ref_cnt_stack"; |
| 25 | } |
| 26 | |
| 27 | virtual void onDraw(SkCanvas* canvas) { |
| 28 | for (int i = 0; i < N; ++i) { |
| 29 | SkRefCnt ref; |
| 30 | for (int j = 0; j < M; ++j) { |
| 31 | ref.ref(); |
| 32 | ref.unref(); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | typedef SkBenchmark INHERITED; |
| 39 | }; |
| 40 | |
| 41 | class PlacedRefCnt : public SkRefCnt { |
| 42 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 43 | SK_DECLARE_INST_COUNT(PlacedRefCnt) |
| 44 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 45 | PlacedRefCnt() : SkRefCnt() { } |
| 46 | void operator delete(void *p) { } |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | typedef SkRefCnt INHERITED; |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 52 | SK_DEFINE_INST_COUNT(PlacedRefCnt) |
| 53 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 54 | class RefCntBench_Heap : public SkBenchmark { |
| 55 | public: |
| 56 | RefCntBench_Heap(void* param) : INHERITED(param) { |
| 57 | } |
| 58 | protected: |
| 59 | virtual const char* onGetName() { |
| 60 | return "ref_cnt_heap"; |
| 61 | } |
| 62 | |
| 63 | virtual void onDraw(SkCanvas* canvas) { |
| 64 | char memory[sizeof(PlacedRefCnt)]; |
| 65 | for (int i = 0; i < N; ++i) { |
| 66 | PlacedRefCnt* ref = new (memory) PlacedRefCnt(); |
| 67 | for (int j = 0; j < M; ++j) { |
| 68 | ref->ref(); |
| 69 | ref->unref(); |
| 70 | } |
| 71 | ref->unref(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | typedef SkBenchmark INHERITED; |
| 77 | }; |
| 78 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 79 | class RefCntBench_New : public SkBenchmark { |
| 80 | public: |
| 81 | RefCntBench_New(void* param) : INHERITED(param) { |
| 82 | } |
| 83 | protected: |
| 84 | virtual const char* onGetName() { |
| 85 | return "ref_cnt_new"; |
| 86 | } |
| 87 | |
| 88 | virtual void onDraw(SkCanvas* canvas) { |
| 89 | for (int i = 0; i < N; ++i) { |
| 90 | SkRefCnt* ref = new SkRefCnt(); |
| 91 | for (int j = 0; j < M; ++j) { |
| 92 | ref->ref(); |
| 93 | ref->unref(); |
| 94 | } |
| 95 | ref->unref(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | typedef SkBenchmark INHERITED; |
| 101 | }; |
| 102 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 105 | class WeakRefCntBench_Stack : public SkBenchmark { |
| 106 | public: |
| 107 | WeakRefCntBench_Stack(void* param) : INHERITED(param) { |
| 108 | } |
| 109 | protected: |
| 110 | virtual const char* onGetName() { |
| 111 | return "ref_cnt_stack_weak"; |
| 112 | } |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 113 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 114 | virtual void onDraw(SkCanvas* canvas) { |
| 115 | for (int i = 0; i < N; ++i) { |
| 116 | SkWeakRefCnt ref; |
| 117 | for (int j = 0; j < M; ++j) { |
| 118 | ref.ref(); |
| 119 | ref.unref(); |
| 120 | } |
| 121 | } |
| 122 | } |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 123 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 124 | private: |
| 125 | typedef SkBenchmark INHERITED; |
| 126 | }; |
| 127 | |
| 128 | class PlacedWeakRefCnt : public SkWeakRefCnt { |
| 129 | public: |
| 130 | PlacedWeakRefCnt() : SkWeakRefCnt() { } |
| 131 | void operator delete(void *p) { } |
| 132 | }; |
| 133 | |
| 134 | class WeakRefCntBench_Heap : public SkBenchmark { |
| 135 | public: |
| 136 | WeakRefCntBench_Heap(void* param) : INHERITED(param) { |
| 137 | } |
| 138 | protected: |
| 139 | virtual const char* onGetName() { |
| 140 | return "ref_cnt_heap_weak"; |
| 141 | } |
| 142 | |
| 143 | virtual void onDraw(SkCanvas* canvas) { |
| 144 | char memory[sizeof(PlacedWeakRefCnt)]; |
| 145 | for (int i = 0; i < N; ++i) { |
| 146 | PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt(); |
| 147 | for (int j = 0; j < M; ++j) { |
| 148 | ref->ref(); |
| 149 | ref->unref(); |
| 150 | } |
| 151 | ref->unref(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | typedef SkBenchmark INHERITED; |
| 157 | }; |
| 158 | |
| 159 | class WeakRefCntBench_New : public SkBenchmark { |
| 160 | public: |
| 161 | WeakRefCntBench_New(void* param) : INHERITED(param) { |
| 162 | } |
| 163 | protected: |
| 164 | virtual const char* onGetName() { |
| 165 | return "ref_cnt_new_weak"; |
| 166 | } |
| 167 | |
| 168 | virtual void onDraw(SkCanvas* canvas) { |
| 169 | for (int i = 0; i < N; ++i) { |
| 170 | SkWeakRefCnt* ref = new SkWeakRefCnt(); |
| 171 | for (int j = 0; j < M; ++j) { |
| 172 | ref->ref(); |
| 173 | ref->unref(); |
| 174 | } |
| 175 | ref->unref(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | typedef SkBenchmark INHERITED; |
| 181 | }; |
| 182 | |
| 183 | /////////////////////////////////////////////////////////////////////////////// |
| 184 | |
| 185 | static SkBenchmark* Fact00(void* p) { return new RefCntBench_Stack(p); } |
| 186 | static SkBenchmark* Fact01(void* p) { return new RefCntBench_Heap(p); } |
| 187 | static SkBenchmark* Fact02(void* p) { return new RefCntBench_New(p); } |
| 188 | |
| 189 | static SkBenchmark* Fact10(void* p) { return new WeakRefCntBench_Stack(p); } |
| 190 | static SkBenchmark* Fact11(void* p) { return new WeakRefCntBench_Heap(p); } |
| 191 | static SkBenchmark* Fact12(void* p) { return new WeakRefCntBench_New(p); } |
| 192 | |
| 193 | static BenchRegistry gReg00(Fact00); |
| 194 | static BenchRegistry gReg01(Fact01); |
| 195 | static BenchRegistry gReg02(Fact02); |
| 196 | |
| 197 | static BenchRegistry gReg10(Fact10); |
| 198 | static BenchRegistry gReg11(Fact11); |
| 199 | static BenchRegistry gReg12(Fact12); |