2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/tests/bench/siphash.c
Ondřej Surý f5c204ac3e
Move the library init and shutdown to executables
Instead of relying on unreliable order of execution of the library
constructors and destructors, move them to individual binaries.  The
advantage is that the execution time and order will remain constant and
will not depend on the dynamic load dependency solver.

This requires more work, but that was mitigated by a simple requirement,
any executable using libisc and libdns, must include <isc/lib.h> and
<dns/lib.h> respectively (in this particular order).  In turn, these two
headers must not be included from within any library as they contain
inlined functions marked with constructor/destructor attributes.
2025-02-22 16:19:00 +01:00

182 lines
4.4 KiB
C

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <isc/ascii.h>
#include <isc/lib.h>
#include <isc/random.h>
#include <isc/siphash.h>
#include <isc/time.h>
#define SIZE (1024 * 1024)
#define KILOHASHES(count, us) ((us) == 0 ? 0.0 : ((count) * 1000.0 / (us)))
int
main(void) {
static uint8_t bytes[SIZE];
static uint8_t key[16];
isc_random_buf(bytes, SIZE);
isc_random_buf(key, sizeof(key));
for (size_t len = 256; len > 0; len = len * 4 / 5) {
isc_time_t start, finish;
uint64_t count = 0;
uint64_t sum = 0;
uint64_t us;
start = isc_time_now_hires();
for (size_t end = len; end < SIZE; end += len) {
uint64_t hash;
uint8_t lower[1024];
isc_ascii_lowercopy(lower, bytes + end - len, len);
isc_siphash24(key, lower, len, true, (void *)&hash);
sum += hash;
count++;
}
finish = isc_time_now_hires();
us = isc_time_microdiff(&finish, &start);
printf("%f us wide-lower len %3zu, %7.0f kh/s (%llx)\n",
(double)us / 1000000.0, len, KILOHASHES(count, us),
(unsigned long long)sum);
}
for (size_t len = 256; len > 0; len = len * 4 / 5) {
isc_time_t start, finish;
uint64_t count = 0;
uint64_t sum = 0;
uint64_t us;
start = isc_time_now_hires();
for (size_t end = len; end < SIZE; end += len) {
uint64_t hash;
isc_siphash24(key, bytes + end - len, len, false,
(void *)&hash);
sum += hash;
count++;
}
finish = isc_time_now_hires();
us = isc_time_microdiff(&finish, &start);
printf("%f us wide-icase len %3zu, %7.0f kh/s (%llx)\n",
(double)us / 1000000.0, len, KILOHASHES(count, us),
(unsigned long long)sum);
}
for (size_t len = 256; len > 0; len = len * 4 / 5) {
isc_time_t start, finish;
uint64_t count = 0;
uint64_t sum = 0;
uint64_t us;
start = isc_time_now_hires();
for (size_t end = len; end < SIZE; end += len) {
uint64_t hash;
isc_siphash24(key, bytes + end - len, len, true,
(void *)&hash);
sum += hash;
count++;
}
finish = isc_time_now_hires();
us = isc_time_microdiff(&finish, &start);
printf("%f us wide-bytes len %3zu, %7.0f kh/s (%llx)\n",
(double)us / 1000000.0, len, KILOHASHES(count, us),
(unsigned long long)sum);
}
for (size_t len = 256; len > 0; len = len * 4 / 5) {
isc_time_t start, finish;
uint64_t count = 0;
uint64_t sum = 0;
uint64_t us;
start = isc_time_now_hires();
for (size_t end = len; end < SIZE; end += len) {
uint32_t hash;
uint8_t lower[1024];
isc_ascii_lowercopy(lower, bytes + end - len, len);
isc_halfsiphash24(key, lower, len, true, (void *)&hash);
sum += hash;
count++;
}
finish = isc_time_now_hires();
us = isc_time_microdiff(&finish, &start);
printf("%f us half-lower len %3zu, %7.0f kh/s (%llx)\n",
(double)us / 1000000.0, len, KILOHASHES(count, us),
(unsigned long long)sum);
}
for (size_t len = 256; len > 0; len = len * 4 / 5) {
isc_time_t start, finish;
uint64_t count = 0;
uint64_t sum = 0;
uint64_t us;
start = isc_time_now_hires();
for (size_t end = len; end < SIZE; end += len) {
uint32_t hash;
isc_halfsiphash24(key, bytes + end - len, len, false,
(void *)&hash);
sum += hash;
count++;
}
finish = isc_time_now_hires();
us = isc_time_microdiff(&finish, &start);
printf("%f us half-icase len %3zu, %7.0f kh/s (%llx)\n",
(double)us / 1000000.0, len, KILOHASHES(count, us),
(unsigned long long)sum);
}
for (size_t len = 256; len > 0; len = len * 4 / 5) {
isc_time_t start, finish;
uint64_t count = 0;
uint64_t sum = 0;
uint64_t us;
start = isc_time_now_hires();
for (size_t end = len; end < SIZE; end += len) {
uint32_t hash;
isc_halfsiphash24(key, bytes + end - len, len, true,
(void *)&hash);
sum += hash;
count++;
}
finish = isc_time_now_hires();
us = isc_time_microdiff(&finish, &start);
printf("%f us half-bytes len %3zu, %7.0f kh/s (%llx)\n",
(double)us / 1000000.0, len, KILOHASHES(count, us),
(unsigned long long)sum);
}
}