-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.cpp
217 lines (198 loc) · 7.81 KB
/
benchmark.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2015, Matthias Vallentin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include <bzlib.h>
#include <zlib.h>
#include <chrono>
#include <iterator>
#include <iostream>
#include <vector>
#if defined(__MSVCRT__) || defined(__OS2__) || defined(_MSC_VER)
#include <fcntl.h>
#include <io.h>
#endif
#include "bundle/bundle.hpp"
// Abstracts an algorithm with compression and decompression functionality.
template <class Algorithm>
struct algorithm;
// The DEFLATE algorithm.
template <unsigned Algorithm>
struct gzip { };
// The BZIP2 algorithm.
template <int Level>
struct bzip2 { };
// An algorithm from bundle.
template <int Level>
struct other { };
// Specialization for bundle.
template <unsigned Algorithm>
struct algorithm<other<Algorithm>> {
static std::string name() {
return bundle::name_of(Algorithm);
}
template <class Input, class Output>
static size_t compress(Input const& in, Output& out) {
out.resize(bundle::bound(Algorithm, in.size()));
auto out_size = out.size();
auto result = bundle::pack(Algorithm, &in[0], in.size(), &out[0], out_size);
// There's an issue with bundle's notion of successful packing. The
// function pack() returns true iff the output is strictly smaller than the
// input. This is obviously not true for RAW, which is why we exclude it.
if (Algorithm == bundle::RAW)
return in.size();
if (!result)
throw std::runtime_error{"bundle compression failed"};
return out_size;
}
template <class Input, class Output>
static size_t uncompress(Input const& in, Output& out) {
size_t out_size = out.size();
if (!bundle::unpack(Algorithm, &in[0], in.size(), &out[0], out_size))
throw std::runtime_error{"bundle decompression failed"};
return out_size;
}
};
// Specialization for zlib.
template <int Level>
struct algorithm<gzip<Level>> {
static std::string name() {
return "DEFLATE:" + std::to_string(Level);
}
template <class Input, class Output>
static size_t compress(Input const& in, Output& out) {
out.resize(compressBound(in.size()));
uLongf out_size = out.size();
auto result = compress2(
reinterpret_cast<Bytef*>(&out[0]), &out_size,
reinterpret_cast<Bytef const*>(in.data()), in.size(), Level);
if (result != Z_OK)
throw std::runtime_error{"zlib compression failed"};
return out_size;
}
template <class Input, class Output>
static size_t uncompress(Input const& in, Output& out) {
using ::uncompress;
uLongf out_size = out.size();
auto result = uncompress(
reinterpret_cast<Bytef*>(&out[0]), &out_size,
reinterpret_cast<Bytef const*>(in.data()), in.size());
if (result != Z_OK)
throw std::runtime_error{"zlib uncompression failed"};
return out_size;
}
};
// Specialization for bzlib.
template <int Level>
struct algorithm<bzip2<Level>> {
static std::string name() {
return "BZIP2:" + std::to_string(Level);
}
template <class Input, class Output>
static size_t compress(Input const& in, Output& out) {
out.resize(static_cast<size_t>(in.size() * 1.1) + 600);
unsigned out_size = out.size();
auto result = BZ2_bzBuffToBuffCompress(
&out[0], &out_size, const_cast<char*>(&in[0]), in.size(), Level, 0, 30);
if (result != BZ_OK)
throw std::runtime_error{"bzip2 compression failed"};
return out_size;
}
template <class Input, class Output>
static size_t uncompress(Input const& in, Output& out) {
unsigned out_size = out.size();
auto result = BZ2_bzBuffToBuffDecompress(
&out[0], &out_size, const_cast<char*>(&in[0]), in.size(), 0, 0);
if (result != BZ_OK)
throw std::runtime_error{"bzip2 uncompression failed"};
return out_size;
}
};
template <class Algorithm, class Buffer>
void run(Buffer const& buffer) {
using clock = std::chrono::high_resolution_clock;
auto to_mus = [](std::chrono::high_resolution_clock::duration d) {
return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
};
Buffer packed;
packed.resize(buffer.size() * 2); // avoid hitting the allocator later
auto pack_start = clock::now();
auto packed_size = Algorithm::compress(buffer, packed);
auto pack_stop = clock::now();
packed.resize(packed_size);
Buffer unpacked;
unpacked.resize(buffer.size());
auto unpack_start = clock::now();
auto unpacked_size = Algorithm::uncompress(packed, unpacked);
auto unpack_stop = clock::now();
std::cout << Algorithm::name() << '\t'
<< buffer.size() << '\t'
<< packed_size << '\t'
<< unpacked_size << '\t'
<< to_mus(pack_stop - pack_start) << '\t'
<< to_mus(unpack_stop - unpack_start) << std::endl;
}
auto main() -> int {
// Force binary std::cin on a few platforms.
#if defined(__MSVCRT__) || defined(__OS2__) || defined(_MSC_VER)
setmode(fileno(stdin), O_BINARY);
setmode(fileno(stdout), O_BINARY);
#endif
std::string buffer{std::istreambuf_iterator<char>{std::cin},
std::istreambuf_iterator<char>{}};
std::cout << "Algorithm\tRaw\tPacked\tUnpacked\tCompression\tDecompression\n";
// Run all bundle algorithms except for SHOCO, which only works with ASCII
// data.
run<algorithm<other<bundle::RAW>>>(buffer);
run<algorithm<other<bundle::LZ4F>>>(buffer);
run<algorithm<other<bundle::MINIZ>>>(buffer);
run<algorithm<other<bundle::LZIP>>>(buffer);
run<algorithm<other<bundle::LZMA20>>>(buffer);
run<algorithm<other<bundle::ZPAQ>>>(buffer);
run<algorithm<other<bundle::LZ4>>>(buffer);
run<algorithm<other<bundle::BROTLI9>>>(buffer);
run<algorithm<other<bundle::ZSTD>>>(buffer);
run<algorithm<other<bundle::LZMA25>>>(buffer);
run<algorithm<other<bundle::BSC>>>(buffer);
run<algorithm<other<bundle::BROTLI11>>>(buffer);
run<algorithm<other<bundle::SHRINKER>>>(buffer);
run<algorithm<other<bundle::CSC20>>>(buffer);
run<algorithm<other<bundle::ZSTDF>>>(buffer);
run<algorithm<other<bundle::BCM>>>(buffer);
run<algorithm<other<bundle::ZLING>>>(buffer);
run<algorithm<other<bundle::MCM>>>(buffer);
run<algorithm<other<bundle::TANGELO>>>(buffer);
run<algorithm<other<bundle::ZMOLLY>>>(buffer);
run<algorithm<other<bundle::CRUSH>>>(buffer);
run<algorithm<other<bundle::LZJB>>>(buffer);
// Run gzip.
run<algorithm<gzip<1>>>(buffer);
run<algorithm<gzip<9>>>(buffer);
// Run bzip2.
run<algorithm<bzip2<1>>>(buffer);
run<algorithm<bzip2<9>>>(buffer);
}