Blob


1 /* See LICENSE file for copyright and license details. */
2 #include <math.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <time.h>
7 #include "../gen/types.h"
8 #include "../grapheme.h"
9 #include "util.h"
11 uint_least32_t *
12 generate_cp_test_buffer(const struct break_test *test, size_t testlen,
13 size_t *buflen)
14 {
15 size_t i, j, off;
16 uint_least32_t *buf;
18 /* allocate and generate buffer */
19 for (i = 0, *buflen = 0; i < testlen; i++) {
20 *buflen += test[i].cplen;
21 }
22 if (!(buf = calloc(*buflen, sizeof(*buf)))) {
23 fprintf(stderr, "generate_test_buffer: calloc: Out of memory.\n");
24 exit(1);
25 }
26 for (i = 0, off = 0; i < testlen; i++) {
27 for (j = 0; j < test[i].cplen; j++) {
28 buf[off + j] = test[i].cp[j];
29 }
30 off += test[i].cplen;
31 }
33 return buf;
34 }
36 char *
37 generate_utf8_test_buffer(const struct break_test *test, size_t testlen,
38 size_t *buflen)
39 {
40 size_t i, j, off, ret;
41 char *buf;
43 /* allocate and generate buffer */
44 for (i = 0, *buflen = 0; i < testlen; i++) {
45 for (j = 0; j < test[i].cplen; j++) {
46 *buflen += grapheme_encode_utf8(test[i].cp[j], NULL, 0);
47 }
48 }
49 (*buflen)++; /* terminating NUL-byte */
50 if (!(buf = malloc(*buflen))) {
51 fprintf(stderr, "generate_test_buffer: malloc: Out of memory.\n");
52 exit(1);
53 }
54 for (i = 0, off = 0; i < testlen; i++) {
55 for (j = 0; j < test[i].cplen; j++, off += ret) {
56 if ((ret = grapheme_encode_utf8(test[i].cp[j],
57 buf + off,
58 *buflen - off)) >
59 (*buflen - off)) {
60 /* shouldn't happen */
61 fprintf(stderr, "generate_utf8_test_buffer: "
62 "Buffer too small.\n");
63 exit(1);
64 }
65 }
66 }
67 buf[*buflen - 1] = '\0';
69 return buf;
70 }
72 static double
73 time_diff(struct timespec *a, struct timespec *b)
74 {
75 return (double)(b->tv_sec - a->tv_sec) +
76 (double)(b->tv_nsec - a->tv_nsec) * 1E-9;
77 }
79 void
80 run_benchmark(void (*func)(const void *), const void *payload,
81 const char *name, const char *comment, const char *unit,
82 double *baseline, size_t num_iterations,
83 size_t units_per_iteration)
84 {
85 struct timespec start, end;
86 size_t i;
87 double diff;
89 printf("\t%s ", name);
90 fflush(stdout);
92 clock_gettime(CLOCK_MONOTONIC, &start);
93 for (i = 0; i < num_iterations; i++) {
94 func(payload);
96 if (i % (num_iterations / 10) == 0) {
97 printf(".");
98 fflush(stdout);
99 }
101 clock_gettime(CLOCK_MONOTONIC, &end);
102 diff = time_diff(&start, &end) / (double)num_iterations /
103 (double)units_per_iteration;
105 if (isnan(*baseline)) {
106 *baseline = diff;
107 printf(" avg. %.3es/%s (baseline)\n", diff, unit);
108 } else {
109 printf(" avg. %.3es/%s (%.2f%% %s%s%s)\n", diff, unit,
110 fabs(1.0 - diff / *baseline) * 100,
111 (diff < *baseline) ? "faster" : "slower",
112 comment ? ", " : "",
113 comment ? comment : "");