Blame


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