• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Benchmark.h"
9 #include "SkCanvas.h"
10 #include "SkChunkAlloc.h"
11 #include "SkPaint.h"
12 #include "SkRandom.h"
13 #include "SkString.h"
14 
15 class ChunkAllocBench : public Benchmark {
16     SkString    fName;
17     size_t      fMinSize;
18 public:
ChunkAllocBench(size_t minSize)19     ChunkAllocBench(size_t minSize)  {
20         fMinSize = minSize;
21         fName.printf("chunkalloc_" SK_SIZE_T_SPECIFIER, minSize);
22     }
23 
isSuitableFor(Backend backend)24     bool isSuitableFor(Backend backend) override {
25         return backend == kNonRendering_Backend;
26     }
27 
28 protected:
onGetName()29     const char* onGetName() override {
30         return fName.c_str();
31     }
32 
onDraw(const int loops,SkCanvas *)33     void onDraw(const int loops, SkCanvas*) 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 < loops; ++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 Benchmark INHERITED;
54 };
55 
56 DEF_BENCH( return new ChunkAllocBench(64); )
57 DEF_BENCH( return new ChunkAllocBench(8*1024); )
58 
59