blob: b125aa4d1fe3e134ae54e98c94df8647d4b1f3c6 [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 */
7#include "SkBenchmark.h"
8#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
22class ComputeChecksumBench : public SkBenchmark {
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 N = SkBENCHLOOP(100000),
27 };
reed@google.com142e1fe2012-07-09 17:44:44 +000028 uint32_t fData[U32COUNT];
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000029 ChecksumType fType;
reed@google.com142e1fe2012-07-09 17:44:44 +000030
31public:
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000032 ComputeChecksumBench(void* param, ChecksumType type) : INHERITED(param), fType(type) {
reed@google.com142e1fe2012-07-09 17:44:44 +000033 SkRandom rand;
34 for (int i = 0; i < U32COUNT; ++i) {
35 fData[i] = rand.nextU();
36 }
robertphillips@google.com433ce5e2012-09-17 10:49:30 +000037 fIsRendering = false;
reed@google.com142e1fe2012-07-09 17:44:44 +000038 }
39
junov@chromium.orgef760602012-06-27 20:03:16 +000040protected:
41 virtual const char* onGetName() {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000042 switch (fType) {
43 case kChecksum_ChecksumType: return "compute_checksum";
44 case kMD5_ChecksumType: return "compute_md5";
45 case kSHA1_ChecksumType: return "compute_sha1";
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000046 case kMurmur3_ChecksumType: return "compute_murmur3";
47
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000048 default: SK_CRASH(); return "";
49 }
junov@chromium.orgef760602012-06-27 20:03:16 +000050 }
51
sugoi@google.com77472f02013-03-05 18:50:01 +000052 virtual void onDraw(SkCanvas*) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000053 switch (fType) {
54 case kChecksum_ChecksumType: {
55 for (int i = 0; i < N; i++) {
56 volatile uint32_t result = SkChecksum::Compute(fData, sizeof(fData));
bungeman@google.com7de18e52013-02-04 15:58:08 +000057 sk_ignore_unused_variable(result);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000058 }
59 } break;
60 case kMD5_ChecksumType: {
61 for (int i = 0; i < N; i++) {
62 SkMD5 md5;
63 md5.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
64 SkMD5::Digest digest;
65 md5.finish(digest);
66 }
67 } break;
68 case kSHA1_ChecksumType: {
69 for (int i = 0; i < N; i++) {
70 SkSHA1 sha1;
71 sha1.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
72 SkSHA1::Digest digest;
73 sha1.finish(digest);
74 }
75 } break;
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000076 case kMurmur3_ChecksumType: {
77 for (int i = 0; i < N; i++) {
78 volatile uint32_t result = SkChecksum::Murmur3(fData, sizeof(fData));
79 sk_ignore_unused_variable(result);
80 }
81 }break;
reed@google.com142e1fe2012-07-09 17:44:44 +000082 }
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000083
junov@chromium.orgef760602012-06-27 20:03:16 +000084 }
85
junov@chromium.orgef760602012-06-27 20:03:16 +000086private:
87 typedef SkBenchmark INHERITED;
88};
89
junov@chromium.orgef760602012-06-27 20:03:16 +000090///////////////////////////////////////////////////////////////////////////////
91
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000092static SkBenchmark* Fact0(void* p) { return new ComputeChecksumBench(p, kChecksum_ChecksumType); }
93static SkBenchmark* Fact1(void* p) { return new ComputeChecksumBench(p, kMD5_ChecksumType); }
94static SkBenchmark* Fact2(void* p) { return new ComputeChecksumBench(p, kSHA1_ChecksumType); }
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000095static SkBenchmark* Fact3(void* p) { return new ComputeChecksumBench(p, kMurmur3_ChecksumType); }
96
junov@chromium.orgef760602012-06-27 20:03:16 +000097
98static BenchRegistry gReg0(Fact0);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000099static BenchRegistry gReg1(Fact1);
100static BenchRegistry gReg2(Fact2);
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +0000101static BenchRegistry gReg3(Fact3);
102