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