ImageCacheBench.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include "bench/Benchmark.h"
  8. #include "src/core/SkResourceCache.h"
  9. namespace {
  10. static void* gGlobalAddress;
  11. class TestKey : public SkResourceCache::Key {
  12. public:
  13. intptr_t fValue;
  14. TestKey(intptr_t value) : fValue(value) {
  15. this->init(&gGlobalAddress, 0, sizeof(fValue));
  16. }
  17. };
  18. struct TestRec : public SkResourceCache::Rec {
  19. TestKey fKey;
  20. intptr_t fValue;
  21. TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {}
  22. const Key& getKey() const override { return fKey; }
  23. size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
  24. const char* getCategory() const override { return "imagecachebench-test"; }
  25. SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
  26. static bool Visitor(const SkResourceCache::Rec&, void*) {
  27. return true;
  28. }
  29. };
  30. }
  31. class ImageCacheBench : public Benchmark {
  32. SkResourceCache fCache;
  33. enum {
  34. CACHE_COUNT = 500
  35. };
  36. public:
  37. ImageCacheBench() : fCache(CACHE_COUNT * 100) {}
  38. void populateCache() {
  39. for (int i = 0; i < CACHE_COUNT; ++i) {
  40. fCache.add(new TestRec(TestKey(i), i));
  41. }
  42. }
  43. protected:
  44. const char* onGetName() override {
  45. return "imagecache";
  46. }
  47. void onDraw(int loops, SkCanvas*) override {
  48. if (fCache.getTotalBytesUsed() == 0) {
  49. this->populateCache();
  50. }
  51. TestKey key(-1);
  52. // search for a miss (-1)
  53. for (int i = 0; i < loops; ++i) {
  54. SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, nullptr);
  55. SkASSERT(!found);
  56. }
  57. }
  58. private:
  59. typedef Benchmark INHERITED;
  60. };
  61. ///////////////////////////////////////////////////////////////////////////////
  62. DEF_BENCH( return new ImageCacheBench(); )