2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkResourceCache.h"
12 static void* gGlobalAddress;
13 class TestKey : public SkResourceCache::Key {
17 TestKey(intptr_t value) : fValue(value) {
18 this->init(&gGlobalAddress, 0, sizeof(fValue));
21 struct TestRec : public SkResourceCache::Rec {
25 TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {}
27 const Key& getKey() const SK_OVERRIDE { return fKey; }
28 size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue); }
30 static bool Visitor(const SkResourceCache::Rec&, void*) {
36 class ImageCacheBench : public Benchmark {
37 SkResourceCache fCache;
43 ImageCacheBench() : fCache(CACHE_COUNT * 100) {}
45 void populateCache() {
46 for (int i = 0; i < CACHE_COUNT; ++i) {
47 fCache.add(SkNEW_ARGS(TestRec, (TestKey(i), i)));
52 const char* onGetName() SK_OVERRIDE {
56 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
57 if (fCache.getTotalBytesUsed() == 0) {
58 this->populateCache();
62 // search for a miss (-1)
63 for (int i = 0; i < loops; ++i) {
64 SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, NULL);
70 typedef Benchmark INHERITED;
73 ///////////////////////////////////////////////////////////////////////////////
75 DEF_BENCH( return new ImageCacheBench(); )