blob: 0f8fdf27089916b0df25d0aa49a0f60078b3fd39 [file] [log] [blame]
reed@google.com602a1d72013-07-23 19:13:54 +00001/*
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed011f39a2014-08-28 13:35:23 -07009#include "SkResourceCache.h"
reed@google.com602a1d72013-07-23 19:13:54 +000010
reed04617132014-08-21 09:46:49 -070011namespace {
12static void* gGlobalAddress;
reed011f39a2014-08-28 13:35:23 -070013class TestKey : public SkResourceCache::Key {
reed04617132014-08-21 09:46:49 -070014public:
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};
reed011f39a2014-08-28 13:35:23 -070022struct TestRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -070023 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); }
reeddee6a8e2014-09-15 06:44:47 -070030
31 static bool Visitor(const SkResourceCache::Rec&, void*) {
32 return true;
33 }
reed680fb9e2014-08-26 09:08:04 -070034};
reed04617132014-08-21 09:46:49 -070035}
36
tfarinaf168b862014-06-19 12:32:29 -070037class ImageCacheBench : public Benchmark {
reed011f39a2014-08-28 13:35:23 -070038 SkResourceCache fCache;
reed@google.com602a1d72013-07-23 19:13:54 +000039
40 enum {
reed@google.com602a1d72013-07-23 19:13:54 +000041 CACHE_COUNT = 500
42 };
43public:
reed011f39a2014-08-28 13:35:23 -070044 ImageCacheBench() : fCache(CACHE_COUNT * 100) {}
reed@google.com602a1d72013-07-23 19:13:54 +000045
46 void populateCache() {
reed@google.com602a1d72013-07-23 19:13:54 +000047 for (int i = 0; i < CACHE_COUNT; ++i) {
reeddee6a8e2014-09-15 06:44:47 -070048 fCache.add(SkNEW_ARGS(TestRec, (TestKey(i), i)));
reed@google.com602a1d72013-07-23 19:13:54 +000049 }
50 }
51
52protected:
53 virtual const char* onGetName() SK_OVERRIDE {
54 return "imagecache";
55 }
56
commit-bot@chromium.org33614712013-12-03 18:17:16 +000057 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
halcanary805ef152014-07-17 06:58:01 -070058 if (fCache.getTotalBytesUsed() == 0) {
reed@google.com602a1d72013-07-23 19:13:54 +000059 this->populateCache();
60 }
61
reed04617132014-08-21 09:46:49 -070062 TestKey key(-1);
reed680fb9e2014-08-26 09:08:04 -070063 // search for a miss (-1)
commit-bot@chromium.org33614712013-12-03 18:17:16 +000064 for (int i = 0; i < loops; ++i) {
reeddee6a8e2014-09-15 06:44:47 -070065 SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, NULL);
66 SkASSERT(!found);
reed@google.com602a1d72013-07-23 19:13:54 +000067 }
68 }
69
70private:
tfarinaf168b862014-06-19 12:32:29 -070071 typedef Benchmark INHERITED;
reed@google.com602a1d72013-07-23 19:13:54 +000072};
73
74///////////////////////////////////////////////////////////////////////////////
75
mtklein@google.com410e6e82013-09-13 19:52:27 +000076DEF_BENCH( return new ImageCacheBench(); )