commit 5367410c209c168edd9784a524a64e425770cc1e
Author:     Laslo Hunhold <[email protected]>
AuthorDate: Sat Dec 25 20:36:43 2021 +0100
Commit:     Laslo Hunhold <[email protected]>
CommitDate: Sat Dec 25 20:36:43 2021 +0100

    Separate benchmark from test
    
    Some packagers like running tests on the final code, so it doesn't
    make sense to include benchmarks in the test code as this doesn't
    serve the correctness-check at all and only wastes time.
    
    Benchmarks only make sense during development to catch regressions.
    
    Signed-off-by: Laslo Hunhold <[email protected]>

diff --git a/Makefile b/Makefile
index 0f093e4..cb7657c 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,9 @@
 
 include config.mk
 
+BENCHMARK =\
+       benchmark/character\
+
 DATA =\
        data/emoji-data.txt\
        data/GraphemeBreakProperty.txt\
@@ -20,7 +23,6 @@ SRC =\
 
 TEST =\
        test/character\
-       test/character-performance\
        test/utf8-decode\
        test/utf8-encode\
 
@@ -34,22 +36,23 @@ MAN7 = man/libgrapheme.7
 
 all: libgrapheme.a libgrapheme.so
 
+benchmark/character.o: benchmark/character.c config.mk gen/character-test.h 
grapheme.h benchmark/util.h
+benchmark/util.o: benchmark/util.c config.mk benchmark/util.h
 gen/character-prop.o: gen/character-prop.c config.mk gen/util.h
 gen/character-test.o: gen/character-test.c config.mk gen/util.h
 gen/util.o: gen/util.c config.mk gen/util.h
 src/character.o: src/character.c config.mk gen/character-prop.h grapheme.h 
src/util.h
 src/utf8.o: src/utf8.c config.mk grapheme.h
-src/util.o: src/util.c config.mk grapheme.h src/util.h
+src/util.o: src/util.c config.mk gen/types.h grapheme.h src/util.h
 test/character.o: test/character.c config.mk gen/character-test.h grapheme.h 
test/util.h
-test/character-performance.o: test/character-performance.c config.mk 
gen/character-test.h grapheme.h test/util.h
 test/utf8-encode.o: test/utf8-encode.c config.mk grapheme.h test/util.h
 test/utf8-decode.o: test/utf8-decode.c config.mk grapheme.h test/util.h
 test/util.o: test/util.c config.mk test/util.h
 
+benchmark/character: benchmark/character.o benchmark/util.o libgrapheme.a
 gen/character-prop: gen/character-prop.o gen/util.o
 gen/character-test: gen/character-test.o gen/util.o
 test/character: test/character.o test/util.o libgrapheme.a
-test/character-performance: test/character-performance.o test/util.o 
libgrapheme.a
 test/utf8-encode: test/utf8-encode.o test/util.o libgrapheme.a
 test/utf8-decode: test/utf8-decode.o test/util.o libgrapheme.a
 
@@ -65,6 +68,9 @@ data/GraphemeBreakProperty.txt:
 data/GraphemeBreakTest.txt:
        wget -O $@ 
https://www.unicode.org/Public/14.0.0/ucd/auxiliary/GraphemeBreakTest.txt
 
+$(BENCHMARK):
+       $(CC) -o $@ $(LDFLAGS) [email protected] benchmark/util.o libgrapheme.a -lutf8proc
+
 $(GEN):
        $(CC) -o $@ $(LDFLAGS) [email protected] gen/util.o
 
@@ -84,6 +90,9 @@ libgrapheme.a: $(SRC:=.o)
 libgrapheme.so: $(SRC:=.o)
        $(CC) -o $@ -shared $?
 
+benchmark: $(BENCHMARK)
+       for m in $(BENCHMARK); do ./$$m; done
+
 test: $(TEST)
        for m in $(TEST); do ./$$m; done
 
@@ -108,17 +117,21 @@ uninstall:
        ldconfig || true
 
 clean:
-       rm -f $(GEN:=.h) $(GEN:=.o) gen/util.o $(GEN) $(SRC:=.o) src/util.o 
$(TEST:=.o) test/util.o $(TEST) libgrapheme.a libgrapheme.so
+       rm -f $(BENCHMARK:=.o) benchmark/util.o $(BENCHMARK) $(GEN:=.h) 
$(GEN:=.o) gen/util.o $(GEN) $(SRC:=.o) src/util.o $(TEST:=.o) test/util.o 
$(TEST) libgrapheme.a libgrapheme.so
 
 clean-data:
        rm -f $(DATA)
 
+print:
+       @echo $(PREFIX)
+
 dist:
        mkdir libgrapheme-$(VERSION)
-       for m in data gen man src test; do mkdir libgrapheme-$(VERSION)/$$m; 
done
+       for m in benchmark data gen man src test; do mkdir 
libgrapheme-$(VERSION)/$$m; done
        cp config.mk grapheme.h LICENSE Makefile README libgrapheme-$(VERSION)
+       cp $(BENCHMARK:=.c) benchmark/util.c benchmark/util.h 
libgrapheme-$(VERSION)/benchmark
        cp $(DATA) libgrapheme-$(VERSION)/data
-       cp $(GEN:=.c) gen/util.c gen/util.h libgrapheme-$(VERSION)/gen
+       cp $(GEN:=.c) gen/util.c gen/types.h gen/util.h 
libgrapheme-$(VERSION)/gen
        cp $(MAN3) $(MAN7) libgrapheme-$(VERSION)/man
        cp $(SRC:=.c) src/util.h libgrapheme-$(VERSION)/src
        cp $(TEST:=.c) test/util.c test/util.h libgrapheme-$(VERSION)/test
