reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | class ChunkAllocBench : public SkBenchmark { |
| 16 | SkString fName; |
| 17 | size_t fMinSize; |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 18 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 19 | ChunkAllocBench(size_t minSize) { |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 20 | fMinSize = minSize; |
senorblanco@chromium.org | 3a67a66 | 2012-07-09 18:22:08 +0000 | [diff] [blame] | 21 | fName.printf("chunkalloc_" SK_SIZE_T_SPECIFIER, minSize); |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 22 | fIsRendering = false; |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 23 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 24 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 25 | protected: |
| 26 | virtual const char* onGetName() SK_OVERRIDE { |
| 27 | return fName.c_str(); |
| 28 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 29 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 30 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 31 | size_t inc = fMinSize >> 4; |
| 32 | SkASSERT(inc > 0); |
| 33 | size_t total = fMinSize * 64; |
| 34 | |
| 35 | SkChunkAlloc alloc(fMinSize); |
| 36 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 37 | for (int i = 0; i < this->getLoops(); ++i) { |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 38 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 48 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 49 | private: |
| 50 | typedef SkBenchmark INHERITED; |
| 51 | }; |
| 52 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 53 | DEF_BENCH( return new ChunkAllocBench(64); ) |
| 54 | DEF_BENCH( return new ChunkAllocBench(8*1024); ) |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 55 | |
| 56 | static int* calloc(size_t num) { |
| 57 | return (int*)sk_calloc_throw(num*sizeof(int)); |
| 58 | } |
| 59 | |
| 60 | static 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 | |
| 67 | class ZerosBench : public SkBenchmark { |
| 68 | size_t fNum; |
| 69 | bool fRead; |
| 70 | bool fWrite; |
| 71 | bool fUseCalloc; |
| 72 | SkString fName; |
| 73 | public: |
| 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 | |
| 91 | protected: |
| 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? |
| 116 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 0)) |
| 117 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 1)) |
| 118 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 0)) |
| 119 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 1)) |
| 120 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 0)) |
| 121 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 1)) |
| 122 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 0)) |
| 123 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 1)) |
| 124 | |
| 125 | DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 0)) |
| 126 | DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 1)) |
| 127 | DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 0)) |
| 128 | DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 1)) |
| 129 | DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 0)) |
| 130 | DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 1)) |
| 131 | DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 0)) |
| 132 | DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 1)) |
| 133 | |
| 134 | DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 0)) |
| 135 | DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 1)) |
| 136 | DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 0)) |
| 137 | DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 1)) |
| 138 | DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 0)) |
| 139 | DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 1)) |
| 140 | DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 0)) |
| 141 | DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 1)) |
| 142 | |
| 143 | DEF_BENCH(return new ZerosBench(300, 0, 0, 0)) |
| 144 | DEF_BENCH(return new ZerosBench(300, 0, 0, 1)) |
| 145 | DEF_BENCH(return new ZerosBench(300, 0, 1, 0)) |
| 146 | DEF_BENCH(return new ZerosBench(300, 0, 1, 1)) |
| 147 | DEF_BENCH(return new ZerosBench(300, 1, 0, 0)) |
| 148 | DEF_BENCH(return new ZerosBench(300, 1, 0, 1)) |
| 149 | DEF_BENCH(return new ZerosBench(300, 1, 1, 0)) |
| 150 | DEF_BENCH(return new ZerosBench(300, 1, 1, 1)) |
| 151 | |
| 152 | DEF_BENCH(return new ZerosBench(4, 0, 0, 0)) |
| 153 | DEF_BENCH(return new ZerosBench(4, 0, 0, 1)) |
| 154 | DEF_BENCH(return new ZerosBench(4, 0, 1, 0)) |
| 155 | DEF_BENCH(return new ZerosBench(4, 0, 1, 1)) |
| 156 | DEF_BENCH(return new ZerosBench(4, 1, 0, 0)) |
| 157 | DEF_BENCH(return new ZerosBench(4, 1, 0, 1)) |
| 158 | DEF_BENCH(return new ZerosBench(4, 1, 1, 0)) |
| 159 | DEF_BENCH(return new ZerosBench(4, 1, 1, 1)) |