blob: 7d3ec9ff9f450f8d96b98667fe4e362adc5f5499 [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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
junov@chromium.orgef760602012-06-27 20:03:16 +00008#include "SkCanvas.h"
9#include "SkChecksum.h"
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000010#include "SkMD5.h"
reed@google.com142e1fe2012-07-09 17:44:44 +000011#include "SkRandom.h"
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000012#include "SkSHA1.h"
bungeman@google.com7de18e52013-02-04 15:58:08 +000013#include "SkTemplates.h"
bungeman@google.com29dea742013-01-31 20:36:30 +000014
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000015enum ChecksumType {
16 kChecksum_ChecksumType,
17 kMD5_ChecksumType,
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000018 kSHA1_ChecksumType,
19 kMurmur3_ChecksumType,
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000020};
junov@chromium.orgef760602012-06-27 20:03:16 +000021
tfarinaf168b862014-06-19 12:32:29 -070022class ComputeChecksumBench : public Benchmark {
reed@google.comfc8581b2012-07-09 17:40:48 +000023 enum {
reed@google.com142e1fe2012-07-09 17:44:44 +000024 U32COUNT = 256,
25 SIZE = U32COUNT * 4,
reed@google.comfc8581b2012-07-09 17:40:48 +000026 };
reed@google.com142e1fe2012-07-09 17:44:44 +000027 uint32_t fData[U32COUNT];
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000028 ChecksumType fType;
reed@google.com142e1fe2012-07-09 17:44:44 +000029
30public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000031 ComputeChecksumBench(ChecksumType type) : fType(type) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000032 SkRandom rand;
reed@google.com142e1fe2012-07-09 17:44:44 +000033 for (int i = 0; i < U32COUNT; ++i) {
34 fData[i] = rand.nextU();
35 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000036 }
37
38 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
39 return backend == kNonRendering_Backend;
reed@google.com142e1fe2012-07-09 17:44:44 +000040 }
41
junov@chromium.orgef760602012-06-27 20:03:16 +000042protected:
43 virtual const char* onGetName() {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000044 switch (fType) {
45 case kChecksum_ChecksumType: return "compute_checksum";
46 case kMD5_ChecksumType: return "compute_md5";
47 case kSHA1_ChecksumType: return "compute_sha1";
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000048 case kMurmur3_ChecksumType: return "compute_murmur3";
49
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000050 default: SK_CRASH(); return "";
51 }
junov@chromium.orgef760602012-06-27 20:03:16 +000052 }
53
commit-bot@chromium.org33614712013-12-03 18:17:16 +000054 virtual void onDraw(const int loops, SkCanvas*) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000055 switch (fType) {
56 case kChecksum_ChecksumType: {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000057 for (int i = 0; i < loops; i++) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000058 volatile uint32_t result = SkChecksum::Compute(fData, sizeof(fData));
bungeman@google.com7de18e52013-02-04 15:58:08 +000059 sk_ignore_unused_variable(result);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000060 }
61 } break;
62 case kMD5_ChecksumType: {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000063 for (int i = 0; i < loops; i++) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000064 SkMD5 md5;
65 md5.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
66 SkMD5::Digest digest;
67 md5.finish(digest);
68 }
69 } break;
70 case kSHA1_ChecksumType: {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000071 for (int i = 0; i < loops; i++) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000072 SkSHA1 sha1;
73 sha1.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
74 SkSHA1::Digest digest;
75 sha1.finish(digest);
76 }
77 } break;
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000078 case kMurmur3_ChecksumType: {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000079 for (int i = 0; i < loops; i++) {
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000080 volatile uint32_t result = SkChecksum::Murmur3(fData, sizeof(fData));
81 sk_ignore_unused_variable(result);
82 }
83 }break;
reed@google.com142e1fe2012-07-09 17:44:44 +000084 }
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000085
junov@chromium.orgef760602012-06-27 20:03:16 +000086 }
87
junov@chromium.orgef760602012-06-27 20:03:16 +000088private:
tfarinaf168b862014-06-19 12:32:29 -070089 typedef Benchmark INHERITED;
junov@chromium.orgef760602012-06-27 20:03:16 +000090};
91
junov@chromium.orgef760602012-06-27 20:03:16 +000092///////////////////////////////////////////////////////////////////////////////
93
mtklein@google.com410e6e82013-09-13 19:52:27 +000094DEF_BENCH( return new ComputeChecksumBench(kChecksum_ChecksumType); )
95DEF_BENCH( return new ComputeChecksumBench(kMD5_ChecksumType); )
96DEF_BENCH( return new ComputeChecksumBench(kSHA1_ChecksumType); )
97DEF_BENCH( return new ComputeChecksumBench(kMurmur3_ChecksumType); )