blob: 418e149f80bdd801cdc7a51f2c0b081e6113d6f9 [file] [log] [blame]
reed@google.comebd24962012-05-17 14:28:11 +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
8#include "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkChunkAlloc.h"
13#include "SkString.h"
14
15class ChunkAllocBench : public SkBenchmark {
16 SkString fName;
17 size_t fMinSize;
reed@google.comebd24962012-05-17 14:28:11 +000018public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000019 ChunkAllocBench(size_t minSize) {
reed@google.comebd24962012-05-17 14:28:11 +000020 fMinSize = minSize;
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +000021 fName.printf("chunkalloc_" SK_SIZE_T_SPECIFIER, minSize);
tomhudson@google.com9dc27132012-09-13 15:50:24 +000022 fIsRendering = false;
reed@google.comebd24962012-05-17 14:28:11 +000023 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000024
reed@google.comebd24962012-05-17 14:28:11 +000025protected:
26 virtual const char* onGetName() SK_OVERRIDE {
27 return fName.c_str();
28 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029
sugoi@google.com77472f02013-03-05 18:50:01 +000030 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
reed@google.comebd24962012-05-17 14:28:11 +000031 size_t inc = fMinSize >> 4;
32 SkASSERT(inc > 0);
33 size_t total = fMinSize * 64;
34
35 SkChunkAlloc alloc(fMinSize);
36
mtklein@google.comc2897432013-09-10 19:23:38 +000037 for (int i = 0; i < this->getLoops(); ++i) {
reed@google.comebd24962012-05-17 14:28:11 +000038 size_t size = 0;
39 int calls = 0;
40 while (size < total) {
41 alloc.allocThrow(inc);
42 size += inc;
43 calls += 1;
44 }
45 alloc.reset();
46 }
47 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
reed@google.comebd24962012-05-17 14:28:11 +000049private:
50 typedef SkBenchmark INHERITED;
51};
52
mtklein@google.com410e6e82013-09-13 19:52:27 +000053DEF_BENCH( return new ChunkAllocBench(64); )
54DEF_BENCH( return new ChunkAllocBench(8*1024); )
mtklein@google.com519f9672013-09-20 14:31:45 +000055
56static int* calloc(size_t num) {
57 return (int*)sk_calloc_throw(num*sizeof(int));
58}
59
60static int* malloc_bzero(size_t num) {
61 const size_t bytes = num*sizeof(int);
62 int* ints = (int*)sk_malloc_throw(bytes);
63 sk_bzero(ints, bytes);
64 return ints;
65}
66
67class ZerosBench : public SkBenchmark {
68 size_t fNum;
69 bool fRead;
70 bool fWrite;
71 bool fUseCalloc;
72 SkString fName;
73public:
74 ZerosBench(size_t num, bool read, bool write, bool useCalloc)
75 : fNum(num)
76 , fRead(read)
77 , fWrite(write)
78 , fUseCalloc(useCalloc) {
79 fName.printf("memory_%s", useCalloc ? "calloc" : "malloc_bzero");
80 if (read && write) {
81 fName.appendf("_rw");
82 } else if (read) {
83 fName.appendf("_r");
84 } else if (write) {
85 fName.appendf("_w");
86 }
87 fName.appendf("_"SK_SIZE_T_SPECIFIER, num);
88 fIsRendering = false;
89 }
90
91protected:
92 virtual const char* onGetName() SK_OVERRIDE {
93 return fName.c_str();
94 }
95
96 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
97 for (int i = 0; i < this->getLoops(); i++) {
98 int* zeros = fUseCalloc ? calloc(fNum) : malloc_bzero(fNum);
99 if (fRead) {
100 volatile int x = 15;
101 for (size_t j = 0; j < fNum; j++) {
102 x ^= zeros[j];
103 }
104 }
105 if (fWrite) {
106 for (size_t j = 0; j < fNum; j++) {
107 zeros[j] = 15;
108 }
109 }
110 sk_free(zeros);
111 }
112 }
113};
114
115// zero count r w useCalloc?
116DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 0))
117DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 1))
118DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 0))
119DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 1))
120DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 0))
121DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 1))
122DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 0))
123DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 1))
124
125DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 0))
126DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 1))
127DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 0))
128DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 1))
129DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 0))
130DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 1))
131DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 0))
132DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 1))
133
134DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 0))
135DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 1))
136DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 0))
137DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 1))
138DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 0))
139DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 1))
140DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 0))
141DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 1))
142
143DEF_BENCH(return new ZerosBench(300, 0, 0, 0))
144DEF_BENCH(return new ZerosBench(300, 0, 0, 1))
145DEF_BENCH(return new ZerosBench(300, 0, 1, 0))
146DEF_BENCH(return new ZerosBench(300, 0, 1, 1))
147DEF_BENCH(return new ZerosBench(300, 1, 0, 0))
148DEF_BENCH(return new ZerosBench(300, 1, 0, 1))
149DEF_BENCH(return new ZerosBench(300, 1, 1, 0))
150DEF_BENCH(return new ZerosBench(300, 1, 1, 1))
151
152DEF_BENCH(return new ZerosBench(4, 0, 0, 0))
153DEF_BENCH(return new ZerosBench(4, 0, 0, 1))
154DEF_BENCH(return new ZerosBench(4, 0, 1, 0))
155DEF_BENCH(return new ZerosBench(4, 0, 1, 1))
156DEF_BENCH(return new ZerosBench(4, 1, 0, 0))
157DEF_BENCH(return new ZerosBench(4, 1, 0, 1))
158DEF_BENCH(return new ZerosBench(4, 1, 1, 0))
159DEF_BENCH(return new ZerosBench(4, 1, 1, 1))