2015-07-10 17:37:05 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <switch.h>
|
|
|
|
#include <tap.h>
|
|
|
|
|
|
|
|
// #define BENCHMARK 1
|
|
|
|
|
|
|
|
int main () {
|
|
|
|
|
|
|
|
switch_event_t *event = NULL;
|
|
|
|
switch_bool_t verbose = SWITCH_TRUE;
|
|
|
|
const char *err = NULL;
|
|
|
|
switch_time_t start_ts, end_ts;
|
|
|
|
unsigned long long micro_total = 0;
|
|
|
|
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
|
|
|
|
|
|
|
|
int rc = 0, loops = 10;
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
|
|
|
char **index = NULL;
|
|
|
|
switch_hash_t *hash = NULL;
|
|
|
|
|
|
|
|
#ifndef BENCHMARK
|
|
|
|
plan(2 + ( 5 * loops));
|
|
|
|
#else
|
|
|
|
plan(2);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
status = switch_core_init(SCF_MINIMAL, verbose, &err);
|
|
|
|
|
|
|
|
if ( !ok( status == SWITCH_STATUS_SUCCESS, "Initialize FreeSWITCH core\n")) {
|
|
|
|
bail_out(0, "Bail due to failure to initialize FreeSWITCH[%s]", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = switch_core_hash_init(&hash);
|
|
|
|
|
|
|
|
if ( !ok(status == SWITCH_STATUS_SUCCESS, "Create a new hash")) {
|
|
|
|
bail_out(0, "Bail due to failure to create hash");
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
ok(status == SWITCH_STATUS_SUCCESS, "Insert into the hash");
|
|
|
|
}
|
|
|
|
#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;
|
|
|
|
note("switch_hash insert: Total %ldus / %ld loops, %.2f us per loop, %.0f loops per second\n",
|
|
|
|
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]);
|
|
|
|
ok(data != NULL, "Successful lookup");
|
|
|
|
is( index[x], data, "Returned correct data");
|
|
|
|
}
|
|
|
|
#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])) {
|
|
|
|
fail("Failed to properly locate one of the values");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
note("switch_hash find: Total %ldus / %ld loops, %.2f us per loop, %.0f loops per second\n",
|
|
|
|
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]);
|
|
|
|
ok(data != NULL, "Create a new hash");
|
|
|
|
is( index[x], data, "Returned correct data");
|
|
|
|
}
|
|
|
|
#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])) {
|
|
|
|
fail("Failed to delete and return the value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
note("switch_hash delete: Total %ldus / %d loops, %.2f us per loop, %.0f loops per second\n",
|
|
|
|
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;
|
2015-10-07 13:35:28 -04:00
|
|
|
diag("switch_hash 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);
|
|
|
|
|
|
|
|
switch_core_destroy();
|
|
|
|
|
|
|
|
done_testing();
|
|
|
|
}
|