blob: 8226ddd322cb451164a5e91fef76545114a2dfa1 [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"
bungeman@google.com7de18e52013-02-04 15:58:08 +000014#include "SkTemplates.h"
bungeman@google.com29dea742013-01-31 20:36:30 +000015
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000016enum ChecksumType {
17 kChecksum_ChecksumType,
18 kMD5_ChecksumType,
19 kSHA1_ChecksumType,
20 kCityHash32,
21 kCityHash64
22};
junov@chromium.orgef760602012-06-27 20:03:16 +000023
24class ComputeChecksumBench : public SkBenchmark {
reed@google.comfc8581b2012-07-09 17:40:48 +000025 enum {
reed@google.com142e1fe2012-07-09 17:44:44 +000026 U32COUNT = 256,
27 SIZE = U32COUNT * 4,
reed@google.comfc8581b2012-07-09 17:40:48 +000028 N = SkBENCHLOOP(100000),
29 };
reed@google.com142e1fe2012-07-09 17:44:44 +000030 uint32_t fData[U32COUNT];
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000031 ChecksumType fType;
reed@google.com142e1fe2012-07-09 17:44:44 +000032
33public:
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000034 ComputeChecksumBench(void* param, ChecksumType type) : INHERITED(param), fType(type) {
reed@google.com142e1fe2012-07-09 17:44:44 +000035 SkRandom rand;
36 for (int i = 0; i < U32COUNT; ++i) {
37 fData[i] = rand.nextU();
38 }
robertphillips@google.com433ce5e2012-09-17 10:49:30 +000039 fIsRendering = false;
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";
48 case kCityHash32: return "compute_cityhash32";
49 case kCityHash64: return "compute_cityhash64";
50 default: SK_CRASH(); return "";
51 }
junov@chromium.orgef760602012-06-27 20:03:16 +000052 }
53
54 virtual void onDraw(SkCanvas* canvas) {
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000055 switch (fType) {
56 case kChecksum_ChecksumType: {
57 for (int i = 0; i < N; i++) {
58 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: {
63 for (int i = 0; i < N; i++) {
64 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: {
71 for (int i = 0; i < N; i++) {
72 SkSHA1 sha1;
73 sha1.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
74 SkSHA1::Digest digest;
75 sha1.finish(digest);
76 }
77 } break;
78 case kCityHash32: {
79 for (int i = 0; i < N; i++) {
80 volatile uint32_t result = SkCityHash::Compute32(reinterpret_cast<char*>(fData), sizeof(fData));
bungeman@google.com7de18e52013-02-04 15:58:08 +000081 sk_ignore_unused_variable(result);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000082 }
83 } break;
84 case kCityHash64: {
85 for (int i = 0; i < N; i++) {
86 volatile uint64_t result = SkCityHash::Compute64(reinterpret_cast<char*>(fData), sizeof(fData));
bungeman@google.com7de18e52013-02-04 15:58:08 +000087 sk_ignore_unused_variable(result);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000088 }
89 } break;
reed@google.com142e1fe2012-07-09 17:44:44 +000090 }
bungeman@google.comcfcb1be2013-01-31 19:47:48 +000091
junov@chromium.orgef760602012-06-27 20:03:16 +000092 }
93
junov@chromium.orgef760602012-06-27 20:03:16 +000094private:
95 typedef SkBenchmark INHERITED;
96};
97
junov@chromium.orgef760602012-06-27 20:03:16 +000098///////////////////////////////////////////////////////////////////////////////
99
bungeman@google.comcfcb1be2013-01-31 19:47:48 +0000100static SkBenchmark* Fact0(void* p) { return new ComputeChecksumBench(p, kChecksum_ChecksumType); }
101static SkBenchmark* Fact1(void* p) { return new ComputeChecksumBench(p, kMD5_ChecksumType); }
102static SkBenchmark* Fact2(void* p) { return new ComputeChecksumBench(p, kSHA1_ChecksumType); }
103static SkBenchmark* Fact3(void* p) { return new ComputeChecksumBench(p, kCityHash32); }
104static SkBenchmark* Fact4(void* p) { return new ComputeChecksumBench(p, kCityHash64); }
junov@chromium.orgef760602012-06-27 20:03:16 +0000105
106static BenchRegistry gReg0(Fact0);
bungeman@google.comcfcb1be2013-01-31 19:47:48 +0000107static BenchRegistry gReg1(Fact1);
108static BenchRegistry gReg2(Fact2);
109static BenchRegistry gReg3(Fact3);
110static BenchRegistry gReg4(Fact4);