Home | History | Annotate | Line # | Download | only in bench
      1 /*	$NetBSD: dns_name_fromwire.c,v 1.2 2025/01/26 16:25:47 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #include <stddef.h>
     17 #include <stdlib.h>
     18 #include <unistd.h>
     19 
     20 #include <isc/ascii.h>
     21 #include <isc/buffer.h>
     22 #include <isc/random.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 #include "old.h"
     31 
     32 static uint32_t
     33 old_bench(const uint8_t *data, size_t size) {
     34 	isc_result_t result;
     35 	dns_fixedname_t fixed;
     36 	dns_name_t *name = dns_fixedname_initname(&fixed);
     37 	dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
     38 	isc_buffer_t buf;
     39 	uint32_t count = 0;
     40 
     41 	isc_buffer_constinit(&buf, data, size);
     42 	isc_buffer_add(&buf, size);
     43 	isc_buffer_setactive(&buf, size);
     44 
     45 	while (isc_buffer_consumedlength(&buf) < size) {
     46 		result = old_name_fromwire(name, &buf, dctx, 0, NULL);
     47 		if (result != ISC_R_SUCCESS) {
     48 			isc_buffer_forward(&buf, 1);
     49 		}
     50 		count++;
     51 	}
     52 	return count;
     53 }
     54 
     55 static uint32_t
     56 new_bench(const uint8_t *data, size_t size) {
     57 	isc_result_t result;
     58 	dns_fixedname_t fixed;
     59 	dns_name_t *name = dns_fixedname_initname(&fixed);
     60 	dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
     61 	isc_buffer_t buf;
     62 	uint32_t count = 0;
     63 
     64 	isc_buffer_constinit(&buf, data, size);
     65 	isc_buffer_add(&buf, size);
     66 	isc_buffer_setactive(&buf, size);
     67 
     68 	while (isc_buffer_consumedlength(&buf) < size) {
     69 		result = dns_name_fromwire(name, &buf, dctx, NULL);
     70 		if (result != ISC_R_SUCCESS) {
     71 			isc_buffer_forward(&buf, 1);
     72 		}
     73 		count++;
     74 	}
     75 	return count;
     76 }
     77 
     78 static void
     79 oldnew_bench(const uint8_t *data, size_t size) {
     80 	isc_time_t t0;
     81 	t0 = isc_time_now_hires();
     82 	uint32_t n1 = old_bench(data, size);
     83 	isc_time_t t1;
     84 	t1 = isc_time_now_hires();
     85 	uint32_t n2 = new_bench(data, size);
     86 	isc_time_t t2;
     87 	t2 = isc_time_now_hires();
     88 
     89 	double t01 = (double)isc_time_microdiff(&t1, &t0);
     90 	double t12 = (double)isc_time_microdiff(&t2, &t1);
     91 	printf("  old %u / %f ms; %f / us\n", n1, t01 / 1000.0, n1 / t01);
     92 	printf("  new %u / %f ms; %f / us\n", n2, t12 / 1000.0, n2 / t12);
     93 	printf("  old/new %f or %f\n", t01 / t12, t12 / t01);
     94 }
     95 
     96 #define NAMES 1000
     97 static uint8_t buf[1024 * NAMES];
     98 
     99 int
    100 main(void) {
    101 	unsigned int p;
    102 
    103 	printf("random buffer\n");
    104 	isc_random_buf(buf, sizeof(buf));
    105 	oldnew_bench(buf, sizeof(buf));
    106 
    107 	p = 0;
    108 	for (unsigned int name = 0; name < NAMES; name++) {
    109 		unsigned int start = p;
    110 		unsigned int prev = p;
    111 		buf[p++] = 0;
    112 		for (unsigned int label = 0; label < 127; label++) {
    113 			unsigned int ptr = prev - start;
    114 			prev = p;
    115 			buf[p++] = 1;
    116 			buf[p++] = 'a';
    117 			buf[p++] = 0xC0 | (ptr >> 8);
    118 			buf[p++] = 0xFF & ptr;
    119 		}
    120 	}
    121 	printf("127 compression pointers\n");
    122 	oldnew_bench(buf, p);
    123 
    124 	p = 0;
    125 	for (unsigned int name = 0; name < NAMES; name++) {
    126 		for (unsigned int label = 0; label < 127; label++) {
    127 			buf[p++] = 1;
    128 			buf[p++] = 'a';
    129 		}
    130 		buf[p++] = 0;
    131 	}
    132 	printf("127 sequential labels\n");
    133 	oldnew_bench(buf, p);
    134 
    135 	p = 0;
    136 	for (unsigned int name = 0; name < NAMES; name++) {
    137 		for (unsigned int label = 0; label < 4; label++) {
    138 			buf[p++] = 62;
    139 			for (unsigned int c = 0; c < 62; c++) {
    140 				buf[p++] = 'a';
    141 			}
    142 		}
    143 		buf[p++] = 0;
    144 	}
    145 	printf("4 long sequential labels\n");
    146 	oldnew_bench(buf, p);
    147 }
    148