/* Copyright (c) 2020 Dovecot authors, see the included COPYING file */
#include "lib.h"
-#include "buffer.h"
+#include "array.h"
#include "istream.h"
#include "ostream.h"
#include "randgen.h"
#include "time-util.h"
#include "strnum.h"
+#include "settings.h"
#include "compression.h"
#include <stdio.h>
* compressed and how long it took.
*/
-static void bench_compression_speed(const struct compression_handler *handler,
- unsigned int level, unsigned long block_count)
+static void
+bench_compression_speed(const struct compression_handler *handler,
+ struct event *event, unsigned long block_count)
{
struct istream *is = i_stream_create_file("decompressed.bin", 1024);
struct ostream *os = o_stream_create_file("compressed.bin", 0, 0644, 0);
- struct ostream *os_compressed = handler->create_ostream(os, level);
+ struct ostream *os_compressed = handler->create_ostream_auto(os, event);
o_stream_unref(&os);
const unsigned char *data;
if (is->stream_errno != 0)
printf("Error: %s\n", i_stream_get_error(is));
- i_assert(o_stream_finish(os_compressed) == 1);
+ int ret = o_stream_finish(os_compressed);
+ i_assert(ret != 0);
+ if (ret < 0)
+ i_fatal("%s", o_stream_get_error(os_compressed));
o_stream_unref(&os_compressed);
i_stream_unref(&is);
static void print_usage(const char *prog)
{
- fprintf(stderr, "Usage: %s block_size count level\n", prog);
- fprintf(stderr, "Runs with 1000 8k blocks using level 6 if nothing given\n");
+ fprintf(stderr, "Usage: %s [<block_size> [<count> [<compression settings>]]]\n", prog);
+ fprintf(stderr, "Runs with 1000 8k blocks if nothing given\n");
lib_exit(1);
}
int main(int argc, const char *argv[])
{
- unsigned int level = 6;
lib_init();
unsigned long block_size = 8192UL;
unsigned long block_count = 1000UL;
+ ARRAY_TYPE(const_string) set_array;
+ t_array_init(&set_array, 4);
+
if (argc >= 3) {
if (str_to_ulong(argv[1], &block_size) < 0 ||
str_to_ulong(argv[2], &block_count) < 0) {
fprintf(stderr, "Invalid parameters\n");
print_usage(argv[0]);
}
- if (argc == 4 &&
- str_to_uint(argv[3], &level) < 0) {
- fprintf(stderr, "Invalid parameters\n");
- print_usage(argv[0]);
+ while (argc > 3) {
+ const char *key, *value;
+ if (!t_split_key_value_eq(argv[3], &key, &value)) {
+ fprintf(stderr, "Invalid parameters\n");
+ print_usage(argv[0]);
+ }
+ array_push_back(&set_array, &key);
+ array_push_back(&set_array, &value);
+ argc--; argv++;
}
if (argc > 4) {
print_usage(argv[0]);
print_usage(argv[0]);
}
+ struct settings_simple set;
+ array_append_zero(&set_array);
+ settings_simple_init(&set, array_front(&set_array));
+
unsigned char buf[block_size];
printf("Input data is %lu blocks of %lu bytes\n\n", block_count, block_size);
for (unsigned int i = 0; compression_handlers[i].name != NULL; i++) T_BEGIN {
if (compression_handlers[i].create_istream != NULL &&
- compression_handlers[i].create_ostream != NULL) {
- bench_compression_speed(&compression_handlers[i], level,
- block_count);
+ compression_handlers[i].create_ostream_auto != NULL) {
+ bench_compression_speed(&compression_handlers[i],
+ set.event, block_count);
}
} T_END;
i_unlink("decompressed.bin");
i_unlink("compressed.bin");
+ settings_simple_deinit(&set);
lib_deinit();
}
#include "sha1.h"
#include "randgen.h"
#include "test-common.h"
+#include "settings.h"
#include "compression.h"
#include "iostream-lz4.h"
#include <unistd.h>
#include <fcntl.h>
+static struct settings_simple set;
+
static void test_compression_handler_detect(const struct compression_handler *handler)
{
const unsigned char test_data[] = {'h','e','l','l','o',' ',
buffer = buffer_create_dynamic(default_pool, 1024);
test_output = test_ostream_create(buffer);
- output = handler->create_ostream(test_output, 1);
+ output = handler->create_ostream_auto(test_output, set.event);
o_stream_unref(&test_output);
/* write data at once */
buffer = buffer_create_dynamic(default_pool, 1024);
test_output = test_ostream_create(buffer);
- output = handler->create_ostream(test_output, 1);
+ output = handler->create_ostream_auto(test_output, set.event);
o_stream_unref(&test_output);
/* write data at once */
handler->name, autodetect ? "yes" : "no"));
buffer = buffer_create_dynamic(default_pool, 128);
test_output = test_ostream_create(buffer);
- output = handler->create_ostream(test_output, 1);
+ output = handler->create_ostream_auto(test_output, set.event);
o_stream_unref(&test_output);
test_assert(o_stream_finish(output) == 1);
o_stream_unref(&output);
buffer = buffer_create_dynamic(default_pool, 1024);
test_output = test_ostream_create(buffer);
- output = handler->create_ostream(test_output, 1);
+ output = handler->create_ostream_auto(test_output, set.event);
o_stream_unref(&test_output);
/* write data at once */
buffer = buffer_create_dynamic(default_pool, 1024);
test_output = test_ostream_create(buffer);
- output = handler->create_ostream(test_output, 1);
+ output = handler->create_ostream_auto(test_output, set.event);
o_stream_unref(&test_output);
/* write data at once */
if (fd == -1)
i_fatal("creat(%s) failed: %m", path);
file_output = o_stream_create_fd_file(fd, 0, FALSE);
- output = handler->create_ostream(file_output, 1);
+ output = handler->create_ostream_auto(file_output, set.event);
sha1_init(&sha1);
/* 1) write lots of easily compressible data */
buffer_t *buffer = t_buffer_create(64);
buffer_t *compressed_data = t_buffer_create(256);
struct ostream *os = test_ostream_create_nonblocking(buffer, 64);
- struct ostream *os_compressed = handler->create_ostream(os, 9);
+ struct ostream *os_compressed =
+ handler->create_ostream_auto(os, set.event);
o_stream_unref(&os);
unsigned char input_buffer[64];
i_rand_minmax(1, 512));
/* Create compressor output stream */
- output2 = handler->create_ostream(output1, i_rand_minmax(1, 6));
+ output2 = handler->create_ostream_auto(output1, set.event);
/* Compress the data incrementally */
in_pos = out_pos = 0;
input = i_stream_create_from_data(randomness, RANDOMNESS_SIZE);
temp_output = iostream_temp_create(".temp.", 0);
- output = handler->create_ostream(temp_output, i_rand_minmax(1, 6));
+ output = handler->create_ostream_auto(temp_output, set.event);
switch (o_stream_send_istream(output, input)) {
case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
buffer_t *odata = buffer_create_dynamic(pool_datastack_create(), 65535);
unsigned char buf[IO_BLOCK_SIZE];
struct ostream *os = test_ostream_create(odata);
- struct ostream *output = handler->create_ostream(os, 1);
+ struct ostream *output = handler->create_ostream_auto(os, set.event);
o_stream_unref(&os);
for (unsigned int i = 0; i < 10; i++) {
for (i = 0; compression_handlers[i].name != NULL; i++) {
if (compression_handlers[i].create_istream != NULL &&
- compression_handlers[i].create_ostream != NULL &&
+ compression_handlers[i].create_ostream_auto != NULL &&
(!autodetect ||
compression_handlers[i].is_compressed != NULL)) T_BEGIN {
if (compression_handlers[i].is_compressed != NULL &&
buf_output = o_stream_create_buffer(buf);
o_stream_set_finish_via_child(buf_output, FALSE);
- output = gz->create_ostream(buf_output, 6);
+ output = gz->create_ostream_auto(buf_output, set.event);
o_stream_nsend_str(output, str1);
test_assert(o_stream_finish(output) > 0);
o_stream_destroy(&output);
if (str2[0] != '\0') {
- output = gz->create_ostream(buf_output, 6);
+ output = gz->create_ostream_auto(buf_output, set.event);
o_stream_nsend_str(output, "world");
test_assert(o_stream_finish(output) > 0);
o_stream_destroy(&output);
sha1_init(&sha1);
file_output = o_stream_create_fd_file(fd_out, 0, FALSE);
- output = handler->create_ostream(file_output, 1);
+ output = handler->create_ostream_auto(file_output, set.event);
input = i_stream_create_fd_autoclose(&fd_in, IO_BLOCK_SIZE);
while (i_stream_read_more(input, &data, &size) > 0) {
sha1_loop(&sha1, data, size);
test_end();
}
+static void test_compression_init(void)
+{
+ settings_simple_init(&set, NULL);
+}
+
+static void test_compression_deinit(void)
+{
+ settings_simple_deinit(&set);
+}
+
int main(int argc, char *argv[])
{
static void (*const test_functions[])(void) = {
+ test_compression_init,
test_compression,
test_istream_decompression_try,
test_gz_concat,
test_gz_large_header,
test_lz4_small_header,
test_compression_ext,
+ test_compression_deinit,
NULL
};
if (argc == 2) {
return 0;
}
if (argc == 3) {
+ lib_init();
+ settings_simple_init(&set, NULL);
test_compress_file(argv[1], argv[2]);
+ settings_simple_deinit(&set);
+ lib_deinit();
return 0;
}
return test_run(test_functions);