1 /* 2 * Copyright 2013 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 "SkResourceCache.h" 10 11 namespace { 12 static void* gGlobalAddress; 13 class TestKey : public SkResourceCache::Key { 14 public: 15 intptr_t fValue; 16 TestKey(intptr_t value)17 TestKey(intptr_t value) : fValue(value) { 18 this->init(&gGlobalAddress, 0, sizeof(fValue)); 19 } 20 }; 21 struct TestRec : public SkResourceCache::Rec { 22 TestKey fKey; 23 intptr_t fValue; 24 TestRec__anonb5751c1f0111::TestRec25 TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {} 26 getKey__anonb5751c1f0111::TestRec27 const Key& getKey() const override { return fKey; } bytesUsed__anonb5751c1f0111::TestRec28 size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); } 29 Visitor__anonb5751c1f0111::TestRec30 static bool Visitor(const SkResourceCache::Rec&, void*) { 31 return true; 32 } 33 }; 34 } 35 36 class ImageCacheBench : public Benchmark { 37 SkResourceCache fCache; 38 39 enum { 40 CACHE_COUNT = 500 41 }; 42 public: ImageCacheBench()43 ImageCacheBench() : fCache(CACHE_COUNT * 100) {} 44 populateCache()45 void populateCache() { 46 for (int i = 0; i < CACHE_COUNT; ++i) { 47 fCache.add(SkNEW_ARGS(TestRec, (TestKey(i), i))); 48 } 49 } 50 51 protected: onGetName()52 const char* onGetName() override { 53 return "imagecache"; 54 } 55 onDraw(const int loops,SkCanvas *)56 void onDraw(const int loops, SkCanvas*) override { 57 if (fCache.getTotalBytesUsed() == 0) { 58 this->populateCache(); 59 } 60 61 TestKey key(-1); 62 // search for a miss (-1) 63 for (int i = 0; i < loops; ++i) { 64 SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, NULL); 65 SkASSERT(!found); 66 } 67 } 68 69 private: 70 typedef Benchmark INHERITED; 71 }; 72 73 /////////////////////////////////////////////////////////////////////////////// 74 75 DEF_BENCH( return new ImageCacheBench(); ) 76