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 "SkMD5.h"
11#include "SkRandom.h"
12#include "SkSHA1.h"
13#include "SkTemplates.h"
14
15enum ChecksumType {
16    kChecksum_ChecksumType,
17    kMD5_ChecksumType,
18    kSHA1_ChecksumType,
19    kMurmur3_ChecksumType,
20};
21
22class ComputeChecksumBench : public Benchmark {
23    enum {
24        U32COUNT  = 256,
25        SIZE      = U32COUNT * 4,
26    };
27    uint32_t    fData[U32COUNT];
28    ChecksumType fType;
29
30public:
31    ComputeChecksumBench(ChecksumType type) : fType(type) {
32        SkRandom rand;
33        for (int i = 0; i < U32COUNT; ++i) {
34            fData[i] = rand.nextU();
35        }
36    }
37
38    bool isSuitableFor(Backend backend) override {
39        return backend == kNonRendering_Backend;
40    }
41
42protected:
43    virtual const char* onGetName() {
44        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 kMurmur3_ChecksumType: return "compute_murmur3";
49
50            default: SK_CRASH(); return "";
51        }
52    }
53
54    virtual void onDraw(const int loops, SkCanvas*) {
55        switch (fType) {
56            case kChecksum_ChecksumType: {
57                for (int i = 0; i < loops; i++) {
58                    volatile uint32_t result = SkChecksum::Compute(fData, sizeof(fData));
59                    sk_ignore_unused_variable(result);
60                }
61            } break;
62            case kMD5_ChecksumType: {
63                for (int i = 0; i < loops; 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 < loops; 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 kMurmur3_ChecksumType: {
79                for (int i = 0; i < loops; i++) {
80                    volatile uint32_t result = SkChecksum::Murmur3(fData, sizeof(fData));
81                    sk_ignore_unused_variable(result);
82                }
83            }break;
84        }
85
86    }
87
88private:
89    typedef Benchmark INHERITED;
90};
91
92///////////////////////////////////////////////////////////////////////////////
93
94DEF_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); )
98