blob: 26c98b9fe4c5c7edc87e5f27543ea83ade2de186 [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 {
tomhudson@google.comfc157222012-09-13 15:42:33 +000014 N = SkBENCHLOOP(100000),
bungeman@google.com55487522012-05-14 14:09:24 +000015 M = SkBENCHLOOP(2)
16};
17
18class RefCntBench_Stack : public SkBenchmark {
19public:
20 RefCntBench_Stack(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000021 fIsRendering = false;
bungeman@google.com55487522012-05-14 14:09:24 +000022 }
23protected:
24 virtual const char* onGetName() {
25 return "ref_cnt_stack";
26 }
27
sugoi@google.com77472f02013-03-05 18:50:01 +000028 virtual void onDraw(SkCanvas*) {
bungeman@google.com55487522012-05-14 14:09:24 +000029 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
38private:
39 typedef SkBenchmark INHERITED;
40};
41
42class PlacedRefCnt : public SkRefCnt {
43public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000044 SK_DECLARE_INST_COUNT(PlacedRefCnt)
45
bungeman@google.com55487522012-05-14 14:09:24 +000046 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000047 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000048
49private:
50 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000051};
52
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000053SK_DEFINE_INST_COUNT(PlacedRefCnt)
54
bungeman@google.com55487522012-05-14 14:09:24 +000055class RefCntBench_Heap : public SkBenchmark {
56public:
57 RefCntBench_Heap(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000058 fIsRendering = false;
bungeman@google.com55487522012-05-14 14:09:24 +000059 }
60protected:
61 virtual const char* onGetName() {
62 return "ref_cnt_heap";
63 }
64
sugoi@google.com77472f02013-03-05 18:50:01 +000065 virtual void onDraw(SkCanvas*) {
bungeman@google.com55487522012-05-14 14:09:24 +000066 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
77private:
78 typedef SkBenchmark INHERITED;
79};
80
bungeman@google.coma02bc152012-05-16 18:21:56 +000081class RefCntBench_New : public SkBenchmark {
82public:
83 RefCntBench_New(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000084 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +000085 }
86protected:
87 virtual const char* onGetName() {
88 return "ref_cnt_new";
89 }
90
sugoi@google.com77472f02013-03-05 18:50:01 +000091 virtual void onDraw(SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000092 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
102private:
103 typedef SkBenchmark INHERITED;
104};
105
bungeman@google.com55487522012-05-14 14:09:24 +0000106///////////////////////////////////////////////////////////////////////////////
107
bungeman@google.coma02bc152012-05-16 18:21:56 +0000108class WeakRefCntBench_Stack : public SkBenchmark {
109public:
110 WeakRefCntBench_Stack(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000111 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000112 }
113protected:
114 virtual const char* onGetName() {
115 return "ref_cnt_stack_weak";
116 }
bungeman@google.com55487522012-05-14 14:09:24 +0000117
sugoi@google.com77472f02013-03-05 18:50:01 +0000118 virtual void onDraw(SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000119 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.com55487522012-05-14 14:09:24 +0000127
bungeman@google.coma02bc152012-05-16 18:21:56 +0000128private:
129 typedef SkBenchmark INHERITED;
130};
131
132class PlacedWeakRefCnt : public SkWeakRefCnt {
133public:
134 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000135 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000136};
137
138class WeakRefCntBench_Heap : public SkBenchmark {
139public:
140 WeakRefCntBench_Heap(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000141 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000142 }
143protected:
144 virtual const char* onGetName() {
145 return "ref_cnt_heap_weak";
146 }
147
sugoi@google.com77472f02013-03-05 18:50:01 +0000148 virtual void onDraw(SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000149 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
160private:
161 typedef SkBenchmark INHERITED;
162};
163
164class WeakRefCntBench_New : public SkBenchmark {
165public:
166 WeakRefCntBench_New(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000167 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000168 }
169protected:
170 virtual const char* onGetName() {
171 return "ref_cnt_new_weak";
172 }
173
sugoi@google.com77472f02013-03-05 18:50:01 +0000174 virtual void onDraw(SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000175 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
185private:
186 typedef SkBenchmark INHERITED;
187};
188
189///////////////////////////////////////////////////////////////////////////////
190
191static SkBenchmark* Fact00(void* p) { return new RefCntBench_Stack(p); }
192static SkBenchmark* Fact01(void* p) { return new RefCntBench_Heap(p); }
193static SkBenchmark* Fact02(void* p) { return new RefCntBench_New(p); }
194
195static SkBenchmark* Fact10(void* p) { return new WeakRefCntBench_Stack(p); }
196static SkBenchmark* Fact11(void* p) { return new WeakRefCntBench_Heap(p); }
197static SkBenchmark* Fact12(void* p) { return new WeakRefCntBench_New(p); }
198
199static BenchRegistry gReg00(Fact00);
200static BenchRegistry gReg01(Fact01);
201static BenchRegistry gReg02(Fact02);
202
203static BenchRegistry gReg10(Fact10);
204static BenchRegistry gReg11(Fact11);
205static BenchRegistry gReg12(Fact12);