File: hash-performance.cc

package info (click to toggle)
eckit 1.32.4-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 600,644 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 41
file content (98 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <cassert>
#include <iostream>
#include <memory>

#include "eckit/io/Buffer.h"
#include "eckit/log/Bytes.h"
#include "eckit/log/Seconds.h"
#include "eckit/log/Timer.h"

#include "eckit/utils/Hash.h"

#include "eckit/testing/Test.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

namespace eckit {
namespace test {

//----------------------------------------------------------------------------------------------------------------------

template <int N, int M>
void timeAdd(Hash& hash, eckit::Buffer& buffer, eckit::Timer& timer) {

    timer.start();

    for (int i = 0; i < N; ++i) {
        hash.reset();
        for (size_t j = 0; j < M; ++j) {
            hash.add(buffer, buffer.size());
        }
        hash.digest();
    }

    timer.stop();

    std::cout << " - " << N << " x " << M << " x add(" << Bytes(buffer.size()) << ") rate "
              << Bytes(N * M * buffer.size(), timer) << std::endl;
}

template <int N>
void timeCompute(Hash& hash, eckit::Buffer& buffer, eckit::Timer& timer) {

    timer.start();

    for (int i = 0; i < N; ++i) {
        std::string s = hash.compute(buffer, buffer.size());
        assert(!s.empty());
    }

    timer.stop();

    std::cout << " - " << N << " x compute(" << Bytes(buffer.size()) << ") rate " << Bytes(N * buffer.size(), timer)
              << std::endl;
}

//----------------------------------------------------------------------------------------------------------------------

CASE("Test hash performance") {

    eckit::Buffer buffer(4 * 1024 * 1024);
    eckit::Buffer buffer2(64 * 1024 * 1024);
    eckit::Timer timer;

    std::vector<std::string> hashes{"xxh64", "MD4", "MD5", "SHA1"};
    for (auto& name : hashes) {

        if (eckit::HashFactory::instance().has(name)) {

            std::cout << name << std::endl;

            std::unique_ptr<eckit::Hash> hash(eckit::HashFactory::instance().build(name));

            timeAdd<20, 1>(*hash, buffer, timer);
            timeCompute<5>(*hash, buffer2, timer);
        }
    }
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace test
}  // namespace eckit

int main(int argc, char* argv[]) {
    return run_tests(argc, argv);
}