Commit 6f29a9bb authored by Adenilson Cavalcanti's avatar Adenilson Cavalcanti Committed by Commit Bot

Adding a compression level flag to zlib_bench

Optimizations can have different impact depending on the compression
level used.

It is quite handy to be able to change the compression level used for
experiments while optimizing zlib.

This patch adds an optional flag (-compression) to allow passing the desired compression
level (0 to 9). To keep consistency, the user has to pass the target
wrapper format (e.g. -wrapper gzip|zlib|raw).

Finally, a help parameter is now supported (i.e. ./zlib_bench -h | --help) that
will print the expected parameters while invoking the tool.

Change-Id: I0b29fc2bd31bfccc39a7b7a10b510d9ca46d47e2
Reviewed-on: https://chromium-review.googlesource.com/1180341
Commit-Queue: Adenilson Cavalcanti <cavalcantii@chromium.org>
Reviewed-by: default avatarChris Blume <cblume@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584233}
parent c827a672
...@@ -112,6 +112,7 @@ void zlib_compress( ...@@ -112,6 +112,7 @@ void zlib_compress(
const char* input, const char* input,
const size_t input_size, const size_t input_size,
std::string* output, std::string* output,
const int compression_level,
bool resize_output = false) bool resize_output = false)
{ {
if (resize_output) if (resize_output)
...@@ -121,7 +122,7 @@ void zlib_compress( ...@@ -121,7 +122,7 @@ void zlib_compress(
z_stream stream; z_stream stream;
memset(&stream, 0, sizeof(stream)); memset(&stream, 0, sizeof(stream));
int result = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, int result = deflateInit2(&stream, compression_level, Z_DEFLATED,
zlib_stream_wrapper_type(type), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); zlib_stream_wrapper_type(type), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
if (result != Z_OK) if (result != Z_OK)
error_exit("deflateInit2 failed", result); error_exit("deflateInit2 failed", result);
...@@ -181,7 +182,8 @@ void verify_equal(const char* input, size_t size, std::string* output) { ...@@ -181,7 +182,8 @@ void verify_equal(const char* input, size_t size, std::string* output) {
exit(3); exit(3);
} }
void zlib_file(const char* name, const zlib_wrapper type) { void zlib_file(const char* name, const zlib_wrapper type,
const int compression_level) {
/* /*
* Read the file data. * Read the file data.
*/ */
...@@ -229,13 +231,15 @@ void zlib_file(const char* name, const zlib_wrapper type) { ...@@ -229,13 +231,15 @@ void zlib_file(const char* name, const zlib_wrapper type) {
auto start = now(); auto start = now();
for (int b = 0; b < blocks; ++b) for (int b = 0; b < blocks; ++b)
for (int r = 0; r < repeats; ++r) for (int r = 0; r < repeats; ++r)
zlib_compress(type, input[b], input_length[b], &compressed[b]); zlib_compress(type, input[b], input_length[b], &compressed[b],
compression_level);
ctime[run] = std::chrono::duration<double>(now() - start).count(); ctime[run] = std::chrono::duration<double>(now() - start).count();
// Compress again, resizing compressed, so we don't leave junk at the // Compress again, resizing compressed, so we don't leave junk at the
// end of the compressed string that could confuse zlib_uncompress(). // end of the compressed string that could confuse zlib_uncompress().
for (int b = 0; b < blocks; ++b) for (int b = 0; b < blocks; ++b)
zlib_compress(type, input[b], input_length[b], &compressed[b], true); zlib_compress(type, input[b], input_length[b], &compressed[b],
compression_level, true);
for (int b = 0; b < blocks; ++b) for (int b = 0; b < blocks; ++b)
output[b].resize(input_length[b]); output[b].resize(input_length[b]);
...@@ -277,22 +281,57 @@ void zlib_file(const char* name, const zlib_wrapper type) { ...@@ -277,22 +281,57 @@ void zlib_file(const char* name, const zlib_wrapper type) {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
zlib_wrapper type = kWrapperNONE; zlib_wrapper type = kWrapperNONE;
int file_index = 3;
// DEFAULT is equivalent to level 6. Compression level ranges from 1 to 9,
// being 0 used for Z_NO_COMPRESSION.
int compression_level = Z_DEFAULT_COMPRESSION;
auto help_msg_exit = [&argv] {
printf("usage: %s -wrapper gzip|zlib|raw -compression [0:9] files...\n", argv[0]);
exit(1);
};
// If no arguments were passed, print usage and exit.
if (argc == 1)
help_msg_exit();
if (argc > 1) { if (argc > 1) {
if (!strcmp(argv[1], "zlib")) if (strcmp(argv[1], "-wrapper") || !(strcmp(argv[1], "-h")) ||
!(strcmp(argv[1], "--help")))
help_msg_exit();
// Got check the argument count before reading.
if (argc < 3)
help_msg_exit();
if (!strcmp(argv[2], "zlib"))
type = kWrapperZLIB; type = kWrapperZLIB;
else if (!strcmp(argv[1], "gzip")) else if (!strcmp(argv[2], "gzip"))
type = kWrapperGZIP; type = kWrapperGZIP;
else if (!strcmp(argv[1], "raw")) else if (!strcmp(argv[2], "raw"))
type = kWrapperZRAW; type = kWrapperZRAW;
else
help_msg_exit();
}
if (argc >= 4) {
if (!strcmp(argv[3], "-compression")) {
int user_level = atoi(argv[4]);
if ((user_level <= 9) && (user_level >= 0)) {
compression_level = user_level;
file_index = 5;
} else
help_msg_exit();
}
} }
if ((type != kWrapperNONE) && (argc > 2)) { if ((type != kWrapperNONE) && (argc > 2)) {
for (int file = 2; file < argc; ++file) while (file_index < argc) {
zlib_file(argv[file], type); // TODO: return a status (e.g. success | fail).
return 0; zlib_file(argv[file_index], type, compression_level);
++file_index;
}
} }
printf("usage: %s gzip|zlib|raw files...\n", argv[0]); return 0;
return 1;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment