/* * Test program for id.c */ #include #include "id.h" struct id my_id; main() { int i, n; void *v; id_init(&my_id, 10000); random_test(); } #define LIST_SZ 50000 struct id_list { int id; int ptr; } id_list[LIST_SZ]; int list_cnt; int new_cnt; int max = 5000; int count = 10000000; long long avr_depth; #define rdtsc(low,high) \ __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high)) static inline unsigned long get_tsc(void) { register unsigned long eax, edx; rdtsc(eax,edx); return(eax); } random_test() { int n, i; void *v; unsigned long t1, t2, t3, t4; for (n = 0; n < count; n++) { /* * favor insertion so we will tend to run will max * id's active. */ if (list_cnt && (list_cnt > max || rand() < (RAND_MAX/4))) { i = rand() % list_cnt; v = id_lookup(&my_id, id_list[i].id); if ((int)v != id_list[i].ptr) { printf("list_cnt=%d, i=%d\n", list_cnt, i); printf("failed id=%d, expected %d got %d\n", id_list[i].id, id_list[i].ptr, (int)v); } else { #if 0 printf("rm id=%d, ptr=%d\n", id_list[i].id, id_list[i].ptr); #endif id_remove(&my_id, id_list[i].id); } id_list[i] = id_list[--list_cnt]; } else { new_cnt++; id_list[list_cnt].id = id_new(&my_id, (void *)new_cnt); id_list[list_cnt].ptr = new_cnt; #if 0 printf("ins id=%d, ptr=%d\n", id_list[list_cnt].id, id_list[list_cnt].ptr); #endif list_cnt++; avr_depth += list_cnt; } } t1 = get_tsc(); id_lookup(&my_id, id_list[0].id); t2 = get_tsc(); n = id_new(&my_id, (void *)++new_cnt); t3 = get_tsc(); id_remove(&my_id, n); t4 = get_tsc(); for (i = 0; i < list_cnt; i++) { v = id_lookup(&my_id, id_list[i].id); if ((int)v != id_list[i].ptr) { printf("failed id=%d, expected %d got %d\n", id_list[i].id, id_list[i], (int)v); } id_remove(&my_id, id_list[i].id); } printf("my_id.count=%d\n", my_id.count); printf("new_cnt=%d\n", new_cnt); printf("avr_depth = %d\n", (int)(avr_depth/new_cnt)); printf("id_lookup took %d cycles\n", t2-t1); printf("id_new took %d cycles\n", t3-t2); printf("id_remove took %d cycles\n", t4-t3); }