1#include <iostream>
2#include <iomanip>
3#include <city.h>
4#include <Common/Stopwatch.h>
5#include <Common/SipHash.h>
6#include <IO/ReadBufferFromFileDescriptor.h>
7#include <IO/ReadHelpers.h>
8#include "config_core.h"
9#if USE_SSL
10# include <openssl/md5.h>
11#endif
12
13
14int main(int, char **)
15{
16 using Strings = std::vector<std::string>;
17 using Hashes = std::vector<char>;
18 Strings strings;
19 size_t rows = 0;
20 size_t bytes = 0;
21
22 {
23 Stopwatch watch;
24
25 DB::ReadBufferFromFileDescriptor in(STDIN_FILENO);
26
27 while (!in.eof())
28 {
29 strings.push_back(std::string());
30 DB::readEscapedString(strings.back(), in);
31 DB::assertChar('\n', in);
32 bytes += strings.back().size() + 1;
33 }
34
35 watch.stop();
36 rows = strings.size();
37 std::cerr << std::fixed << std::setprecision(2)
38 << "Read " << rows << " rows, " << bytes / 1000000.0 << " MB"
39 << ", elapsed: " << watch.elapsedSeconds()
40 << " (" << rows / watch.elapsedSeconds() << " rows/sec., " << bytes / 1000000.0 / watch.elapsedSeconds() << " MB/sec.)"
41 << std::endl;
42 }
43
44 Hashes hashes(16 * rows);
45
46 {
47 Stopwatch watch;
48
49 for (size_t i = 0; i < rows; ++i)
50 {
51 *reinterpret_cast<UInt64*>(&hashes[i * 16]) = CityHash_v1_0_2::CityHash64(strings[i].data(), strings[i].size());
52 }
53
54 watch.stop();
55
56 UInt64 check = CityHash_v1_0_2::CityHash64(hashes.data(), hashes.size());
57
58 std::cerr << std::fixed << std::setprecision(2)
59 << "CityHash64 (check = " << check << ")"
60 << ", elapsed: " << watch.elapsedSeconds()
61 << " (" << rows / watch.elapsedSeconds() << " rows/sec., " << bytes / 1000000.0 / watch.elapsedSeconds() << " MB/sec.)"
62 << std::endl;
63 }
64
65/* {
66 Stopwatch watch;
67
68 std::vector<char> seed(16);
69
70 for (size_t i = 0; i < rows; ++i)
71 {
72 sipHash(
73 reinterpret_cast<unsigned char *>(&hashes[i * 16]),
74 reinterpret_cast<const unsigned char *>(strings[i].data()),
75 strings[i].size(),
76 reinterpret_cast<const unsigned char *>(seed.data()));
77 }
78
79 watch.stop();
80
81 UInt64 check = CityHash_v1_0_2::CityHash64(hashes.data(), hashes.size());
82
83 std::cerr << std::fixed << std::setprecision(2)
84 << "SipHash (check = " << check << ")"
85 << ", elapsed: " << watch.elapsedSeconds()
86 << " (" << rows / watch.elapsedSeconds() << " rows/sec., " << bytes / 1000000.0 / watch.elapsedSeconds() << " MB/sec.)"
87 << std::endl;
88 }*/
89
90 {
91 Stopwatch watch;
92
93 for (size_t i = 0; i < rows; ++i)
94 {
95 SipHash hash;
96 hash.update(strings[i].data(), strings[i].size());
97 hash.get128(&hashes[i * 16]);
98 }
99
100 watch.stop();
101
102 UInt64 check = CityHash_v1_0_2::CityHash64(hashes.data(), hashes.size());
103
104 std::cerr << std::fixed << std::setprecision(2)
105 << "SipHash, stream (check = " << check << ")"
106 << ", elapsed: " << watch.elapsedSeconds()
107 << " (" << rows / watch.elapsedSeconds() << " rows/sec., " << bytes / 1000000.0 / watch.elapsedSeconds() << " MB/sec.)"
108 << std::endl;
109 }
110
111#if USE_SSL
112 {
113 Stopwatch watch;
114
115 for (size_t i = 0; i < rows; ++i)
116 {
117 MD5_CTX state;
118 MD5_Init(&state);
119 MD5_Update(&state, reinterpret_cast<const unsigned char *>(strings[i].data()), strings[i].size());
120 MD5_Final(reinterpret_cast<unsigned char *>(&hashes[i * 16]), &state);
121 }
122
123 watch.stop();
124
125 UInt64 check = CityHash_v1_0_2::CityHash64(hashes.data(), hashes.size());
126
127 std::cerr << std::fixed << std::setprecision(2)
128 << "MD5 (check = " << check << ")"
129 << ", elapsed: " << watch.elapsedSeconds()
130 << " (" << rows / watch.elapsedSeconds() << " rows/sec., " << bytes / 1000000.0 / watch.elapsedSeconds() << " MB/sec.)"
131 << std::endl;
132 }
133#endif
134
135 return 0;
136}
137