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