reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 9 | #include "SkResourceCache.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 10 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 11 | namespace { |
| 12 | static void* gGlobalAddress; |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 13 | class TestKey : public SkResourceCache::Key { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 14 | public: |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 15 | intptr_t fValue; |
| 16 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 17 | TestKey(intptr_t value) : fValue(value) { |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 18 | this->init(&gGlobalAddress, 0, sizeof(fValue)); |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 19 | } |
| 20 | }; |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 21 | struct TestRec : public SkResourceCache::Rec { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 22 | TestKey fKey; |
| 23 | intptr_t fValue; |
| 24 | |
| 25 | TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {} |
| 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | const Key& getKey() const override { return fKey; } |
| 28 | size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); } |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 29 | const char* getCategory() const override { return "imagecachebench-test"; } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; } |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 31 | |
| 32 | static bool Visitor(const SkResourceCache::Rec&, void*) { |
| 33 | return true; |
| 34 | } |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 35 | }; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 36 | } |
| 37 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 38 | class ImageCacheBench : public Benchmark { |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 39 | SkResourceCache fCache; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 40 | |
| 41 | enum { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 42 | CACHE_COUNT = 500 |
| 43 | }; |
| 44 | public: |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 45 | ImageCacheBench() : fCache(CACHE_COUNT * 100) {} |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 46 | |
| 47 | void populateCache() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 48 | for (int i = 0; i < CACHE_COUNT; ++i) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 49 | fCache.add(new TestRec(TestKey(i), i)); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | const char* onGetName() override { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 55 | return "imagecache"; |
| 56 | } |
| 57 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 58 | void onDraw(int loops, SkCanvas*) override { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 59 | if (fCache.getTotalBytesUsed() == 0) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 60 | this->populateCache(); |
| 61 | } |
| 62 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 63 | TestKey key(-1); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 64 | // search for a miss (-1) |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 65 | for (int i = 0; i < loops; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, nullptr); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 67 | SkASSERT(!found); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 72 | typedef Benchmark INHERITED; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 77 | DEF_BENCH( return new ImageCacheBench(); ) |