snappy 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,8 +36,6 @@
36
36
  #define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
37
37
 
38
38
  #include <cstddef>
39
- #include <cstdint>
40
- #include <string>
41
39
 
42
40
  #if ${HAVE_SYS_UIO_H_01} // HAVE_SYS_UIO_H
43
41
  #include <sys/uio.h>
@@ -51,15 +49,6 @@
51
49
 
52
50
  namespace snappy {
53
51
 
54
- using int8 = std::int8_t;
55
- using uint8 = std::uint8_t;
56
- using int16 = std::int16_t;
57
- using uint16 = std::uint16_t;
58
- using int32 = std::int32_t;
59
- using uint32 = std::uint32_t;
60
- using int64 = std::int64_t;
61
- using uint64 = std::uint64_t;
62
-
63
52
  #if !${HAVE_SYS_UIO_H_01} // !HAVE_SYS_UIO_H
64
53
  // Windows does not have an iovec type, yet the concept is universally useful.
65
54
  // It is simple to define it ourselves, so we put it inside our own namespace.
@@ -28,23 +28,74 @@
28
28
  //
29
29
  // Various stubs for the unit tests for the open-source version of Snappy.
30
30
 
31
- #ifdef HAVE_CONFIG_H
32
- #include "config.h"
33
- #endif
34
-
35
- #ifdef HAVE_WINDOWS_H
36
- // Needed to be able to use std::max without workarounds in the source code.
37
- // https://support.microsoft.com/en-us/help/143208/prb-using-stl-in-windows-program-can-cause-min-max-conflicts
38
- #define NOMINMAX
39
- #include <windows.h>
40
- #endif
41
-
42
31
  #include "snappy-test.h"
43
32
 
44
33
  #include <algorithm>
34
+ #include <cstdarg>
35
+ #include <cstdio>
36
+ #include <cstdlib>
37
+ #include <iostream>
38
+ #include <string>
39
+
40
+ namespace file {
41
+
42
+ OptionsStub::OptionsStub() = default;
43
+ OptionsStub::~OptionsStub() = default;
44
+
45
+ const OptionsStub &Defaults() {
46
+ static OptionsStub defaults;
47
+ return defaults;
48
+ }
49
+
50
+ StatusStub::StatusStub() = default;
51
+ StatusStub::StatusStub(const StatusStub &) = default;
52
+ StatusStub &StatusStub::operator=(const StatusStub &) = default;
53
+ StatusStub::~StatusStub() = default;
54
+
55
+ bool StatusStub::ok() { return true; }
56
+
57
+ StatusStub GetContents(const std::string &filename, std::string *output,
58
+ const OptionsStub & /* options */) {
59
+ std::FILE *fp = std::fopen(filename.c_str(), "rb");
60
+ if (fp == nullptr) {
61
+ std::perror(filename.c_str());
62
+ std::exit(1);
63
+ }
45
64
 
46
- DEFINE_bool(run_microbenchmarks, true,
47
- "Run microbenchmarks before doing anything else.");
65
+ output->clear();
66
+ while (!std::feof(fp)) {
67
+ char buffer[4096];
68
+ size_t bytes_read = std::fread(buffer, 1, sizeof(buffer), fp);
69
+ if (bytes_read == 0 && std::ferror(fp)) {
70
+ std::perror("fread");
71
+ std::exit(1);
72
+ }
73
+ output->append(buffer, bytes_read);
74
+ }
75
+
76
+ std::fclose(fp);
77
+ return StatusStub();
78
+ }
79
+
80
+ StatusStub SetContents(const std::string &file_name, const std::string &content,
81
+ const OptionsStub & /* options */) {
82
+ std::FILE *fp = std::fopen(file_name.c_str(), "wb");
83
+ if (fp == nullptr) {
84
+ std::perror(file_name.c_str());
85
+ std::exit(1);
86
+ }
87
+
88
+ size_t bytes_written = std::fwrite(content.data(), 1, content.size(), fp);
89
+ if (bytes_written != content.size()) {
90
+ std::perror("fwrite");
91
+ std::exit(1);
92
+ }
93
+
94
+ std::fclose(fp);
95
+ return StatusStub();
96
+ }
97
+
98
+ } // namespace file
48
99
 
49
100
  namespace snappy {
50
101
 
@@ -56,212 +107,51 @@ std::string ReadTestDataFile(const std::string& base, size_t size_limit) {
56
107
  prefix = std::string(srcdir) + "/";
57
108
  }
58
109
  file::GetContents(prefix + "testdata/" + base, &contents, file::Defaults()
59
- ).CheckSuccess();
110
+ ).ok();
60
111
  if (size_limit > 0) {
61
112
  contents = contents.substr(0, size_limit);
62
113
  }
63
114
  return contents;
64
115
  }
65
116
 
