From 5f3f5efcabf800358edd2507230f0dbc3ecc059c Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 24 Jun 2020 18:06:50 +0200 Subject: csv-test nach csv-perf umbenannt --- csv-perf.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ csv-test.c | 139 ------------------------------------------------------------- makefile | 8 ++-- 3 files changed, 143 insertions(+), 143 deletions(-) create mode 100644 csv-perf.c delete mode 100644 csv-test.c diff --git a/csv-perf.c b/csv-perf.c new file mode 100644 index 0000000..efd2155 --- /dev/null +++ b/csv-perf.c @@ -0,0 +1,139 @@ +#include +#include +#include +#include +#include +#include + +#include "csv.h" + +static int +test(FILE *f) +{ + int line = 0; + csv_t csv = { 0 }; + + while ( csv_read(&csv, f) != -1 ) { + ++line; + } + + return line; +} + +static int +test_exception(FILE *f) +{ + int line = 0; + csv_t csv[1]; + + csv_init(csv); + for ( int n; (n = csv_read(csv, f)) != -1; ) { + if (++line == 500000) csv_field(csv, -1); + } + + return line; +} + +static void +my_signal_handler(int sig) +{ + if ( sig == SIGABRT ) { + fprintf(stderr, "my_signal_handler!\n"); + exit(1); + } +} + +static int +test_exception_with_signal(FILE *f) +{ + void (*old_handler)(int) = signal(SIGABRT, my_signal_handler); + + int line = 0; + csv_t csv[1]; + + csv_init(csv); + for ( int n; (n = csv_read(csv, f)) != -1; ) { + if (++line == 500000) csv_field(csv, -1); + } + + signal(SIGABRT, old_handler); + + return line; +} + +static void +my_error_handler(csv_err_t csv_err, void *env) +{ + assert(env != NULL); + + fprintf(stderr, "my_error_handler: %s\n", csv_err_str(csv_err)); + longjmp(*((jmp_buf *) env), 1); +} + +static int +test_exception_longjmp(FILE *f) +{ + /* start with default options */ + csv_options_t csv_options = csv_default_options; + + /* setup error handler */ + jmp_buf env; + csv_options.cb_error = my_error_handler; + csv_options.cb_error_arg = &env; + + /* setup csv-Object with custom options */ + csv_t csv[1]; + csv_init_opt(csv, &csv_options); + + if ( setjmp(env) == 0 ) { + int line = 0; + + for ( int n; (n = csv_read(csv, f)) != -1; ) { + // process data... + if (++line == 500000) csv_field(csv, -1); + } + return line; + } + else { + csv_cleanup(csv); + return -1; + } +} + +int +main(void) +{ +#if 0 + const char *names[] = { "Thomas \"DG\" Schmucker", "Andrea", "Markus", "Marion", NULL }; + + csv_write(stdout, -1, names, NULL); + csv_write(stdout, 0, names, NULL); + csv_write(stdout, 1, names, NULL); + csv_write(stdout, 2, names, NULL); + csv_write(stdout, 3, names, NULL); + csv_write(stdout, 4, names, NULL); + csv_write(stdout, 5, names, NULL); + return 0; +#endif + + FILE *in = fopen("500000 Records.csv", "r"); + if ( in == NULL ) { + fprintf(stderr, "failed to open testfile...\n"); + return EXIT_FAILURE; + } + + clock_t start = clock(); + + int lines = test(in); + (void) test_exception; + (void) test_exception_with_signal; + (void) test_exception_longjmp; + + clock_t end = clock(); + + fclose(in); + + printf("%d lines processed, duration: %.3lf sec\n", lines, ((double)(end - start)) / CLOCKS_PER_SEC); + + return EXIT_SUCCESS; +} diff --git a/csv-test.c b/csv-test.c deleted file mode 100644 index efd2155..0000000 --- a/csv-test.c +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "csv.h" - -static int -test(FILE *f) -{ - int line = 0; - csv_t csv = { 0 }; - - while ( csv_read(&csv, f) != -1 ) { - ++line; - } - - return line; -} - -static int -test_exception(FILE *f) -{ - int line = 0; - csv_t csv[1]; - - csv_init(csv); - for ( int n; (n = csv_read(csv, f)) != -1; ) { - if (++line == 500000) csv_field(csv, -1); - } - - return line; -} - -static void -my_signal_handler(int sig) -{ - if ( sig == SIGABRT ) { - fprintf(stderr, "my_signal_handler!\n"); - exit(1); - } -} - -static int -test_exception_with_signal(FILE *f) -{ - void (*old_handler)(int) = signal(SIGABRT, my_signal_handler); - - int line = 0; - csv_t csv[1]; - - csv_init(csv); - for ( int n; (n = csv_read(csv, f)) != -1; ) { - if (++line == 500000) csv_field(csv, -1); - } - - signal(SIGABRT, old_handler); - - return line; -} - -static void -my_error_handler(csv_err_t csv_err, void *env) -{ - assert(env != NULL); - - fprintf(stderr, "my_error_handler: %s\n", csv_err_str(csv_err)); - longjmp(*((jmp_buf *) env), 1); -} - -static int -test_exception_longjmp(FILE *f) -{ - /* start with default options */ - csv_options_t csv_options = csv_default_options; - - /* setup error handler */ - jmp_buf env; - csv_options.cb_error = my_error_handler; - csv_options.cb_error_arg = &env; - - /* setup csv-Object with custom options */ - csv_t csv[1]; - csv_init_opt(csv, &csv_options); - - if ( setjmp(env) == 0 ) { - int line = 0; - - for ( int n; (n = csv_read(csv, f)) != -1; ) { - // process data... - if (++line == 500000) csv_field(csv, -1); - } - return line; - } - else { - csv_cleanup(csv); - return -1; - } -} - -int -main(void) -{ -#if 0 - const char *names[] = { "Thomas \"DG\" Schmucker", "Andrea", "Markus", "Marion", NULL }; - - csv_write(stdout, -1, names, NULL); - csv_write(stdout, 0, names, NULL); - csv_write(stdout, 1, names, NULL); - csv_write(stdout, 2, names, NULL); - csv_write(stdout, 3, names, NULL); - csv_write(stdout, 4, names, NULL); - csv_write(stdout, 5, names, NULL); - return 0; -#endif - - FILE *in = fopen("500000 Records.csv", "r"); - if ( in == NULL ) { - fprintf(stderr, "failed to open testfile...\n"); - return EXIT_FAILURE; - } - - clock_t start = clock(); - - int lines = test(in); - (void) test_exception; - (void) test_exception_with_signal; - (void) test_exception_longjmp; - - clock_t end = clock(); - - fclose(in); - - printf("%d lines processed, duration: %.3lf sec\n", lines, ((double)(end - start)) / CLOCKS_PER_SEC); - - return EXIT_SUCCESS; -} diff --git a/makefile b/makefile index 4a29f19..d6e2f54 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,10 @@ -all: csv-test seq-read +all: csv-perf seq-read CC=cc CFLAGS=-Wall -Wextra -pedantic -std=c99 -O2 -csv-test: csv-test.c csv.o csv.h - $(CC) $(CFLAGS) -o $@ csv-test.c csv.o +csv-perf: csv-perf.c csv.o csv.h + $(CC) $(CFLAGS) -o $@ csv-perf.c csv.o seq-read: seq-read.c $(CC) $(CFLAGS) -o $@ $< @@ -13,7 +13,7 @@ csv.o: csv.c csv.h $(CC) $(CFLAGS) -DNDEBUG -o $@ -c $< clean: - rm csv-test seq-read csv.o + rm csv-perf seq-read csv.o .PHONY: all clean -- cgit v1.3