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"
herbbcfd5112015-09-18 09:24:18 -07009#include "SkSharedMutex.h"
10#include "SkSpinlock.h"
11#include "SkString.h"
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000012
herbbcfd5112015-09-18 09:24:18 -070013template <typename Mutex>
tfarinaf168b862014-06-19 12:32:29 -070014class MutexBench : public Benchmark {
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000015public:
herbbcfd5112015-09-18 09:24:18 -070016 MutexBench(SkString benchPrefix) : fBenchName(benchPrefix += "UncontendedBenchmark") { }
mtklein36352bf2015-03-25 18:17:31 -070017 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000018 return backend == kNonRendering_Backend;
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000019 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000020
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000021protected:
mtkleinf0599002015-07-13 06:18:39 -070022 const char* onGetName() override {
herbbcfd5112015-09-18 09:24:18 -070023 return fBenchName.c_str();
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000024 }
25
mtkleina1ebeb22015-10-01 09:43:39 -070026 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000027 for (int i = 0; i < loops; i++) {
mtkleina16e8bc2015-09-22 10:38:48 -070028 fMu.acquire();
29 fMu.release();
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000030 }
31 }
32
33private:
tfarinaf168b862014-06-19 12:32:29 -070034 typedef Benchmark INHERITED;
herbbcfd5112015-09-18 09:24:18 -070035 SkString fBenchName;
mtkleina16e8bc2015-09-22 10:38:48 -070036 Mutex fMu;
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000037};
38
herbd32087a2015-09-18 10:50:35 -070039class SharedBench : public Benchmark {
40public:
41 bool isSuitableFor(Backend backend) override {
42 return backend == kNonRendering_Backend;
43 }
44
45protected:
46 const char* onGetName() override {
47 return "SkSharedMutexSharedUncontendedBenchmark";
48 }
49
mtkleina1ebeb22015-10-01 09:43:39 -070050 void onDraw(int loops, SkCanvas*) override {
herbd32087a2015-09-18 10:50:35 -070051 for (int i = 0; i < loops; i++) {
mtkleina16e8bc2015-09-22 10:38:48 -070052 fMu.acquireShared();
53 fMu.releaseShared();
herbd32087a2015-09-18 10:50:35 -070054 }
55 }
56
57private:
58 typedef Benchmark INHERITED;
mtkleina16e8bc2015-09-22 10:38:48 -070059 SkSharedMutex fMu;
herbd32087a2015-09-18 10:50:35 -070060};
61
tomhudson@google.com410e9dc2011-11-14 17:30:08 +000062///////////////////////////////////////////////////////////////////////////////
63
herbbcfd5112015-09-18 09:24:18 -070064DEF_BENCH( return new MutexBench<SkSharedMutex>(SkString("SkSharedMutex")); )
65DEF_BENCH( return new MutexBench<SkMutex>(SkString("SkMutex")); )
66DEF_BENCH( return new MutexBench<SkSpinlock>(SkString("SkSpinlock")); )
herbd32087a2015-09-18 10:50:35 -070067DEF_BENCH( return new SharedBench; )
68