-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufbench.cpp
173 lines (147 loc) · 4.06 KB
/
bufbench.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <benchmark/benchmark.h>
#include "buffer.h"
#include <stdint.h>
#include <vector>
uint8_t* get_buf(uint32_t len)
{
const char* seed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
uint8_t* buf = new uint8_t[len];
for (uint32_t i = 0; i < len; ++i) {
buf[i] = seed_chars[i % 52];
}
return buf;
}
std::vector<uint8_t> get_vec(uint32_t len)
{
const char* seed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::vector<uint8_t> vec(len);
for (uint32_t i = 0; i < len; ++i) {
vec[i] = seed_chars[i % 52];
}
return vec;
}
static void bm_create_arraybuf_from_byte_buffer(benchmark::State& state)
{
const uint32_t len = 65536;
uint8_t* test_buf = get_buf(len);
for (auto _ : state) {
arraybuf ab(nullptr, len);
memcpy(&ab[0], test_buf, len);
}
delete[] test_buf;
}
// Register the function as a benchmark
BENCHMARK(bm_create_arraybuf_from_byte_buffer);
static void bm_create_arraybuf_from_vector(benchmark::State& state)
{
const uint32_t len = 65536;
std::vector<uint8_t> vec = get_vec(len);
for (auto _ : state) {
arraybuf ab(vec);
}
}
BENCHMARK(bm_create_arraybuf_from_vector);
static void bm_find_first_easy(benchmark::State& state)
{
const uint32_t len = 256;
std::vector<uint8_t> vec = get_vec(len);
arraybuf ab(vec);
uint32_t result = 0;
for (auto _ : state) {
result = ab.find_first(arraybuf{'Z', 'a', 'b'});
if (result != 25) {
state.SkipWithError("Could not match string");
break;
}
}
}
BENCHMARK(bm_find_first_easy);
static void bm_find_first_hard(benchmark::State& state)
{
const uint32_t len = state.range(0);
std::vector<uint8_t> vec = get_vec(len);
vec[len - 5] = '1';
vec[len - 4] = '2';
vec[len - 3] = '3';
arraybuf ab(vec);
uint32_t result = 0;
for (auto _ : state) {
result = ab.find_first(arraybuf{'1', '2', '3'});
if (result != len - 5) {
state.SkipWithError("Could not match string");
break;
}
}
}
BENCHMARK(bm_find_first_hard)->Arg(65536)->Arg(67108864)->Arg(1073741824);
static void bm_find_first_needle(benchmark::State& state)
{
const uint32_t len = state.range(0);
std::vector<uint8_t> vec = get_vec(len);
vec[len - 5] = '1';
vec[len - 4] = '2';
vec[len - 3] = '3';
arraybuf ab(vec);
uint32_t result = 0;
buffer_needle bn({'1', '2', '3'});
for (auto _ : state) {
result = bn.first_match(ab);
if (result != len - 5) {
state.SkipWithError("Could not match string");
break;
}
}
}
BENCHMARK(bm_find_first_needle)->Arg(65536)->Arg(67108864)->Arg(1073741824);
static void bm_find_all(benchmark::State& state)
{
const uint32_t len = state.range(0);
std::vector<uint8_t> vec = get_vec(len);
arraybuf ab(vec);
std::list<uint32_t> result;
uint32_t expected_length = len / 52;
for (auto _ : state) {
result = ab.find_all(arraybuf{'Z', 'a', 'b'});
if (result.size() != expected_length) {
state.SkipWithError("Could not match string");
break;
}
}
}
BENCHMARK(bm_find_all)->Arg(65536)->Arg(67108864)->Arg(1073741824);
static void bm_find_all_needle_short(benchmark::State& state)
{
const uint32_t len = state.range(0);
std::vector<uint8_t> vec = get_vec(len);
arraybuf ab(vec);
std::list<uint32_t> result;
buffer_needle bn({'Z', 'a', 'b'});
uint32_t expected_length = len / 52;
for (auto _ : state) {
result = bn.match(ab);
if (result.size() != expected_length) {
state.SkipWithError("Could not match string");
break;
}
}
}
BENCHMARK(bm_find_all_needle_short)->Arg(65536)->Arg(67108864)->Arg(1073741824);
static void bm_find_all_needle_long(benchmark::State& state)
{
const uint32_t len = state.range(0);
std::vector<uint8_t> vec = get_vec(len);
arraybuf ab(vec);
std::list<uint32_t> result;
buffer_needle bn({'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'});
uint32_t expected_length = len / 52;
for (auto _ : state) {
result = bn.match(ab);
if (result.size() != expected_length) {
state.SkipWithError("Could not match string");
break;
}
}
}
BENCHMARK(bm_find_all_needle_long)->Arg(65536)->Arg(67108864)->Arg(1073741824);
BENCHMARK_MAIN();