blob: d1d014d9398c63d96271fc61e4e64c83dcfbff39 [file] [log] [blame]
tomhudson@google.com410e9dc2011-11-14 17:30:08 +00001/*
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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
mtklein1b249332015-07-07 12:21:21 -07008#include "SkMutex.h"
Tom Hudson2880df22015-10-29 09:55:42 -04009#include "SkSharedMutex.h"
10#include "SkSpinlock.h"
11#include "SkString.h"
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000012
Tom Hudson2880df22015-10-29 09:55:42 -040013template <typename Mutex>
tfarinaf168b862014-06-19 12:32:29 -070014class MutexBench : public Benchmark {
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000015public:
Tom Hudson2880df22015-10-29 09:55:42 -040016 MutexBench(SkString benchPrefix) : fBenchName(benchPrefix += "UncontendedBenchmark") { }
17 bool isSuitableFor(Backend backend) override {
18 return backend == kNonRendering_Backend;
19 }
20
21protected:
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
33private:
34 typedef Benchmark INHERITED;
35 SkString fBenchName;
36 Mutex fMu;
37};
38
39class SharedBench : public Benchmark {
40public:
mtklein36352bf2015-03-25 18:17:31 -070041 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000042 return backend == kNonRendering_Backend;
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000043 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000044
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000045protected:
mtkleinf0599002015-07-13 06:18:39 -070046 const char* onGetName() override {
Tom Hudson2880df22015-10-29 09:55:42 -040047 return "SkSharedMutexSharedUncontendedBenchmark";
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000048 }
49
Tom Hudson2880df22015-10-29 09:55:42 -040050 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000051 for (int i = 0; i < loops; i++) {
Tom Hudson2880df22015-10-29 09:55:42 -040052 fMu.acquireShared();
53 fMu.releaseShared();
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000054 }
55 }
56
57private:
tfarinaf168b862014-06-19 12:32:29 -070058 typedef Benchmark INHERITED;
Tom Hudson2880df22015-10-29 09:55:42 -040059 SkSharedMutex fMu;
herbd32087a2015-09-18 10:50:35 -070060};
61
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000062///////////////////////////////////////////////////////////////////////////////
63
Tom Hudson2880df22015-10-29 09:55:42 -040064DEF_BENCH( return new MutexBench<SkSharedMutex>(SkString("SkSharedMutex")); )
65DEF_BENCH( return new MutexBench<SkMutex>(SkString("SkMutex")); )
66DEF_BENCH( return new MutexBench<SkSpinlock>(SkString("SkSpinlock")); )
67DEF_BENCH( return new SharedBench; )
68