aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/MemoryBench.cpp
blob: ab9c07dd0635f18b3d9f19f1096cc37599688f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBenchmark.h"
#include "SkCanvas.h"
#include "SkPaint.h"
#include "SkRandom.h"
#include "SkChunkAlloc.h"
#include "SkString.h"

class ChunkAllocBench : public SkBenchmark {
    SkString    fName;
    size_t      fMinSize;

    enum {
        N = SkBENCHLOOP(1000)
    };
public:
    ChunkAllocBench(void* param, size_t minSize) : INHERITED(param) {
        fMinSize = minSize;
        fName.printf("chunkalloc_" SK_SIZE_T_SPECIFIER, minSize);
        fIsRendering = false;
    }

protected:
    virtual const char* onGetName() SK_OVERRIDE {
        return fName.c_str();
    }

    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
        size_t inc = fMinSize >> 4;
        SkASSERT(inc > 0);
        size_t total = fMinSize * 64;

        SkChunkAlloc alloc(fMinSize);

        for (int i = 0; i < N; ++i) {
            size_t size = 0;
            int calls = 0;
            while (size < total) {
                alloc.allocThrow(inc);
                size += inc;
                calls += 1;
            }
            alloc.reset();
        }
    }

private:
    typedef SkBenchmark INHERITED;
};

static SkBenchmark* F0(void* p) { return new ChunkAllocBench(p, 64); }
static SkBenchmark* F1(void* p) { return new ChunkAllocBench(p, 8*1024); }

static BenchRegistry gR0(F0);
static BenchRegistry gR1(F1);