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