1 /*
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 "Benchmark.h"
8 #include "SkCanvas.h"
9 #include "SkChecksum.h"
10 #include "SkOpts.h"
11 #include "SkMD5.h"
12 #include "SkRandom.h"
13 #include "SkTemplates.h"
14 
15 enum ChecksumType {
16     kMD5_ChecksumType,
17     kHash_ChecksumType,
18 };
19 
20 class ComputeChecksumBench : public Benchmark {
21     enum {
22         U32COUNT  = 256,
23         SIZE      = U32COUNT * 4,
24     };
25     uint32_t    fData[U32COUNT];
26     ChecksumType fType;
27 
28 public:
29     ComputeChecksumBench(ChecksumType type) : fType(type) {
30         SkRandom rand;
31         for (int i = 0; i < U32COUNT; ++i) {
32             fData[i] = rand.nextU();
33         }
34     }
35 
36     bool isSuitableFor(Backend backend) override {
37         return backend == kNonRendering_Backend;
38     }
39 
40 protected:
41     const char* onGetName() override {
42         switch (fType) {
43             case kMD5_ChecksumType: return "compute_md5";
44             case kHash_ChecksumType: return "compute_hash";
45 
46             default: SK_ABORT("Invalid Type"); return "";
47         }
48     }
49 
50     void onDraw(int loops, SkCanvas*) override {
51         switch (fType) {
52             case kMD5_ChecksumType: {
53                 for (int i = 0; i < loops; i++) {
54                     SkMD5 md5;
55                     md5.write(fData, sizeof(fData));
56                     SkMD5::Digest digest;
57                     md5.finish(digest);
58                 }
59             } break;
60             case kHash_ChecksumType: {
61                 for (int i = 0; i < loops; i++) {
62                     volatile uint32_t result = SkOpts::hash(fData, sizeof(fData));
63                     sk_ignore_unused_variable(result);
64                 }
65             }break;
66         }
67 
68     }
69 
70 private:
71     typedef Benchmark INHERITED;
72 };
73 
74 ///////////////////////////////////////////////////////////////////////////////
75 
76 DEF_BENCH( return new ComputeChecksumBench(kMD5_ChecksumType); )
77 DEF_BENCH( return new ComputeChecksumBench(kHash_ChecksumType); )
78