Skip to content

Commit 1cd9419

Browse files
committed
add RandomUniform32 benchmarks
1 parent fa3d255 commit 1cd9419

File tree

1 file changed

+262
-0
lines changed

1 file changed

+262
-0
lines changed

rand.cc

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
// Copyright (c) 2020 Vicente Romero Calero. All rights reserved.
2+
// Licensed under the MIT License.
3+
// See LICENSE file in the project root for full license information.
4+
5+
#include <algorithm>
6+
#include <iostream>
7+
#include <limits>
8+
#include <random>
9+
#include <unordered_map>
10+
#include <vector>
11+
12+
#include "common.h"
13+
14+
#include "benchmark/include/benchmark/benchmark.h"
15+
#include "SIMDCompressionAndIntersection/include/binarypacking.h"
16+
#include "SIMDCompressionAndIntersection/include/fastpfor.h"
17+
#include "SIMDCompressionAndIntersection/include/variablebyte.h"
18+
#include "SIMDCompressionAndIntersection/include/varintgb.h"
19+
#include "VTEnc/vtenc.h"
20+
21+
class RandomUniform32 : public benchmark::Fixture {
22+
private:
23+
void makeSortedSet(std::vector<uint32_t>& v) {
24+
std::sort(v.begin(), v.end());
25+
26+
auto last = std::unique(v.begin(), v.end());
27+
v.erase(last, v.end());
28+
}
29+
30+
void generateRandomDistribution(size_t len) {
31+
std::random_device rd;
32+
std::mt19937 mt(rd());
33+
std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint32_t>::max());
34+
std::vector<uint32_t> v = std::vector<uint32_t>(len);
35+
36+
for (size_t i = 0; i < len; ++i) {
37+
v[i] = dist(mt);
38+
}
39+
40+
makeSortedSet(v);
41+
42+
dist_map[len] = v;
43+
}
44+
45+
public:
46+
static std::unordered_map<size_t, std::vector<uint32_t> > dist_map;
47+
48+
void SetUp(const ::benchmark::State& state) {
49+
size_t len = state.range(0);
50+
51+
if (dist_map.find(len) == dist_map.end()) {
52+
generateRandomDistribution(len);
53+
}
54+
}
55+
56+
void TearDown(const ::benchmark::State& state) {}
57+
};
58+
59+
std::unordered_map<size_t, std::vector<uint32_t> > RandomUniform32::dist_map = std::unordered_map<size_t, std::vector<uint32_t> >();
60+
61+
static void benchmarkEncode(SIMDCompressionUtil& comp, benchmark::State& state) {
62+
CompressionStats stats(state);
63+
64+
for (auto _ : state) {
65+
state.PauseTiming();
66+
comp.Reset();
67+
state.ResumeTiming();
68+
69+
comp.Encode();
70+
}
71+
72+
stats.SetInputLengthInBytes(comp.InputLength() * sizeof(uint32_t));
73+
stats.SetEncodedLengthInBytes(comp.EncodedLength() * sizeof(uint32_t));
74+
stats.SetFinalStats();
75+
}
76+
77+
static void benchmarkDecode(SIMDCompressionUtil& comp, benchmark::State& state) {
78+
CompressionStats stats(state);
79+
80+
comp.Encode();
81+
82+
for (auto _ : state) {
83+
comp.Decode();
84+
}
85+
86+
comp.EqualityCheck();
87+
88+
stats.SetInputLengthInBytes(comp.InputLength() * sizeof(uint32_t));
89+
stats.SetEncodedLengthInBytes(comp.EncodedLength() * sizeof(uint32_t));
90+
stats.SetFinalStats();
91+
}
92+
93+
BENCHMARK_DEFINE_F(RandomUniform32, Copy)(benchmark::State& state) {
94+
CompressionStats stats(state);
95+
size_t len = state.range(0);
96+
std::vector<uint32_t> &data = dist_map[len];
97+
std::vector<uint32_t> copyTo(data.size());
98+
99+
for (auto _ : state)
100+
std::copy(data.begin(), data.end(), copyTo.begin());
101+
102+
stats.SetInputLengthInBytes(data.size() * sizeof(uint32_t));
103+
stats.SetEncodedLengthInBytes(data.size() * sizeof(uint32_t));
104+
stats.SetFinalStats();
105+
}
106+
107+
BENCHMARK_DEFINE_F(RandomUniform32, VTEncEncode)(benchmark::State& state) {
108+
CompressionStats stats(state);
109+
size_t len = state.range(0);
110+
std::vector<uint32_t> &data = dist_map[len];
111+
size_t encodedLength;
112+
std::vector<uint8_t> encoded(vtenc_set_max_encoded_size_u32(data.size()));
113+
114+
for (auto _ : state)
115+
vtenc_set_encode_u32(data.data(), data.size(), encoded.data(), encoded.size(), &encodedLength);
116+
117+
stats.SetInputLengthInBytes(data.size() * sizeof(uint32_t));
118+
stats.SetEncodedLengthInBytes(encodedLength);
119+
stats.SetFinalStats();
120+
}
121+
122+
BENCHMARK_DEFINE_F(RandomUniform32, VTEncDecode)(benchmark::State& state) {
123+
CompressionStats stats(state);
124+
size_t len = state.range(0);
125+
std::vector<uint32_t> &data = dist_map[len];
126+
127+
size_t encodedLength;
128+
std::vector<uint8_t> encoded(vtenc_set_max_encoded_size_u32(data.size()));
129+
vtenc_set_encode_u32(data.data(), data.size(), encoded.data(), encoded.size(), &encodedLength);
130+
131+
std::vector<uint32_t> decoded(vtenc_set_decoded_size_u32(encoded.data(), encodedLength));
132+
133+
for (auto _ : state)
134+
vtenc_set_decode_u32(encoded.data(), encodedLength, decoded.data(), decoded.size());
135+
136+
if (data != decoded) {
137+
throw std::logic_error("equality check failed");
138+
}
139+
140+
stats.SetInputLengthInBytes(data.size() * sizeof(uint32_t));
141+
stats.SetEncodedLengthInBytes(encodedLength);
142+
stats.SetFinalStats();
143+
}
144+
145+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaVariableByteEncode)(benchmark::State& state) {
146+
size_t len = state.range(0);
147+
std::vector<uint32_t> &data = dist_map[len];
148+
SIMDCompressionLib::VByte<true> codec;
149+
SIMDCompressionUtil comp(codec, 1, data);
150+
benchmarkEncode(comp, state);
151+
}
152+
153+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaVariableByteDecode)(benchmark::State& state) {
154+
size_t len = state.range(0);
155+
std::vector<uint32_t> &data = dist_map[len];
156+
SIMDCompressionLib::VByte<true> codec;
157+
SIMDCompressionUtil comp(codec, 1, data);
158+
benchmarkDecode(comp, state);
159+
}
160+
161+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaVarIntGBEncode)(benchmark::State& state) {
162+
size_t len = state.range(0);
163+
std::vector<uint32_t> &data = dist_map[len];
164+
SIMDCompressionLib::VarIntGB<true> codec;
165+
SIMDCompressionUtil comp(codec, 1, data);
166+
benchmarkEncode(comp, state);
167+
}
168+
169+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaVarIntGBDecode)(benchmark::State& state) {
170+
size_t len = state.range(0);
171+
std::vector<uint32_t> &data = dist_map[len];
172+
SIMDCompressionLib::VarIntGB<true> codec;
173+
SIMDCompressionUtil comp(codec, 1, data);
174+
benchmarkDecode(comp, state);
175+
}
176+
177+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaBinaryPackingEncode)(benchmark::State& state) {
178+
size_t len = state.range(0);
179+
std::vector<uint32_t> &data = dist_map[len];
180+
SIMDCompressionLib::BinaryPacking<SIMDCompressionLib::BasicBlockPacker> codec;
181+
SIMDCompressionUtil comp(codec, codec.BlockSize, data);
182+
benchmarkEncode(comp, state);
183+
}
184+
185+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaBinaryPackingDecode)(benchmark::State& state) {
186+
size_t len = state.range(0);
187+
std::vector<uint32_t> &data = dist_map[len];
188+
SIMDCompressionLib::BinaryPacking<SIMDCompressionLib::BasicBlockPacker> codec;
189+
SIMDCompressionUtil comp(codec, codec.BlockSize, data);
190+
benchmarkDecode(comp, state);
191+
}
192+
193+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaFastPFor128Encode)(benchmark::State& state) {
194+
size_t len = state.range(0);
195+
std::vector<uint32_t> &data = dist_map[len];
196+
SIMDCompressionLib::FastPFor<4, true> codec;
197+
SIMDCompressionUtil comp(codec, codec.BlockSize, data);
198+
benchmarkEncode(comp, state);
199+
}
200+
201+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaFastPFor128Decode)(benchmark::State& state) {
202+
size_t len = state.range(0);
203+
std::vector<uint32_t> &data = dist_map[len];
204+
SIMDCompressionLib::FastPFor<4, true> codec;
205+
SIMDCompressionUtil comp(codec, codec.BlockSize, data);
206+
benchmarkDecode(comp, state);
207+
}
208+
209+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaFastPFor256Encode)(benchmark::State& state) {
210+
size_t len = state.range(0);
211+
std::vector<uint32_t> &data = dist_map[len];
212+
SIMDCompressionLib::FastPFor<8, true> codec;
213+
SIMDCompressionUtil comp(codec, codec.BlockSize, data);
214+
benchmarkEncode(comp, state);
215+
}
216+
217+
BENCHMARK_DEFINE_F(RandomUniform32, DeltaFastPFor256Decode)(benchmark::State& state) {
218+
size_t len = state.range(0);
219+
std::vector<uint32_t> &data = dist_map[len];
220+
SIMDCompressionLib::FastPFor<8, true> codec;
221+
SIMDCompressionUtil comp(codec, codec.BlockSize, data);
222+
benchmarkDecode(comp, state);
223+
}
224+
225+
BENCHMARK_REGISTER_F(RandomUniform32, Copy)
226+
->RangeMultiplier(10)->Range(100, 10000000);
227+
228+
BENCHMARK_REGISTER_F(RandomUniform32, VTEncEncode)
229+
->RangeMultiplier(10)->Range(100, 10000000);
230+
231+
BENCHMARK_REGISTER_F(RandomUniform32, VTEncDecode)
232+
->RangeMultiplier(10)->Range(100, 10000000);
233+
234+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaVariableByteEncode)
235+
->RangeMultiplier(10)->Range(100, 10000000);
236+
237+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaVariableByteDecode)
238+
->RangeMultiplier(10)->Range(100, 10000000);
239+
240+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaVarIntGBEncode)
241+
->RangeMultiplier(10)->Range(100, 10000000);
242+
243+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaVarIntGBDecode)
244+
->RangeMultiplier(10)->Range(100, 10000000);
245+
246+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaBinaryPackingEncode)
247+
->RangeMultiplier(10)->Range(100, 10000000);
248+
249+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaBinaryPackingDecode)
250+
->RangeMultiplier(10)->Range(100, 10000000);
251+
252+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaFastPFor128Encode)
253+
->RangeMultiplier(10)->Range(100, 10000000);
254+
255+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaFastPFor128Decode)
256+
->RangeMultiplier(10)->Range(100, 10000000);
257+
258+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaFastPFor256Encode)
259+
->RangeMultiplier(10)->Range(100, 10000000);
260+
261+
BENCHMARK_REGISTER_F(RandomUniform32, DeltaFastPFor256Decode)
262+
->RangeMultiplier(10)->Range(100, 10000000);

0 commit comments

Comments
 (0)