blob: 1d577d65f12ac30911c14825789b19f02fd71079 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "src/core/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) {
reed7eeba252015-02-24 13:54:23 -080018 this->init(&gGlobalAddress, 0, 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
mtklein36352bf2015-03-25 18:17:31 -070027 const Key& getKey() const override { return fKey; }
28 size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
reed216b6432015-08-19 12:25:40 -070029 const char* getCategory() const override { return "imagecachebench-test"; }
halcanary96fcdcc2015-08-27 07:41:13 -070030 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
reedc90e0142014-09-15 11:39:44 -070031
32 static bool Visitor(const SkResourceCache::Rec&, void*) {
33 return true;
34 }
reed680fb9e2014-08-26 09:08:04 -070035};
reed04617132014-08-21 09:46:49 -070036}
37
tfarinaf168b862014-06-19 12:32:29 -070038class ImageCacheBench : public Benchmark {
reed011f39a2014-08-28 13:35:23 -070039 SkResourceCache fCache;
reed@google.com602a1d72013-07-23 19:13:54 +000040
41 enum {
reed@google.com602a1d72013-07-23 19:13:54 +000042 CACHE_COUNT = 500
43 };
44public:
reed011f39a2014-08-28 13:35:23 -070045 ImageCacheBench() : fCache(CACHE_COUNT * 100) {}
reed@google.com602a1d72013-07-23 19:13:54 +000046
47 void populateCache() {
reed@google.com602a1d72013-07-23 19:13:54 +000048 for (int i = 0; i < CACHE_COUNT; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070049 fCache.add(new TestRec(TestKey(i), i));
reed@google.com602a1d72013-07-23 19:13:54 +000050 }
51 }
52
53protected:
mtklein36352bf2015-03-25 18:17:31 -070054 const char* onGetName() override {
reed@google.com602a1d72013-07-23 19:13:54 +000055 return "imagecache";
56 }
57
mtkleina1ebeb22015-10-01 09:43:39 -070058 void onDraw(int loops, SkCanvas*) override {
halcanary805ef152014-07-17 06:58:01 -070059 if (fCache.getTotalBytesUsed() == 0) {
reed@google.com602a1d72013-07-23 19:13:54 +000060 this->populateCache();
61 }
62
reed04617132014-08-21 09:46:49 -070063 TestKey key(-1);
reed680fb9e2014-08-26 09:08:04 -070064 // search for a miss (-1)
commit-bot@chromium.org33614712013-12-03 18:17:16 +000065 for (int i = 0; i < loops; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070066 SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, nullptr);
reedc90e0142014-09-15 11:39:44 -070067 SkASSERT(!found);
reed@google.com602a1d72013-07-23 19:13:54 +000068 }
69 }
70
71private:
tfarinaf168b862014-06-19 12:32:29 -070072 typedef Benchmark INHERITED;
reed@google.com602a1d72013-07-23 19:13:54 +000073};
74
75///////////////////////////////////////////////////////////////////////////////
76
mtklein@google.com410e6e82013-09-13 19:52:27 +000077DEF_BENCH( return new ImageCacheBench(); )