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