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