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 */ 7 #include "Benchmark.h" 8 #include "SkMutex.h" 9 #include "SkSharedMutex.h" 10 #include "SkSpinlock.h" 11 #include "SkString.h" 12 13 template <typename Mutex> 14 class MutexBench : public Benchmark { 15 public: 16 MutexBench(SkString benchPrefix) : fBenchName(benchPrefix += "UncontendedBenchmark") { } 17 bool isSuitableFor(Backend backend) override { 18 return backend == kNonRendering_Backend; 19 } 20 21 protected: 22 const char* onGetName() override { 23 return fBenchName.c_str(); 24 } 25 26 void onDraw(int loops, SkCanvas*) override { 27 for (int i = 0; i < loops; i++) { 28 fMu.acquire(); 29 fMu.release(); 30 } 31 } 32 33 private: 34 typedef Benchmark INHERITED; 35 SkString fBenchName; 36 Mutex fMu; 37 }; 38 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 50 void onDraw(int loops, SkCanvas*) override { 51 for (int i = 0; i < loops; i++) { 52 fMu.acquireShared(); 53 fMu.releaseShared(); 54 } 55 } 56 57 private: 58 typedef Benchmark INHERITED; 59 SkSharedMutex fMu; 60 }; 61 62 /////////////////////////////////////////////////////////////////////////////// 63 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")); ) 67 DEF_BENCH( return new SharedBench; ) 68 69