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>
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"
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)
15 3448adb0 2022-11-02 op size_t i, j, off;
16 3448adb0 2022-11-02 op uint_least32_t *buf;
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;
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");
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];
30 3448adb0 2022-11-02 op off += test[i].cplen;
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)
40 3448adb0 2022-11-02 op size_t i, j, off, ret;
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);
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");
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],
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");
67 3448adb0 2022-11-02 op buf[*buflen - 1] = '\0';
73 3448adb0 2022-11-02 op time_diff(struct timespec *a, struct timespec *b)
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;
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)
85 3448adb0 2022-11-02 op struct timespec start, end;
89 3448adb0 2022-11-02 op printf("\t%s ", name);
90 3448adb0 2022-11-02 op fflush(stdout);
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);
96 3448adb0 2022-11-02 op if (i % (num_iterations / 10) == 0) {
98 3448adb0 2022-11-02 op fflush(stdout);
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;
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);
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 : "");