2015-07-10 17:37:05 -04:00
|
|
|
#include <switch.h>
|
2018-10-12 20:33:08 -04:00
|
|
|
#include <test/switch_test.h>
|
2015-07-10 17:37:05 -04:00
|
|
|
|
|
|
|
// #define BENCHMARK 1
|
|
|
|
|
2019-11-21 16:14:12 -05:00
|
|
|
FST_MINCORE_BEGIN("./conf")
|
2015-07-10 17:37:05 -04:00
|
|
|
|
2018-10-12 20:33:08 -04:00
|
|
|
FST_SUITE_BEGIN(switch_hash)
|
|
|
|
|
|
|
|
FST_SETUP_BEGIN()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
FST_SETUP_END()
|
|
|
|
|
|
|
|
FST_TEARDOWN_BEGIN()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
FST_TEARDOWN_END()
|
|
|
|
|
|
|
|
FST_TEST_BEGIN(benchmark)
|
|
|
|
{
|
2015-07-10 17:37:05 -04:00
|
|
|
switch_time_t start_ts, end_ts;
|
2019-01-17 02:38:13 -05:00
|
|
|
uint64_t micro_total = 0;
|
2015-07-10 17:37:05 -04:00
|
|
|
double micro_per = 0;
|
|
|
|
double rate_per_sec = 0;
|
2015-09-28 16:06:15 -04:00
|
|
|
int x = 0;
|
|
|
|
|
2015-07-10 17:37:05 -04:00
|
|
|
#ifdef BENCHMARK
|
|
|
|
switch_time_t small_start_ts, small_end_ts;
|
|
|
|
#endif
|
|
|
|
|
2021-02-23 12:59:26 -05:00
|
|
|
int loops = 10;
|
2015-07-10 17:37:05 -04:00
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
|
|
|
char **index = NULL;
|
|
|
|
switch_hash_t *hash = NULL;
|
|
|
|
|
2018-10-12 20:33:08 -04:00
|
|
|
fst_requires(switch_core_hash_init(&hash) == SWITCH_STATUS_SUCCESS);
|
2015-07-10 17:37:05 -04:00
|
|
|
|
|
|
|
index = calloc(loops, sizeof(char *));
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
index[x] = switch_mprintf("%d", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* START LOOPS */
|
|
|
|
start_ts = switch_time_now();
|
|
|
|
|
|
|
|
/* Insertion */
|
|
|
|
#ifndef BENCHMARK
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
status = switch_core_hash_insert(hash, index[x], (void *) index[x]);
|
2018-10-12 20:33:08 -04:00
|
|
|
fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Failed to insert into the hash");
|
2015-07-10 17:37:05 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
small_start_ts = switch_time_now();
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
switch_core_hash_insert(hash, index[x], (void *) index[x]);
|
|
|
|
}
|
|
|
|
small_end_ts = switch_time_now();
|
|
|
|
|
|
|
|
micro_total = small_end_ts - small_start_ts;
|
|
|
|
micro_per = micro_total / (double) loops;
|
|
|
|
rate_per_sec = 1000000 / micro_per;
|
2018-10-12 20:33:08 -04:00
|
|
|
printf("switch_hash insert: Total %ldus / %ld loops, %.2f us per loop, %.0f loops per second\n",
|
2015-07-10 17:37:05 -04:00
|
|
|
micro_total, loops, micro_per, rate_per_sec);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Lookup */
|
|
|
|
#ifndef BENCHMARK
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
char *data = NULL;
|
|
|
|
data = switch_core_hash_find(hash, index[x]);
|
2018-10-12 20:33:08 -04:00
|
|
|
fst_xcheck(data != NULL, "Lookup failed");
|
|
|
|
fst_check_string_equals( index[x], data);
|
2015-07-10 17:37:05 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
small_start_ts = switch_time_now();
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
if ( ! switch_core_hash_find(hash, index[x])) {
|
2018-10-12 20:33:08 -04:00
|
|
|
fst_fail("Failed to properly locate one of the values");
|
2015-07-10 17:37:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
small_end_ts = switch_time_now();
|
|
|
|
|
|
|
|
micro_total = small_end_ts - small_start_ts;
|
|
|
|
micro_per = micro_total / (double) loops;
|
|
|
|
rate_per_sec = 1000000 / micro_per;
|
2018-10-12 20:33:08 -04:00
|
|
|
printf("switch_hash find: Total %ldus / %ld loops, %.2f us per loop, %.0f loops per second\n",
|
2015-07-10 17:37:05 -04:00
|
|
|
micro_total, loops, micro_per, rate_per_sec);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Delete */
|
|
|
|
#ifndef BENCHMARK
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
char *data = NULL;
|
|
|
|
data = switch_core_hash_delete(hash, index[x]);
|
2018-10-12 20:33:08 -04:00
|
|
|
fst_xcheck(data != NULL, "Delete from the hash");
|
|
|
|
fst_check_string_equals( index[x], data );
|
2015-07-10 17:37:05 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
small_start_ts = switch_time_now();
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
if ( !switch_core_hash_delete(hash, index[x])) {
|
2018-10-12 20:33:08 -04:00
|
|
|
fst_fail("Failed to delete and return the value");
|
2015-07-10 17:37:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
small_end_ts = switch_time_now();
|
|
|
|
|
|
|
|
micro_total = small_end_ts - small_start_ts;
|
|
|
|
micro_per = micro_total / (double) loops;
|
|
|
|
rate_per_sec = 1000000 / micro_per;
|
2018-10-12 20:33:08 -04:00
|
|
|
printf("switch_hash delete: Total %ldus / %d loops, %.2f us per loop, %.0f loops per second\n",
|
2015-07-10 17:37:05 -04:00
|
|
|
micro_total, loops, micro_per, rate_per_sec);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
end_ts = switch_time_now();
|
|
|
|
/* END LOOPS */
|
|
|
|
|
|
|
|
switch_core_hash_destroy(&hash);
|
2015-09-28 16:06:15 -04:00
|
|
|
for ( x = 0; x < loops; x++) {
|
2015-07-10 17:37:05 -04:00
|
|
|
free(index[x]);
|
|
|
|
}
|
|
|
|
free(index);
|
|
|
|
|
|
|
|
micro_total = end_ts - start_ts;
|
|
|
|
micro_per = micro_total / (double) loops;
|
|
|
|
rate_per_sec = 1000000 / micro_per;
|
2019-01-17 02:38:13 -05:00
|
|
|
printf("switch_hash Total %" SWITCH_UINT64_T_FMT "us / %d loops, %.2f us per loop, %.0f loops per second\n",
|
2015-07-10 17:37:05 -04:00
|
|
|
micro_total, loops, micro_per, rate_per_sec);
|
2018-10-12 20:33:08 -04:00
|
|
|
}
|
|
|
|
FST_TEST_END()
|
2015-07-10 17:37:05 -04:00
|
|
|
|
2018-10-12 20:33:08 -04:00
|
|
|
FST_SUITE_END()
|
|
|
|
|
|
|
|
FST_MINCORE_END()
|
2015-07-10 17:37:05 -04:00
|
|
|
|