blob: d8d60526e81f344d35d814cc2507e5ee6ab526d2 [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;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000018
reed@google.comebd24962012-05-17 14:28:11 +000019 enum {
20 N = SkBENCHLOOP(1000)
21 };
22public:
23 ChunkAllocBench(void* param, size_t minSize) : INHERITED(param) {
24 fMinSize = minSize;
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +000025 fName.printf("chunkalloc_" SK_SIZE_T_SPECIFIER, minSize);
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
reed@google.comebd24962012-05-17 14:28:11 +000033 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 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@google.comebd24962012-05-17 14:28:11 +000052private:
53 typedef SkBenchmark INHERITED;
54};
55
56static SkBenchmark* F0(void* p) { return new ChunkAllocBench(p, 64); }
57static SkBenchmark* F1(void* p) { return new ChunkAllocBench(p, 8*1024); }
58
59static BenchRegistry gR0(F0);
60static BenchRegistry gR1(F1);
61