Add "repeat" and "verbose" flags to "bro" tool.

This commit is contained in:
Eugene Klyuchnikov 2015-09-01 13:44:17 +02:00
parent b58317a652
commit a67f0056f2
1 changed files with 77 additions and 25 deletions

View File

@ -44,10 +44,14 @@ static void ParseArgv(int argc, char **argv,
char **output_path,
int *force,
int *quality,
int *decompress) {
int *decompress,
int *repeat,
int *verbose) {
*force = 0;
*input_path = 0;
*output_path = 0;
*repeat = 1;
*verbose = 0;
{
size_t argv0_len = strlen(argv[0]);
*decompress =
@ -66,6 +70,13 @@ static void ParseArgv(int argc, char **argv,
!strcmp("-d", argv[k])) {
*decompress = 1;
continue;
} else if (!strcmp("--verbose", argv[k]) ||
!strcmp("-v", argv[k])) {
if (*verbose != 0) {
goto error;
}
*verbose = 1;
continue;
}
if (k < argc - 1) {
if (!strcmp("--input", argv[k]) ||
@ -93,6 +104,13 @@ static void ParseArgv(int argc, char **argv,
}
++k;
continue;
} else if (!strcmp("--repeat", argv[k]) ||
!strcmp("-r", argv[k])) {
if (!ParseQuality(argv[k + 1], repeat)) {
goto error;
}
++k;
continue;
}
}
goto error;
@ -101,7 +119,8 @@ static void ParseArgv(int argc, char **argv,
error:
fprintf(stderr,
"Usage: %s [--force] [--quality n] [--decompress]"
" [--input filename] [--output filename]\n",
" [--input filename] [--output filename] [--repeat iters]"
" [--verbose]\n",
argv[0]);
exit(1);
}
@ -138,41 +157,74 @@ static FILE *OpenOutputFile(const char *output_path, const int force) {
return fdopen(fd, "wb");
}
int64_t FileSize(char *path) {
FILE *f = fopen(path, "rb");
if (f == NULL) {
return -1;
}
fseek(f, 0L, SEEK_END);
int64_t retval = ftell(f);
fclose(f);
return retval;
}
int main(int argc, char** argv) {
char *input_path = 0;
char *output_path = 0;
int force = 0;
int quality = 11;
int decompress = 0;
int repeat = 1;
int verbose = 0;
ParseArgv(argc, argv, &input_path, &output_path, &force,
&quality, &decompress);
FILE* fin = OpenInputFile(input_path);
FILE* fout = OpenOutputFile(output_path, force);
if (decompress) {
BrotliInput in = BrotliFileInput(fin);
BrotliOutput out = BrotliFileOutput(fout);
if (!BrotliDecompress(in, out)) {
fprintf(stderr, "corrupt input\n");
&quality, &decompress, &repeat, &verbose);
const clock_t clock_start = clock();
for (int i = 0; i < repeat; ++i) {
FILE* fin = OpenInputFile(input_path);
FILE* fout = OpenOutputFile(output_path, force);
if (decompress) {
BrotliInput in = BrotliFileInput(fin);
BrotliOutput out = BrotliFileOutput(fout);
if (!BrotliDecompress(in, out)) {
fprintf(stderr, "corrupt input\n");
exit(1);
}
} else {
brotli::BrotliParams params;
params.quality = quality;
brotli::BrotliFileIn in(fin, 1 << 16);
brotli::BrotliFileOut out(fout);
if (!BrotliCompress(params, &in, &out)) {
fprintf(stderr, "compression failed\n");
unlink(output_path);
exit(1);
}
}
if (fclose(fin) != 0) {
perror("fclose");
exit(1);
}
} else {
brotli::BrotliParams params;
params.quality = quality;
brotli::BrotliFileIn in(fin, 1 << 16);
brotli::BrotliFileOut out(fout);
if (!BrotliCompress(params, &in, &out)) {
fprintf(stderr, "compression failed\n");
unlink(output_path);
if (fclose(fout) != 0) {
perror("fclose");
exit(1);
}
}
if (fclose(fin) != 0) {
perror("fclose");
exit(1);
}
if (fclose(fout) != 0) {
perror("fclose");
exit(1);
if (verbose) {
const clock_t clock_end = clock();
double duration =
static_cast<double>(clock_end - clock_start) / CLOCKS_PER_SEC;
if (duration < 1e-9) {
duration = 1e-9;
}
int64_t uncompressed_bytes = repeat *
FileSize(decompress ? output_path : input_path);
double uncompressed_bytes_in_MB = uncompressed_bytes / (1024.0 * 1024.0);
if (decompress) {
printf("Brotli decompression speed: ");
} else {
printf("Brotli compression speed: ");
}
printf("%g MB/s\n", uncompressed_bytes_in_MB / duration);
}
return 0;
}