blob: f21317a21a7eb54dba083b24814e0476ba98f2dd [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:
43 PlacedRefCnt() : SkRefCnt() { }
44 void operator delete(void *p) { }
45};
46
47class RefCntBench_Heap : public SkBenchmark {
48public:
49 RefCntBench_Heap(void* param) : INHERITED(param) {
50 }
51protected:
52 virtual const char* onGetName() {
53 return "ref_cnt_heap";
54 }
55
56 virtual void onDraw(SkCanvas* canvas) {
57 char memory[sizeof(PlacedRefCnt)];
58 for (int i = 0; i < N; ++i) {
59 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
60 for (int j = 0; j < M; ++j) {
61 ref->ref();
62 ref->unref();
63 }
64 ref->unref();
65 }
66 }
67
68private:
69 typedef SkBenchmark INHERITED;
70};
71
bungeman@google.coma02bc152012-05-16 18:21:56 +000072class RefCntBench_New : public SkBenchmark {
73public:
74 RefCntBench_New(void* param) : INHERITED(param) {
75 }
76protected:
77 virtual const char* onGetName() {
78 return "ref_cnt_new";
79 }
80
81 virtual void onDraw(SkCanvas* canvas) {
82 for (int i = 0; i < N; ++i) {
83 SkRefCnt* ref = new SkRefCnt();
84 for (int j = 0; j < M; ++j) {
85 ref->ref();
86 ref->unref();
87 }
88 ref->unref();
89 }
90 }
91
92private:
93 typedef SkBenchmark INHERITED;
94};
95
bungeman@google.com55487522012-05-14 14:09:24 +000096///////////////////////////////////////////////////////////////////////////////
97
bungeman@google.coma02bc152012-05-16 18:21:56 +000098class WeakRefCntBench_Stack : public SkBenchmark {
99public:
100 WeakRefCntBench_Stack(void* param) : INHERITED(param) {
101 }
102protected:
103 virtual const char* onGetName() {
104 return "ref_cnt_stack_weak";
105 }
bungeman@google.com55487522012-05-14 14:09:24 +0000106
bungeman@google.coma02bc152012-05-16 18:21:56 +0000107 virtual void onDraw(SkCanvas* canvas) {
108 for (int i = 0; i < N; ++i) {
109 SkWeakRefCnt ref;
110 for (int j = 0; j < M; ++j) {
111 ref.ref();
112 ref.unref();
113 }
114 }
115 }
bungeman@google.com55487522012-05-14 14:09:24 +0000116
bungeman@google.coma02bc152012-05-16 18:21:56 +0000117private:
118 typedef SkBenchmark INHERITED;
119};
120
121class PlacedWeakRefCnt : public SkWeakRefCnt {
122public:
123 PlacedWeakRefCnt() : SkWeakRefCnt() { }
124 void operator delete(void *p) { }
125};
126
127class WeakRefCntBench_Heap : public SkBenchmark {
128public:
129 WeakRefCntBench_Heap(void* param) : INHERITED(param) {
130 }
131protected:
132 virtual const char* onGetName() {
133 return "ref_cnt_heap_weak";
134 }
135
136 virtual void onDraw(SkCanvas* canvas) {
137 char memory[sizeof(PlacedWeakRefCnt)];
138 for (int i = 0; i < N; ++i) {
139 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
140 for (int j = 0; j < M; ++j) {
141 ref->ref();
142 ref->unref();
143 }
144 ref->unref();
145 }
146 }
147
148private:
149 typedef SkBenchmark INHERITED;
150};
151
152class WeakRefCntBench_New : public SkBenchmark {
153public:
154 WeakRefCntBench_New(void* param) : INHERITED(param) {
155 }
156protected:
157 virtual const char* onGetName() {
158 return "ref_cnt_new_weak";
159 }
160
161 virtual void onDraw(SkCanvas* canvas) {
162 for (int i = 0; i < N; ++i) {
163 SkWeakRefCnt* ref = new SkWeakRefCnt();
164 for (int j = 0; j < M; ++j) {
165 ref->ref();
166 ref->unref();
167 }
168 ref->unref();
169 }
170 }
171
172private:
173 typedef SkBenchmark INHERITED;
174};
175
176///////////////////////////////////////////////////////////////////////////////
177
178static SkBenchmark* Fact00(void* p) { return new RefCntBench_Stack(p); }
179static SkBenchmark* Fact01(void* p) { return new RefCntBench_Heap(p); }
180static SkBenchmark* Fact02(void* p) { return new RefCntBench_New(p); }
181
182static SkBenchmark* Fact10(void* p) { return new WeakRefCntBench_Stack(p); }
183static SkBenchmark* Fact11(void* p) { return new WeakRefCntBench_Heap(p); }
184static SkBenchmark* Fact12(void* p) { return new WeakRefCntBench_New(p); }
185
186static BenchRegistry gReg00(Fact00);
187static BenchRegistry gReg01(Fact01);
188static BenchRegistry gReg02(Fact02);
189
190static BenchRegistry gReg10(Fact10);
191static BenchRegistry gReg11(Fact11);
192static BenchRegistry gReg12(Fact12);