Debian Codegraph @

tests/bench/compress.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13 #include <err.h>
14 #include <errno.h>
15 #include <stdbool.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include <isc/buffer.h>
21 #include <isc/mem.h>
22 #include <isc/result.h>
23 #include <isc/time.h>
24 #include <isc/util.h>
25
26 #include <dns/compress.h>
27 #include <dns/fixedname.h>
28 #include <dns/name.h>
29
30 static void
31 CHECKRESULT(isc_result_t result, const char *msg) {
32 if (result != ISC_R_SUCCESS) {
33 printf("%s: %s\n", msg, isc_result_totext(result));
34 exit(EXIT_FAILURE);
35 }
36 }
37
38 int
39 main(void) {
40 isc_result_t result;
41 isc_buffer_t buf;
42
43 isc_mem_t *mctx = NULL;
44 isc_mem_create(&mctx);
45
46 static dns_fixedname_t fixedname[65536];
47 unsigned int count = 0;
48
49 char *line = NULL;
50 size_t linecap = 0;
51 ssize_t linelen;
52 while ((linelen = getline(&line, &linecap, stdin)) > 0) {
53 if (line[linelen - 1] == '\n') {
54 line[--linelen] = '\0';
55 }
56 isc_buffer_init(&buf, line, linelen);
57 isc_buffer_add(&buf, linelen);
58
59 if (count == ARRAY_SIZE(fixedname)) {
60 errx(1, "too many names");
61 }
62 dns_name_t *name = dns_fixedname_initname(&fixedname[count++]);
63 result = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
64 CHECKRESULT(result, line);
65 }
66
67 unsigned int repeat = 100;
68
69 isc_time_t start;
70 start = isc_time_now_hires();
71
72 for (unsigned int n = 0; n < repeat; n++) {
73 static uint8_t wire[4 * 1024];
74 dns_compress_t cctx;
75
76 isc_buffer_init(&buf, wire, sizeof(wire));
77 dns_compress_init(&cctx, mctx, 0);
78
79 for (unsigned int i = 0; i < count; i++) {
80 dns_name_t *name = dns_fixedname_name(&fixedname[i]);
81 result = dns_name_towire(name, &cctx, &buf, NULL);
82 if (result == ISC_R_NOSPACE) {
83 dns_compress_invalidate(&cctx);
84 dns_compress_init(&cctx, mctx, 0);
85 isc_buffer_init(&buf, wire, sizeof(wire));
86 } else {
87 CHECKRESULT(result, "dns_name_towire");
88 }
89 }
90 dns_compress_invalidate(&cctx);
91 }
92
93 isc_time_t finish;
94 finish = isc_time_now_hires();
95
96 uint64_t microseconds = isc_time_microdiff(&finish, &start);
97 printf("time %f / %u\n", (double)microseconds / 1000000.0, repeat);
98
99 printf("names %u\n", count);
100
101 isc_mem_destroy(&mctx);
102
103 return 0;
104 }