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); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 25 | return backend == kNonRendering_Backend; |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 26 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 27 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 28 | protected: |
| 29 | virtual const char* onGetName() SK_OVERRIDE { |
| 30 | return fName.c_str(); |
| 31 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 33 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 34 | size_t inc = fMinSize >> 4; |
| 35 | SkASSERT(inc > 0); |
| 36 | size_t total = fMinSize * 64; |
| 37 | |
| 38 | SkChunkAlloc alloc(fMinSize); |
| 39 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 40 | for (int i = 0; i < loops; ++i) { |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 41 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 51 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 52 | private: |
| 53 | typedef SkBenchmark INHERITED; |
| 54 | }; |
| 55 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 56 | DEF_BENCH( return new ChunkAllocBench(64); ) |
| 57 | DEF_BENCH( return new ChunkAllocBench(8*1024); ) |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 58 | |
| 59 | static int* calloc(size_t num) { |
| 60 | return (int*)sk_calloc_throw(num*sizeof(int)); |
| 61 | } |
| 62 | |
| 63 | static 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 | |
| 70 | class ZerosBench : public SkBenchmark { |
| 71 | size_t fNum; |
| 72 | bool fRead; |
| 73 | bool fWrite; |
| 74 | bool fUseCalloc; |
| 75 | SkString fName; |
| 76 | public: |
| 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.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 94 | return backend == kNonRendering_Backend; |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | protected: |
| 98 | virtual const char* onGetName() SK_OVERRIDE { |
| 99 | return fName.c_str(); |
| 100 | } |
| 101 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 102 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
| 103 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 104 | 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? |
| 122 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 0)) |
| 123 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 1)) |
| 124 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 0)) |
| 125 | DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 1)) |
| 126 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 0)) |
| 127 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 1)) |
| 128 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 0)) |
| 129 | DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 1)) |
| 130 | |
| 131 | DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 0)) |
| 132 | DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 1)) |
| 133 | DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 0)) |
| 134 | DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 1)) |
| 135 | DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 0)) |
| 136 | DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 1)) |
| 137 | DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 0)) |
| 138 | DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 1)) |
| 139 | |
| 140 | DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 0)) |
| 141 | DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 1)) |
| 142 | DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 0)) |
| 143 | DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 1)) |
| 144 | DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 0)) |
| 145 | DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 1)) |
| 146 | DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 0)) |
| 147 | DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 1)) |
| 148 | |
| 149 | DEF_BENCH(return new ZerosBench(300, 0, 0, 0)) |
| 150 | DEF_BENCH(return new ZerosBench(300, 0, 0, 1)) |
| 151 | DEF_BENCH(return new ZerosBench(300, 0, 1, 0)) |
| 152 | DEF_BENCH(return new ZerosBench(300, 0, 1, 1)) |
| 153 | DEF_BENCH(return new ZerosBench(300, 1, 0, 0)) |
| 154 | DEF_BENCH(return new ZerosBench(300, 1, 0, 1)) |
| 155 | DEF_BENCH(return new ZerosBench(300, 1, 1, 0)) |
| 156 | DEF_BENCH(return new ZerosBench(300, 1, 1, 1)) |
| 157 | |
| 158 | DEF_BENCH(return new ZerosBench(4, 0, 0, 0)) |
| 159 | DEF_BENCH(return new ZerosBench(4, 0, 0, 1)) |
| 160 | DEF_BENCH(return new ZerosBench(4, 0, 1, 0)) |
| 161 | DEF_BENCH(return new ZerosBench(4, 0, 1, 1)) |
| 162 | DEF_BENCH(return new ZerosBench(4, 1, 0, 0)) |
| 163 | DEF_BENCH(return new ZerosBench(4, 1, 0, 1)) |
| 164 | DEF_BENCH(return new ZerosBench(4, 1, 1, 0)) |
| 165 | DEF_BENCH(return new ZerosBench(4, 1, 1, 1)) |