diff --git a/gen/types.h b/gen/types.h
new file mode 100644
index 0000000..83f7f41
--- /dev/null
+++ b/gen/types.h
@@ -0,0 +1,26 @@
+/* See LICENSE file for copyright and license details. */
+#ifndef TYPES_H
+#define TYPES_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+struct range {
+       uint_least32_t lower;
+       uint_least32_t upper;
+};
+
+struct range_list {
+       struct range *data;
+       size_t len;
+};
+
+struct test {
+       uint_least32_t *cp;
+       size_t cplen;
+       size_t *len;
+       size_t lenlen;
+       char *descr;
+};
+
+#endif /* TYPES_H */
diff --git a/gen/util.c b/gen/util.c
index 1836f70..4cbdadd 100644
--- a/gen/util.c
+++ b/gen/util.c
@@ -252,7 +252,7 @@ property_list_print(const struct property *prop, size_t 
numprops,
        size_t i, j;
 
        printf("/* Automatically generated by %s */\n"
-              "#include <stdint.h>\n\n#include \"../src/util.h\"\n\n",
+              "#include <stdint.h>\n\n#include \"../gen/types.h\"\n\n",
               progname);
 
        /* print enum */
@@ -393,11 +393,10 @@ segment_test_list_print(const struct segment_test *st, 
size_t numsegtests,
        size_t i, j;
 
        printf("/* Automatically generated by %s */\n"
-              "#include <stdint.h>\n#include <stddef.h>\n\n", progname);
+              "#include <stdint.h>\n#include <stddef.h>\n\n"
+              "#include \"../gen/types.h\"\n\n", progname);
 
-       printf("static const struct {\n\tuint_least32_t *cp;\n"
-              "\tsize_t cplen;\n\tsize_t *len;\n\tsize_t lenlen;\n"
-              "\tchar *descr;\n} %s[] = {\n", identifier);
+       printf("static const struct test %s[] = {\n", identifier);
        for (i = 0; i < numsegtests; i++) {
                printf("\t{\n");
 
diff --git a/src/util.c b/src/util.c
index 9b18fcb..60af166 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2,6 +2,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+#include "../gen/types.h"
 #include "../grapheme.h"
 #include "util.h"
 
diff --git a/src/util.h b/src/util.h
index f646071..1108025 100644
--- a/src/util.h
+++ b/src/util.h
@@ -5,20 +5,11 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "../gen/types.h"
 #include "../grapheme.h"
 
 #define LEN(x) (sizeof(x) / sizeof(*(x)))
 
-struct range {
-       uint_least32_t lower;
-       uint_least32_t upper;
-};
-
-struct range_list {
-       struct range *data;
-       size_t len;
-};
-
 int heisenstate_get(struct grapheme_internal_heisenstate *, int);
 int heisenstate_set(struct grapheme_internal_heisenstate *, int, int);
 
diff --git a/test/character-performance.c b/test/character-performance.c
deleted file mode 100644
index 27e6b7d..0000000
--- a/test/character-performance.c
+++ /dev/null
@@ -1,64 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include "../grapheme.h"
-#include "../gen/character-test.h"
-#include "util.h"
-
-#define NUM_ITERATIONS 1000
-
-int
-main(int argc, char *argv[])
-{
-       struct timespec start, end;
-       size_t i, j, bufsiz, off;
-       uint32_t *buf;
-       GRAPHEME_STATE state;
-       double cp_per_sec;
-
-       (void)argc;
-
-       /* allocate and generate buffer */
-       for (i = 0, bufsiz = 0; i < LEN(character_test); i++) {
-               bufsiz += character_test[i].cplen;
-       }
-       if (!(buf = calloc(bufsiz, sizeof(*buf)))) {
-               fprintf(stderr, "%s: calloc: Out of memory.\n", argv[0]);
-               return 1;
-       }
-       for (i = 0, off = 0; i < LEN(character_test); i++) {
-               for (j = 0; j < character_test[i].cplen; j++) {
-                       buf[off + j] = character_test[i].cp[j];
-               }
-               off += character_test[i].cplen;
-       }
-
-       /* run test */
-       printf("%s: Running benchmark ", argv[0]);
-       fflush(stdout);
-
-       clock_gettime(CLOCK_MONOTONIC, &start);
-       for (i = 0; i < NUM_ITERATIONS; i++) {
-               memset(&state, 0, sizeof(state));
-               for (j = 0; j < bufsiz - 1; j++) {
-                       (void)grapheme_is_character_break(buf[j], buf[j+1],
-                                                         &state);
-               }
-               if (i % (NUM_ITERATIONS / 10) == 0) {
-                       printf(".");
-                       fflush(stdout);
-               }
-       }
-       clock_gettime(CLOCK_MONOTONIC, &end);
-
-       cp_per_sec = (double)(NUM_ITERATIONS * bufsiz) /
-                    time_diff(&start, &end);
-
-       printf(" %.2e CP/s\n", cp_per_sec);
-
-       return 0;
-}
diff --git a/test/util.c b/test/util.c
index 5937468..c8f5a7d 100644
--- a/test/util.c
+++ b/test/util.c
@@ -2,10 +2,3 @@
 #include <time.h>
 
 #include "util.h"
-
-double
-time_diff(struct timespec *a, struct timespec *b)
-{
-       return (double)(b->tv_sec - a->tv_sec) +
-              (double)(b->tv_nsec - a->tv_nsec) * 1E-9;
-}
diff --git a/test/util.h b/test/util.h
index d89e25d..500e192 100644
--- a/test/util.h
+++ b/test/util.h
@@ -6,6 +6,4 @@
 
 #define LEN(x) (sizeof(x) / sizeof(*(x)))
 
-double time_diff(struct timespec *, struct timespec *);
-
 #endif /* UTIL_H */

Reply via email to