forked from google/gipfeli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgipfeli_test.cc
114 lines (100 loc) · 3.38 KB
/
gipfeli_test.cc
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
#include <sys/time.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "gipfeli.h"
#include "stubs-internal.h"
using std::cout;
using std::endl;
using std::vector;
const char testdata_prefix[] = "testdata/";
const int kRepetitions = 101;
const int kFiles = 9;
static struct {
const char* label;
const char* filename;
} files[] = {
{"html", "html"},
{"urls", "urls.10K"},
{"jpg", "fireworks.jpeg"},
{"pdf", "paper-100k.pdf"},
{"html4", "html_x_4"},
{"txt1", "alice29.txt"},
{"txt2", "asyoulik.txt"},
{"txt3", "lcet10.txt"},
{"txt4", "plrabn12.txt"},
};
void ReadTestDataFile(const string& filename, string* content) {
std::ifstream ifs(filename.c_str());
content->assign((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
double start;
double stop;
double Timestamp() {
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_usec + t.tv_sec * 1000000LL;
}
void ResetTimer() { start = Timestamp(); }
double GetElapsedTime() {
stop = Timestamp();
return (stop - start) / 1000000.0;
}
// Return true if compression and uncompression was successful
bool TestFile(const string& label, const string& filename,
size_t* original_size, size_t* compressed_size,
double* compression_time, double* uncompression_time) {
// Read the input
string original;
ReadTestDataFile(testdata_prefix + filename, &original);
*original_size = original.size();
// Init compressor and compress
string compressed;
util::compression::Compressor* compressor =
util::compression::NewGipfeliCompressor();
ResetTimer();
*compressed_size = compressor->Compress(original, &compressed);
*compression_time = GetElapsedTime();
// Decompress and destroy compressor
string uncompressed;
ResetTimer();
bool success = compressor->Uncompress(compressed, &uncompressed);
*uncompression_time = GetElapsedTime();
delete compressor;
return success && (original == uncompressed);
}
int main() {
// Collect performance data
vector<size_t> original_size(kFiles, 0);
vector<size_t> compressed_size(kFiles, 0);
vector<double> sum_of_compression_time(kFiles, 0);
vector<double> sum_of_uncompression_time(kFiles, 0);
vector<bool> valid(kFiles, true);
for (int i = 0; i < kRepetitions; i++)
for (int j = 0; j < kFiles; j++) {
double compression_time = 0;
double uncompression_time = 0;
bool success =
TestFile(files[j].label, files[j].filename, &original_size[j],
&compressed_size[j], &compression_time, &uncompression_time);
if (!success) valid[j] = false;
sum_of_compression_time[j] += compression_time;
sum_of_uncompression_time[j] += uncompression_time;
}
// Output test and benchmark results
for (int i = 0; i < kFiles; i++) {
cout << files[i].label << " [" << files[i].filename
<< "] Compression ratio: "
<< (1000 * compressed_size[i] / original_size[i]) / 10.0
<< "% Verification test: " << (valid[i] ? "PASSED." : "FAILED.")
<< endl;
cout << "Compression speed: "
<< (original_size[i] / (sum_of_compression_time[i] / kRepetitions)) /
1000000 << " M/s."
<< " Uncompression speed: "
<< (original_size[i] / (sum_of_uncompression_time[i] / kRepetitions)) /
1000000 << " M/s." << endl;
}
}