66
- std::string ReadTestDataFile(const std::string& base) {
67
- return ReadTestDataFile(base, 0);
68
- }
69
-
70
117
  std::string StrFormat(const char* format, ...) {
71
- char buf[4096];
72
- va_list ap;
118
+ char buffer[4096];
119
+ std::va_list ap;
73
120
  va_start(ap, format);
74
- vsnprintf(buf, sizeof(buf), format, ap);
121
+ std::vsnprintf(buffer, sizeof(buffer), format, ap);
75
122
  va_end(ap);
76
- return buf;
123
+ return buffer;
77
124
  }
78
125
 
79
- bool benchmark_running = false;
80
- int64 benchmark_real_time_us = 0;
81
- int64 benchmark_cpu_time_us = 0;
82
- std::string* benchmark_label = nullptr;
83
- int64 benchmark_bytes_processed = 0;
84
-
85
- void ResetBenchmarkTiming() {
86
- benchmark_real_time_us = 0;
87
- benchmark_cpu_time_us = 0;
88
- }
126
+ LogMessage::~LogMessage() { std::cerr << std::endl; }
89
127
 
90
- #ifdef WIN32
91
- LARGE_INTEGER benchmark_start_real;
92
- FILETIME benchmark_start_cpu;
93
- #else // WIN32
94
- struct timeval benchmark_start_real;
95
- struct rusage benchmark_start_cpu;
96
- #endif // WIN32
97
-
98
- void StartBenchmarkTiming() {
99
- #ifdef WIN32
100
- QueryPerformanceCounter(&benchmark_start_real);
101
- FILETIME dummy;
102
- CHECK(GetProcessTimes(
103
- GetCurrentProcess(), &dummy, &dummy, &dummy, &benchmark_start_cpu));
104
- #else
105
- gettimeofday(&benchmark_start_real, NULL);
106
- if (getrusage(RUSAGE_SELF, &benchmark_start_cpu) == -1) {
107
- perror("getrusage(RUSAGE_SELF)");
108
- exit(1);
109
- }
110
- #endif
111
- benchmark_running = true;
128
+ LogMessage &LogMessage::operator<<(const std::string &message) {
129
+ std::cerr << message;
130
+ return *this;
112
131
  }
113
132
 
114
- void StopBenchmarkTiming() {
115
- if (!benchmark_running) {
116
- return;
117
- }
118
-
119
- #ifdef WIN32
120
- LARGE_INTEGER benchmark_stop_real;
121
- LARGE_INTEGER benchmark_frequency;
122
- QueryPerformanceCounter(&benchmark_stop_real);
123
- QueryPerformanceFrequency(&benchmark_frequency);
124
-
125
- double elapsed_real = static_cast<double>(
126
- benchmark_stop_real.QuadPart - benchmark_start_real.QuadPart) /
127
- benchmark_frequency.QuadPart;
128
- benchmark_real_time_us += elapsed_real * 1e6 + 0.5;
129
-
130
- FILETIME benchmark_stop_cpu, dummy;
131
- CHECK(GetProcessTimes(
132
- GetCurrentProcess(), &dummy, &dummy, &dummy, &benchmark_stop_cpu));
133
-
134
- ULARGE_INTEGER start_ulargeint;
135
- start_ulargeint.LowPart = benchmark_start_cpu.dwLowDateTime;
136
- start_ulargeint.HighPart = benchmark_start_cpu.dwHighDateTime;
137
-
138
- ULARGE_INTEGER stop_ulargeint;
139
- stop_ulargeint.LowPart = benchmark_stop_cpu.dwLowDateTime;
140
- stop_ulargeint.HighPart = benchmark_stop_cpu.dwHighDateTime;
141
-
142
- benchmark_cpu_time_us +=
143
- (stop_ulargeint.QuadPart - start_ulargeint.QuadPart + 5) / 10;
144
- #else // WIN32
145
- struct timeval benchmark_stop_real;
146
- gettimeofday(&benchmark_stop_real, NULL);
147
- benchmark_real_time_us +=
148
- 1000000 * (benchmark_stop_real.tv_sec - benchmark_start_real.tv_sec);
149
- benchmark_real_time_us +=
150
- (benchmark_stop_real.tv_usec - benchmark_start_real.tv_usec);
151
-
152
- struct rusage benchmark_stop_cpu;
153
- if (getrusage(RUSAGE_SELF, &benchmark_stop_cpu) == -1) {
154
- perror("getrusage(RUSAGE_SELF)");
155
- exit(1);
156
- }
157
- benchmark_cpu_time_us += 1000000 * (benchmark_stop_cpu.ru_utime.tv_sec -
158
- benchmark_start_cpu.ru_utime.tv_sec);
159
- benchmark_cpu_time_us += (benchmark_stop_cpu.ru_utime.tv_usec -
160
- benchmark_start_cpu.ru_utime.tv_usec);
161
- #endif // WIN32
162
-
163
- benchmark_running = false;
133
+ LogMessage &LogMessage::operator<<(int number) {
134
+ std::cerr << number;
135
+ return *this;
164
136
  }
165
137
 
166
- void SetBenchmarkLabel(const std::string& str) {
167
- if (benchmark_label) {
168
- delete benchmark_label;
169
- }
170
- benchmark_label = new std::string(str);
171
- }
138
+ #ifdef _MSC_VER
139
+ // ~LogMessageCrash calls std::abort() and therefore never exits. This is by
140
+ // design, so temporarily disable warning C4722.
141
+ #pragma warning(push)
142
+ #pragma warning(disable : 4722)
143
+ #endif
172
144
 
173
- void SetBenchmarkBytesProcessed(int64 bytes) {
174
- benchmark_bytes_processed = bytes;
145
+ LogMessageCrash::~LogMessageCrash() {
146
+ std::cerr << std::endl;
147
+ std::abort();
175
148
  }
176
149
 
177
- struct BenchmarkRun {
178
- int64 real_time_us;
179
- int64 cpu_time_us;
180
- };
181
-
182
- struct BenchmarkCompareCPUTime {
183
- bool operator() (const BenchmarkRun& a, const BenchmarkRun& b) const {
184
- return a.cpu_time_us < b.cpu_time_us;
185
- }
186
- };
187
-
188
- void Benchmark::Run() {
189
- for (int test_case_num = start_; test_case_num <= stop_; ++test_case_num) {
190
- // Run a few iterations first to find out approximately how fast
191
- // the benchmark is.
192
- const int kCalibrateIterations = 100;
193
- ResetBenchmarkTiming();
194
- StartBenchmarkTiming();
195
- (*function_)(kCalibrateIterations, test_case_num);
196
- StopBenchmarkTiming();
197
-
198
- // Let each test case run for about 200ms, but at least as many
199
- // as we used to calibrate.
200
- // Run five times and pick the median.
201
- const int kNumRuns = 5;
202
- const int kMedianPos = kNumRuns / 2;
203
- int num_iterations = 0;
204
- if (benchmark_real_time_us > 0) {
205
- num_iterations = 200000 * kCalibrateIterations / benchmark_real_time_us;
206
- }
207
- num_iterations = std::max(num_iterations, kCalibrateIterations);
208
- BenchmarkRun benchmark_runs[kNumRuns];
209
-
210
- for (int run = 0; run < kNumRuns; ++run) {
211
- ResetBenchmarkTiming();
212
- StartBenchmarkTiming();
213
- (*function_)(num_iterations, test_case_num);
214
- StopBenchmarkTiming();
215
-
216
- benchmark_runs[run].real_time_us = benchmark_real_time_us;
217
- benchmark_runs[run].cpu_time_us = benchmark_cpu_time_us;
218
- }
219
-
220
- std::string heading = StrFormat("%s/%d", name_.c_str(), test_case_num);
221
- std::string human_readable_speed;
222
-
223
- std::nth_element(benchmark_runs,
224
- benchmark_runs + kMedianPos,
225
- benchmark_runs + kNumRuns,
226
- BenchmarkCompareCPUTime());
227
- int64 real_time_us = benchmark_runs[kMedianPos].real_time_us;
228
- int64 cpu_time_us = benchmark_runs[kMedianPos].cpu_time_us;
229
- if (cpu_time_us <= 0) {
230
- human_readable_speed = "?";
231
- } else {
232
- int64 bytes_per_second =
233
- benchmark_bytes_processed * 1000000 / cpu_time_us;
234
- if (bytes_per_second < 1024) {
235
- human_readable_speed =
236
- StrFormat("%dB/s", static_cast<int>(bytes_per_second));
237
- } else if (bytes_per_second < 1024 * 1024) {
238
- human_readable_speed = StrFormat(
239
- "%.1fkB/s", bytes_per_second / 1024.0f);
240
- } else if (bytes_per_second < 1024 * 1024 * 1024) {
241
- human_readable_speed = StrFormat(
242
- "%.1fMB/s", bytes_per_second / (1024.0f * 1024.0f));
243
- } else {
244
- human_readable_speed = StrFormat(
245
- "%.1fGB/s", bytes_per_second / (1024.0f * 1024.0f * 1024.0f));
246
- }
247
- }
248
-
249
- fprintf(stderr,
250
- #ifdef WIN32
251
- "%-18s %10I64d %10I64d %10d %s %s\n",
252
- #else
253
- "%-18s %10lld %10lld %10d %s %s\n",
150
+ #ifdef _MSC_VER
151
+ #pragma warning(pop)
254
152
  #endif
255
- heading.c_str(),
256
- static_cast<long long>(real_time_us * 1000 / num_iterations),
257
- static_cast<long long>(cpu_time_us * 1000 / num_iterations),
258
- num_iterations,
259
- human_readable_speed.c_str(),
260
- benchmark_label->c_str());
261
- }
262
- }
263
153
 
264
- #ifdef HAVE_LIBZ
154
+ #if HAVE_LIBZ
265
155
 
266
156
  ZLib::ZLib()
267
157
  : comp_init_(false),