blob: de2daa9376d4beb247a6acc8dcec399e4b41382b [file] [log] [blame]
junov@chromium.orgef760602012-06-27 20:03:16 +00001/*
2 * Copyright 2012 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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "bench/Benchmark.h"
8#include "include/core/SkCanvas.h"
9#include "include/private/SkChecksum.h"
10#include "include/private/SkTemplates.h"
11#include "include/utils/SkRandom.h"
12#include "src/core/SkMD5.h"
13#include "src/core/SkOpts.h"
bungeman@google.com29dea742013-01-31 20:36:30 +000014
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000015enum ChecksumType {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000016 kMD5_ChecksumType,
mtklein4e976072016-08-08 09:06:27 -070017 kHash_ChecksumType,
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000018};
junov@chromium.orgef760602012-06-27 20:03:16 +000019
tfarinaf168b862014-06-19 12:32:29 -070020class ComputeChecksumBench : public Benchmark {
reed@google.comfc8581b2012-07-09 17:40:48 +000021 enum {
reed@google.com142e1fe2012-07-09 17:44:44 +000022 U32COUNT = 256,
23 SIZE = U32COUNT * 4,
reed@google.comfc8581b2012-07-09 17:40:48 +000024 };
reed@google.com142e1fe2012-07-09 17:44:44 +000025 uint32_t fData[U32COUNT];
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000026 ChecksumType fType;
reed@google.com142e1fe2012-07-09 17:44:44 +000027
28public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000029 ComputeChecksumBench(ChecksumType type) : fType(type) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000030 SkRandom rand;
reed@google.com142e1fe2012-07-09 17:44:44 +000031 for (int i = 0; i < U32COUNT; ++i) {
32 fData[i] = rand.nextU();
33 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000034 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000037 return backend == kNonRendering_Backend;
reed@google.com142e1fe2012-07-09 17:44:44 +000038 }
39
junov@chromium.orgef760602012-06-27 20:03:16 +000040protected:
mtkleinf0599002015-07-13 06:18:39 -070041 const char* onGetName() override {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000042 switch (fType) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000043 case kMD5_ChecksumType: return "compute_md5";
mtklein4e976072016-08-08 09:06:27 -070044 case kHash_ChecksumType: return "compute_hash";
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000045
Brian Salomon74c19de2019-08-09 16:05:58 -040046 default: SK_ABORT("Invalid Type");
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000047 }
junov@chromium.orgef760602012-06-27 20:03:16 +000048 }
49
mtkleina1ebeb22015-10-01 09:43:39 -070050 void onDraw(int loops, SkCanvas*) override {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000051 switch (fType) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000052 case kMD5_ChecksumType: {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000053 for (int i = 0; i < loops; i++) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000054 SkMD5 md5;
halcanary1e903042016-04-25 10:29:36 -070055 md5.write(fData, sizeof(fData));
Hal Canary0f2f5222019-04-03 10:13:45 -040056 (void)md5.finish();
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000057 }
58 } break;
mtklein4e976072016-08-08 09:06:27 -070059 case kHash_ChecksumType: {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000060 for (int i = 0; i < loops; i++) {
mtklein4e976072016-08-08 09:06:27 -070061 volatile uint32_t result = SkOpts::hash(fData, sizeof(fData));
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000062 sk_ignore_unused_variable(result);
63 }
64 }break;
reed@google.com142e1fe2012-07-09 17:44:44 +000065 }
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000066
junov@chromium.orgef760602012-06-27 20:03:16 +000067 }
68
junov@chromium.orgef760602012-06-27 20:03:16 +000069private:
John Stiles7571f9e2020-09-02 22:42:33 -040070 using INHERITED = Benchmark;
junov@chromium.orgef760602012-06-27 20:03:16 +000071};
72
junov@chromium.orgef760602012-06-27 20:03:16 +000073///////////////////////////////////////////////////////////////////////////////
74
mtklein@google.com410e6e82013-09-13 19:52:27 +000075DEF_BENCH( return new ComputeChecksumBench(kMD5_ChecksumType); )
mtklein4e976072016-08-08 09:06:27 -070076DEF_BENCH( return new ComputeChecksumBench(kHash_ChecksumType); )