tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 8 | #include "SkMutex.h" |
herb | bcfd511 | 2015-09-18 09:24:18 -0700 | [diff] [blame] | 9 | #include "SkSharedMutex.h" |
| 10 | #include "SkSpinlock.h" |
| 11 | #include "SkString.h" |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 12 | |
herb | bcfd511 | 2015-09-18 09:24:18 -0700 | [diff] [blame] | 13 | template <typename Mutex> |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 14 | class MutexBench : public Benchmark { |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 15 | public: |
herb | bcfd511 | 2015-09-18 09:24:18 -0700 | [diff] [blame] | 16 | MutexBench(SkString benchPrefix) : fBenchName(benchPrefix += "UncontendedBenchmark") { } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 17 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 18 | return backend == kNonRendering_Backend; |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 19 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 20 | |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 21 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 22 | const char* onGetName() override { |
herb | bcfd511 | 2015-09-18 09:24:18 -0700 | [diff] [blame] | 23 | return fBenchName.c_str(); |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 24 | } |
| 25 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 26 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 27 | for (int i = 0; i < loops; i++) { |
mtklein | a16e8bc | 2015-09-22 10:38:48 -0700 | [diff] [blame] | 28 | fMu.acquire(); |
| 29 | fMu.release(); |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 34 | typedef Benchmark INHERITED; |
herb | bcfd511 | 2015-09-18 09:24:18 -0700 | [diff] [blame] | 35 | SkString fBenchName; |
mtklein | a16e8bc | 2015-09-22 10:38:48 -0700 | [diff] [blame] | 36 | Mutex fMu; |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
herb | d32087a | 2015-09-18 10:50:35 -0700 | [diff] [blame] | 39 | class SharedBench : public Benchmark { |
| 40 | public: |
| 41 | bool isSuitableFor(Backend backend) override { |
| 42 | return backend == kNonRendering_Backend; |
| 43 | } |
| 44 | |
| 45 | protected: |
| 46 | const char* onGetName() override { |
| 47 | return "SkSharedMutexSharedUncontendedBenchmark"; |
| 48 | } |
| 49 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 50 | void onDraw(int loops, SkCanvas*) override { |
herb | d32087a | 2015-09-18 10:50:35 -0700 | [diff] [blame] | 51 | for (int i = 0; i < loops; i++) { |
mtklein | a16e8bc | 2015-09-22 10:38:48 -0700 | [diff] [blame] | 52 | fMu.acquireShared(); |
| 53 | fMu.releaseShared(); |
herb | d32087a | 2015-09-18 10:50:35 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | typedef Benchmark INHERITED; |
mtklein | a16e8bc | 2015-09-22 10:38:48 -0700 | [diff] [blame] | 59 | SkSharedMutex fMu; |
herb | d32087a | 2015-09-18 10:50:35 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
tomhudson@google.com | 410e9dc | 2011-11-14 17:30:08 +0000 | [diff] [blame] | 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | |
herb | bcfd511 | 2015-09-18 09:24:18 -0700 | [diff] [blame] | 64 | DEF_BENCH( return new MutexBench<SkSharedMutex>(SkString("SkSharedMutex")); ) |
| 65 | DEF_BENCH( return new MutexBench<SkMutex>(SkString("SkMutex")); ) |
| 66 | DEF_BENCH( return new MutexBench<SkSpinlock>(SkString("SkSpinlock")); ) |
herb | d32087a | 2015-09-18 10:50:35 -0700 | [diff] [blame] | 67 | DEF_BENCH( return new SharedBench; ) |
| 68 | |