blob: 4158d94ec1afca249f1818508e0f81c67cdd5752 [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 "SkCityHash.h"
11#include "SkMD5.h"
reed@google.com142e1fe2012-07-09 17:44:44 +000012#include "SkRandom.h"
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000013#include "SkSHA1.h"
14
bungeman@google.com29dea742013-01-31 20:36:30 +000015template<typename T> inline void sk_ignore_unused(const T&) { }
16
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000017enum ChecksumType {
18 kChecksum_ChecksumType,
19 kMD5_ChecksumType,
20 kSHA1_ChecksumType,
21 kCityHash32,
22 kCityHash64
23};
junov@chromium.orgef760602012-06-27 20:03:16 +000024
25class ComputeChecksumBench : public SkBenchmark {
reed@google.comfc8581b2012-07-09 17:40:48 +000026 enum {
reed@google.com142e1fe2012-07-09 17:44:44 +000027 U32COUNT = 256,
28 SIZE = U32COUNT * 4,
reed@google.comfc8581b2012-07-09 17:40:48 +000029 N = SkBENCHLOOP(100000),
30 };
reed@google.com142e1fe2012-07-09 17:44:44 +000031 uint32_t fData[U32COUNT];
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000032 ChecksumType fType;
reed@google.com142e1fe2012-07-09 17:44:44 +000033
34public:
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000035 ComputeChecksumBench(void* param, ChecksumType type) : INHERITED(param), fType(type) {
reed@google.com142e1fe2012-07-09 17:44:44 +000036 SkRandom rand;
37 for (int i = 0; i < U32COUNT; ++i) {
38 fData[i] = rand.nextU();
39 }
robertphillips@google.com433ce5e2012-09-17 10:49:30 +000040 fIsRendering = false;
reed@google.com142e1fe2012-07-09 17:44:44 +000041 }
42
junov@chromium.orgef760602012-06-27 20:03:16 +000043protected:
44 virtual const char* onGetName() {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000045 switch (fType) {
46 case kChecksum_ChecksumType: return "compute_checksum";
47 case kMD5_ChecksumType: return "compute_md5";
48 case kSHA1_ChecksumType: return "compute_sha1";
49 case kCityHash32: return "compute_cityhash32";
50 case kCityHash64: return "compute_cityhash64";
51 default: SK_CRASH(); return "";
52 }
junov@chromium.orgef760602012-06-27 20:03:16 +000053 }
54
55 virtual void onDraw(SkCanvas* canvas) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000056 switch (fType) {
57 case kChecksum_ChecksumType: {
58 for (int i = 0; i < N; i++) {
59 volatile uint32_t result = SkChecksum::Compute(fData, sizeof(fData));
bungeman@google.com29dea742013-01-31 20:36:30 +000060 sk_ignore_unused(result);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000061 }
62 } break;
63 case kMD5_ChecksumType: {
64 for (int i = 0; i < N; i++) {
65 SkMD5 md5;
66 md5.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
67 SkMD5::Digest digest;
68 md5.finish(digest);
bungeman@google.com29dea742013-01-31 20:36:30 +000069 sk_ignore_unused(digest);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000070 }
71 } break;
72 case kSHA1_ChecksumType: {
73 for (int i = 0; i < N; i++) {
74 SkSHA1 sha1;
75 sha1.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
76 SkSHA1::Digest digest;
77 sha1.finish(digest);
bungeman@google.com29dea742013-01-31 20:36:30 +000078 sk_ignore_unused(digest);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000079 }
80 } break;
81 case kCityHash32: {
82 for (int i = 0; i < N; i++) {
83 volatile uint32_t result = SkCityHash::Compute32(reinterpret_cast<char*>(fData), sizeof(fData));
bungeman@google.com29dea742013-01-31 20:36:30 +000084 sk_ignore_unused(result);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000085 }
86 } break;
87 case kCityHash64: {
88 for (int i = 0; i < N; i++) {
89 volatile uint64_t result = SkCityHash::Compute64(reinterpret_cast<char*>(fData), sizeof(fData));
bungeman@google.com29dea742013-01-31 20:36:30 +000090 sk_ignore_unused(result);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000091 }
92 } break;
reed@google.com142e1fe2012-07-09 17:44:44 +000093 }
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000094
junov@chromium.orgef760602012-06-27 20:03:16 +000095 }
96
junov@chromium.orgef760602012-06-27 20:03:16 +000097private:
98 typedef SkBenchmark INHERITED;
99};
100
junov@chromium.orgef760602012-06-27 20:03:16 +0000101///////////////////////////////////////////////////////////////////////////////
102
bungeman@google.comcfcb1be2013-01-31 19:47:48 +0000103static SkBenchmark* Fact0(void* p) { return new ComputeChecksumBench(p, kChecksum_ChecksumType); }
104static SkBenchmark* Fact1(void* p) { return new ComputeChecksumBench(p, kMD5_ChecksumType); }
105static SkBenchmark* Fact2(void* p) { return new ComputeChecksumBench(p, kSHA1_ChecksumType); }
106static SkBenchmark* Fact3(void* p) { return new ComputeChecksumBench(p, kCityHash32); }
107static SkBenchmark* Fact4(void* p) { return new ComputeChecksumBench(p, kCityHash64); }
junov@chromium.orgef760602012-06-27 20:03:16 +0000108
109static BenchRegistry gReg0(Fact0);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +0000110static BenchRegistry gReg1(Fact1);
111static BenchRegistry gReg2(Fact2);
112static BenchRegistry gReg3(Fact3);
113static BenchRegistry gReg4(Fact4);