blob: b8f40c87fc08d7e56fbe70d3385e08e17fa8c1c3 [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:
reed04617132014-08-21 09:46:49 -070015 intptr_t fValue;
16
fmalita171e5b72014-10-22 11:20:40 -070017 TestKey(intptr_t value) : fValue(value) {
18 this->init(&gGlobalAddress, sizeof(fValue));
reed04617132014-08-21 09:46:49 -070019 }
20};
reed011f39a2014-08-28 13:35:23 -070021struct TestRec : public SkResourceCache::Rec {
reed680fb9e2014-08-26 09:08:04 -070022 TestKey fKey;
23 intptr_t fValue;
24
25 TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {}
26
27 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
28 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue); }
reedc90e0142014-09-15 11:39:44 -070029
30 static bool Visitor(const SkResourceCache::Rec&, void*) {
31 return true;
32 }
reed680fb9e2014-08-26 09:08:04 -070033};
reed04617132014-08-21 09:46:49 -070034}
35
tfarinaf168b862014-06-19 12:32:29 -070036class ImageCacheBench : public Benchmark {
reed011f39a2014-08-28 13:35:23 -070037 SkResourceCache fCache;
reed@google.com602a1d72013-07-23 19:13:54 +000038
39 enum {
reed@google.com602a1d72013-07-23 19:13:54 +000040 CACHE_COUNT = 500
41 };
42public:
reed011f39a2014-08-28 13:35:23 -070043 ImageCacheBench() : fCache(CACHE_COUNT * 100) {}
reed@google.com602a1d72013-07-23 19:13:54 +000044
45 void populateCache() {
reed@google.com602a1d72013-07-23 19:13:54 +000046 for (int i = 0; i < CACHE_COUNT; ++i) {
reedc90e0142014-09-15 11:39:44 -070047 fCache.add(SkNEW_ARGS(TestRec, (TestKey(i), i)));
reed@google.com602a1d72013-07-23 19:13:54 +000048 }
49 }
50
51protected:
52 virtual const char* onGetName() SK_OVERRIDE {
53 return "imagecache";
54 }
55
commit-bot@chromium.org33614712013-12-03 18:17:16 +000056 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
halcanary805ef152014-07-17 06:58:01 -070057 if (fCache.getTotalBytesUsed() == 0) {
reed@google.com602a1d72013-07-23 19:13:54 +000058 this->populateCache();
59 }
60
reed04617132014-08-21 09:46:49 -070061 TestKey key(-1);
reed680fb9e2014-08-26 09:08:04 -070062 // search for a miss (-1)
commit-bot@chromium.org33614712013-12-03 18:17:16 +000063 for (int i = 0; i < loops; ++i) {
reedc90e0142014-09-15 11:39:44 -070064 SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, NULL);
65 SkASSERT(!found);
reed@google.com602a1d72013-07-23 19:13:54 +000066 }
67 }
68
69private:
tfarinaf168b862014-06-19 12:32:29 -070070 typedef Benchmark INHERITED;
reed@google.com602a1d72013-07-23 19:13:54 +000071};
72
73///////////////////////////////////////////////////////////////////////////////
74
mtklein@google.com410e6e82013-09-13 19:52:27 +000075DEF_BENCH( return new ImageCacheBench(); )