blob: 19919ab635e5b180aadb20a4018d4cebfdcfa8bb [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);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000022 }
23
24 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
25 return backend == kNonRendering_Backend;
reed@google.comebd24962012-05-17 14:28:11 +000026 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000027
reed@google.comebd24962012-05-17 14:28:11 +000028protected:
29 virtual const char* onGetName() SK_OVERRIDE {
30 return fName.c_str();
31 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032
commit-bot@chromium.org33614712013-12-03 18:17:16 +000033 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
reed@google.comebd24962012-05-17 14:28:11 +000034 size_t inc = fMinSize >> 4;
35 SkASSERT(inc > 0);
36 size_t total = fMinSize * 64;
37
38 SkChunkAlloc alloc(fMinSize);
39
commit-bot@chromium.org33614712013-12-03 18:17:16 +000040 for (int i = 0; i < loops; ++i) {
reed@google.comebd24962012-05-17 14:28:11 +000041 size_t size = 0;
42 int calls = 0;
43 while (size < total) {
44 alloc.allocThrow(inc);
45 size += inc;
46 calls += 1;
47 }
48 alloc.reset();
49 }
50 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@google.comebd24962012-05-17 14:28:11 +000052private:
53 typedef SkBenchmark INHERITED;
54};
55
mtklein@google.com410e6e82013-09-13 19:52:27 +000056DEF_BENCH( return new ChunkAllocBench(64); )
57DEF_BENCH( return new ChunkAllocBench(8*1024); )
mtklein@google.com519f9672013-09-20 14:31:45 +000058
59static int* calloc(size_t num) {
60 return (int*)sk_calloc_throw(num*sizeof(int));
61}
62
63static int* malloc_bzero(size_t num) {
64 const size_t bytes = num*sizeof(int);
65 int* ints = (int*)sk_malloc_throw(bytes);
66 sk_bzero(ints, bytes);
67 return ints;
68}
69
70class ZerosBench : public SkBenchmark {
71 size_t fNum;
72 bool fRead;
73 bool fWrite;
74 bool fUseCalloc;
75 SkString fName;
76public:
77 ZerosBench(size_t num, bool read, bool write, bool useCalloc)
78 : fNum(num)
79 , fRead(read)
80 , fWrite(write)
81 , fUseCalloc(useCalloc) {
82 fName.printf("memory_%s", useCalloc ? "calloc" : "malloc_bzero");
83 if (read && write) {
84 fName.appendf("_rw");
85 } else if (read) {
86 fName.appendf("_r");
87 } else if (write) {
88 fName.appendf("_w");
89 }
90 fName.appendf("_"SK_SIZE_T_SPECIFIER, num);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000091 }
92
93 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
94 return backend == kNonRendering_Backend;
mtklein@google.com519f9672013-09-20 14:31:45 +000095 }
96
97protected:
98 virtual const char* onGetName() SK_OVERRIDE {
99 return fName.c_str();
100 }
101
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000102 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
103 for (int i = 0; i < loops; i++) {
mtklein@google.com519f9672013-09-20 14:31:45 +0000104 int* zeros = fUseCalloc ? calloc(fNum) : malloc_bzero(fNum);
105 if (fRead) {
106 volatile int x = 15;
107 for (size_t j = 0; j < fNum; j++) {
108 x ^= zeros[j];
109 }
110 }
111 if (fWrite) {
112 for (size_t j = 0; j < fNum; j++) {
113 zeros[j] = 15;
114 }
115 }
116 sk_free(zeros);
117 }
118 }
119};
120
121// zero count r w useCalloc?
122DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 0))
123DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 1))
124DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 0))
125DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 1))
126DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 0))
127DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 1))
128DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 0))
129DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 1))
130
131DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 0))
132DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 1))
133DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 0))
134DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 1))
135DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 0))
136DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 1))
137DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 0))
138DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 1))
139
140DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 0))
141DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 1))
142DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 0))
143DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 1))
144DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 0))
145DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 1))
146DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 0))
147DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 1))
148
149DEF_BENCH(return new ZerosBench(300, 0, 0, 0))
150DEF_BENCH(return new ZerosBench(300, 0, 0, 1))
151DEF_BENCH(return new ZerosBench(300, 0, 1, 0))
152DEF_BENCH(return new ZerosBench(300, 0, 1, 1))
153DEF_BENCH(return new ZerosBench(300, 1, 0, 0))
154DEF_BENCH(return new ZerosBench(300, 1, 0, 1))
155DEF_BENCH(return new ZerosBench(300, 1, 1, 0))
156DEF_BENCH(return new ZerosBench(300, 1, 1, 1))
157
158DEF_BENCH(return new ZerosBench(4, 0, 0, 0))
159DEF_BENCH(return new ZerosBench(4, 0, 0, 1))
160DEF_BENCH(return new ZerosBench(4, 0, 1, 0))
161DEF_BENCH(return new ZerosBench(4, 0, 1, 1))
162DEF_BENCH(return new ZerosBench(4, 1, 0, 0))
163DEF_BENCH(return new ZerosBench(4, 1, 0, 1))
164DEF_BENCH(return new ZerosBench(4, 1, 1, 0))
165DEF_BENCH(return new ZerosBench(4, 1, 1, 1))