blob: 965eb26d5345d40d18728ca29b14527664c52200 [file] [log] [blame]
bungeman@google.com55487522012-05-14 14:09:24 +00001/*
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.coma02bc152012-05-16 18:21:56 +00008#include "SkRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +00009#include "SkThread.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +000010#include "SkWeakRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +000011#include <memory>
12
13enum {
14 N = SkBENCHLOOP(1000000),
15 M = SkBENCHLOOP(2)
16};
17
18class RefCntBench_Stack : public SkBenchmark {
19public:
20 RefCntBench_Stack(void* param) : INHERITED(param) {
21 }
22protected:
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
37private:
38 typedef SkBenchmark INHERITED;
39};
40
41class PlacedRefCnt : public SkRefCnt {
42public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000043 SK_DECLARE_INST_COUNT(PlacedRefCnt)
44
bungeman@google.com55487522012-05-14 14:09:24 +000045 PlacedRefCnt() : SkRefCnt() { }
46 void operator delete(void *p) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000047
48private:
49 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000050};
51
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000052SK_DEFINE_INST_COUNT(PlacedRefCnt)
53
bungeman@google.com55487522012-05-14 14:09:24 +000054class RefCntBench_Heap : public SkBenchmark {
55public:
56 RefCntBench_Heap(void* param) : INHERITED(param) {
57 }
58protected:
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
75private:
76 typedef SkBenchmark INHERITED;
77};
78
bungeman@google.coma02bc152012-05-16 18:21:56 +000079class RefCntBench_New : public SkBenchmark {
80public:
81 RefCntBench_New(void* param) : INHERITED(param) {
82 }
83protected:
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
99private:
100 typedef SkBenchmark INHERITED;
101};
102
bungeman@google.com55487522012-05-14 14:09:24 +0000103///////////////////////////////////////////////////////////////////////////////
104
bungeman@google.coma02bc152012-05-16 18:21:56 +0000105class WeakRefCntBench_Stack : public SkBenchmark {
106public:
107 WeakRefCntBench_Stack(void* param) : INHERITED(param) {
108 }
109protected:
110 virtual const char* onGetName() {
111 return "ref_cnt_stack_weak";
112 }
bungeman@google.com55487522012-05-14 14:09:24 +0000113
bungeman@google.coma02bc152012-05-16 18:21:56 +0000114 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.com55487522012-05-14 14:09:24 +0000123
bungeman@google.coma02bc152012-05-16 18:21:56 +0000124private:
125 typedef SkBenchmark INHERITED;
126};
127
128class PlacedWeakRefCnt : public SkWeakRefCnt {
129public:
130 PlacedWeakRefCnt() : SkWeakRefCnt() { }
131 void operator delete(void *p) { }
132};
133
134class WeakRefCntBench_Heap : public SkBenchmark {
135public:
136 WeakRefCntBench_Heap(void* param) : INHERITED(param) {
137 }
138protected:
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
155private:
156 typedef SkBenchmark INHERITED;
157};
158
159class WeakRefCntBench_New : public SkBenchmark {
160public:
161 WeakRefCntBench_New(void* param) : INHERITED(param) {
162 }
163protected:
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
179private:
180 typedef SkBenchmark INHERITED;
181};
182
183///////////////////////////////////////////////////////////////////////////////
184
185static SkBenchmark* Fact00(void* p) { return new RefCntBench_Stack(p); }
186static SkBenchmark* Fact01(void* p) { return new RefCntBench_Heap(p); }
187static SkBenchmark* Fact02(void* p) { return new RefCntBench_New(p); }
188
189static SkBenchmark* Fact10(void* p) { return new WeakRefCntBench_Stack(p); }
190static SkBenchmark* Fact11(void* p) { return new WeakRefCntBench_Heap(p); }
191static SkBenchmark* Fact12(void* p) { return new WeakRefCntBench_New(p); }
192
193static BenchRegistry gReg00(Fact00);
194static BenchRegistry gReg01(Fact01);
195static BenchRegistry gReg02(Fact02);
196
197static BenchRegistry gReg10(Fact10);
198static BenchRegistry gReg11(Fact11);
199static BenchRegistry gReg12(Fact12);