Debian Codegraph @

tests/bench/dns_name_fromwire.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
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17
18 #include <isc/ascii.h>
19 #include <isc/buffer.h>
20 #include <isc/random.h>
21 #include <isc/time.h>
22 #include <isc/util.h>
23
24 #include <dns/compress.h>
25 #include <dns/fixedname.h>
26 #include <dns/name.h>
27
28 #include "old.h"
29
30 static uint32_t
31 old_bench(const uint8_t *data, size_t size) {
32 isc_result_t result;
33 dns_fixedname_t fixed;
34 dns_name_t *name = dns_fixedname_initname(&fixed);
35 dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
36 isc_buffer_t buf;
37 uint32_t count = 0;
38
39 isc_buffer_constinit(&buf, data, size);
40 isc_buffer_add(&buf, size);
41 isc_buffer_setactive(&buf, size);
42
43 while (isc_buffer_consumedlength(&buf) < size) {
44 result = old_name_fromwire(name, &buf, dctx, 0, NULL);
45 if (result != ISC_R_SUCCESS) {
46 isc_buffer_forward(&buf, 1);
47 }
48 count++;
49 }
50 return count;
51 }
52
53 static uint32_t
54 new_bench(const uint8_t *data, size_t size) {
55 isc_result_t result;
56 dns_fixedname_t fixed;
57 dns_name_t *name = dns_fixedname_initname(&fixed);
58 dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
59 isc_buffer_t buf;
60 uint32_t count = 0;
61
62 isc_buffer_constinit(&buf, data, size);
63 isc_buffer_add(&buf, size);
64 isc_buffer_setactive(&buf, size);
65
66 while (isc_buffer_consumedlength(&buf) < size) {
67 result = dns_name_fromwire(name, &buf, dctx, NULL);
68 if (result != ISC_R_SUCCESS) {
69 isc_buffer_forward(&buf, 1);
70 }
71 count++;
72 }
73 return count;
74 }
75
76 static void
77 oldnew_bench(const uint8_t *data, size_t size) {
78 isc_time_t t0;
79 t0 = isc_time_now_hires();
80 uint32_t n1 = old_bench(data, size);
81 isc_time_t t1;
82 t1 = isc_time_now_hires();
83 uint32_t n2 = new_bench(data, size);
84 isc_time_t t2;
85 t2 = isc_time_now_hires();
86
87 double t01 = (double)isc_time_microdiff(&t1, &t0);
88 double t12 = (double)isc_time_microdiff(&t2, &t1);
89 printf(" old %u / %f ms; %f / us\n", n1, t01 / 1000.0, n1 / t01);
90 printf(" new %u / %f ms; %f / us\n", n2, t12 / 1000.0, n2 / t12);
91 printf(" old/new %f or %f\n", t01 / t12, t12 / t01);
92 }
93
94 #define NAMES 1000
95 static uint8_t buf[1024 * NAMES];
96
97 int
98 main(void) {
99 unsigned int p;
100
101 printf("random buffer\n");
102 isc_random_buf(buf, sizeof(buf));
103 oldnew_bench(buf, sizeof(buf));
104
105 p = 0;
106 for (unsigned int name = 0; name < NAMES; name++) {
107 unsigned int start = p;
108 unsigned int prev = p;
109 buf[p++] = 0;
110 for (unsigned int label = 0; label < 127; label++) {
111 unsigned int ptr = prev - start;
112 prev = p;
113 buf[p++] = 1;
114 buf[p++] = 'a';
115 buf[p++] = 0xC0 | (ptr >> 8);
116 buf[p++] = 0xFF & ptr;
117 }
118 }
119 printf("127 compression pointers\n");
120 oldnew_bench(buf, p);
121
122 p = 0;
123 for (unsigned int name = 0; name < NAMES; name++) {
124 for (unsigned int label = 0; label < 127; label++) {
125 buf[p++] = 1;
126 buf[p++] = 'a';
127 }
128 buf[p++] = 0;
129 }
130 printf("127 sequential labels\n");
131 oldnew_bench(buf, p);
132
133 p = 0;
134 for (unsigned int name = 0; name < NAMES; name++) {
135 for (unsigned int label = 0; label < 4; label++) {
136 buf[p++] = 62;
137 for (unsigned int c = 0; c < 62; c++) {
138 buf[p++] = 'a';
139 }
140 }
141 buf[p++] = 0;
142 }
143 printf("4 long sequential labels\n");
144 oldnew_bench(buf, p);
145 }