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; |
| 18 | |
| 19 | enum { |
| 20 | N = SkBENCHLOOP(1000) |
| 21 | }; |
| 22 | public: |
| 23 | ChunkAllocBench(void* param, size_t minSize) : INHERITED(param) { |
| 24 | fMinSize = minSize; |
| 25 | fName.printf("chunkalloc_%d", minSize); |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | virtual const char* onGetName() SK_OVERRIDE { |
| 30 | return fName.c_str(); |
| 31 | } |
| 32 | |
| 33 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 34 | size_t inc = fMinSize >> 4; |
| 35 | SkASSERT(inc > 0); |
| 36 | size_t total = fMinSize * 64; |
| 37 | |
| 38 | SkChunkAlloc alloc(fMinSize); |
| 39 | |
| 40 | for (int i = 0; i < N; ++i) { |
| 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 | } |
| 51 | |
| 52 | private: |
| 53 | typedef SkBenchmark INHERITED; |
| 54 | }; |
| 55 | |
| 56 | static SkBenchmark* F0(void* p) { return new ChunkAllocBench(p, 64); } |
| 57 | static SkBenchmark* F1(void* p) { return new ChunkAllocBench(p, 8*1024); } |
| 58 | |
| 59 | static BenchRegistry gR0(F0); |
| 60 | static BenchRegistry gR1(F1); |
| 61 | |