/* ////////////////////////////////////////////////////////////////////////////////////// * test */ static tb_void_t tb_sort_int_test_perf(tb_size_t n) { __tb_volatile__ tb_size_t i = 0; // init data tb_long_t* data = (tb_long_t*)tb_nalloc0(n, sizeof(tb_long_t)); tb_assert_and_check_return(data); // init iterator tb_array_iterator_t array_iterator; tb_iterator_ref_t iterator = tb_iterator_make_for_long(&array_iterator, data, n); // make tb_random_clear(tb_null); for (i = 0; i < n; i++) data[i] = tb_random_range(tb_null, TB_MINS16, TB_MAXS16); // sort tb_hong_t time = tb_mclock(); tb_sort_all(iterator, tb_null); time = tb_mclock() - time; // time tb_trace_i("tb_sort_int_all: %lld ms", time); // check for (i = 1; i < n; i++) tb_assert_and_check_break(data[i - 1] <= data[i]); // free tb_free(data); }
static tb_void_t tb_demo_test_cstr_h(tb_size_t index) { // the count tb_size_t count = 1000000; // save func g_func_indx = index; g_func_prev = tb_item_func_str(tb_true); // the func tb_item_func_t func = g_func_prev; func.hash = tb_demo_test_hash_func; // init filter tb_bloom_filter_ref_t filter = tb_bloom_filter_init(TB_BLOOM_FILTER_PROBABILITY_0_001, 1, count, func); if (filter) { // clear random tb_random_clear(tb_random_generator()); // done tb_size_t i = 0; tb_size_t r = 0; tb_char_t s[256] = {0}; tb_hong_t t = tb_mclock(); for (i = 0; i < count; i++) { // the value tb_long_t value = tb_random(); // format it tb_snprintf(s, sizeof(s) - 1, "%ld", value); // set value to filter if (!tb_bloom_filter_set(filter, s)) { // repeat++ r++; } } t = tb_mclock() - t; // trace tb_trace_i("cstr: index: %lu, repeat: %lu, time: %lld ms", index, r, t); // exit filter tb_bloom_filter_exit(filter); } }
/* ////////////////////////////////////////////////////////////////////////////////////// * test */ static tb_void_t tb_random_test(tb_long_t b, tb_long_t e) { // init __tb_volatile__ tb_size_t i = 0; __tb_volatile__ tb_sint32_t n = 1000000; __tb_volatile__ tb_long_t rand = 0; // clear generator tb_random_clear(tb_random_generator()); // done tb_hong_t t = tb_mclock(); for (i = 0; i < n; i++) rand += tb_random_range(tb_random_generator(), b, e); t = tb_mclock() - t; tb_trace_i("time: %lld, average: %d, range: %ld - %ld", t, (rand + (n >> 1)) / n, b, e); }
static tb_void_t tb_random_test_float(tb_float_t b, tb_float_t e) { // init __tb_volatile__ tb_size_t i = 0; __tb_volatile__ tb_size_t n = 1000000; __tb_volatile__ tb_float_t rand = 0; // clear generator tb_random_clear(tb_random_generator()); // done tb_hong_t t = tb_mclock(); for (i = 0; i < n; i++) rand += tb_random_rangef(tb_random_generator(), b, e); t = tb_mclock() - t; tb_trace_i("time: %lld, average: %f, range: %f - %f", t, rand / n, b, e); }
static tb_void_t tb_demo_test_uint32_h(tb_size_t index) { // the count tb_size_t count = 1000000; // save func g_func_indx = index; g_func_prev = tb_item_func_uint32(); // the func tb_item_func_t func = g_func_prev; func.hash = tb_demo_test_hash_func; // init filter tb_bloom_filter_ref_t filter = tb_bloom_filter_init(TB_BLOOM_FILTER_PROBABILITY_0_001, 1, count, func); if (filter) { // clear random tb_random_clear(tb_random_generator()); // done tb_size_t i = 0; tb_size_t r = 0; tb_hong_t t = tb_mclock(); for (i = 0; i < count; i++) { // the value tb_long_t value = tb_random_range(tb_random_generator(), 0, TB_MAXU32); // set value to filter if (!tb_bloom_filter_set(filter, (tb_cpointer_t)value)) { // repeat++ r++; } } t = tb_mclock() - t; // trace tb_trace_i("uint32: index: %lu, repeat: %lu, time: %lld ms", index, r, t); // exit filter tb_bloom_filter_exit(filter); } }
static tb_void_t tb_test_heap_max_perf() { // init element tb_element_t element = tb_element_uint32(); element.comp = tb_test_heap_max_comp; // init heap tb_heap_ref_t heap = tb_heap_init(4096, element); tb_assert_and_check_return(heap); // clear rand tb_random_clear(tb_null); // init time tb_hong_t time = tb_mclock(); // profile __tb_volatile__ tb_size_t i = 0; __tb_volatile__ tb_size_t n = 100000; __tb_volatile__ tb_size_t p; for (i = 0; i < n; i++) tb_heap_put(heap, (tb_pointer_t)(tb_size_t)tb_random_range(tb_null, 0, 50)); for (i = 0; tb_heap_size(heap); i++) { // get the top value tb_size_t v = (tb_size_t)tb_heap_top(heap); // check order tb_assert_abort(!i || p >= v); // save the previous value p = v; // pop it tb_heap_pop(heap); } // exit time time = tb_mclock() - time; // trace tb_trace_i("heap_max: %lld ms", time); // exit heap tb_heap_exit(heap); }
static tb_void_t tb_demo_test_cstr_p() { // the count tb_size_t count = 10000000; // init filter tb_bloom_filter_ref_t filter = tb_bloom_filter_init(TB_BLOOM_FILTER_PROBABILITY_0_01, 3, count, tb_item_func_str(tb_true)); if (filter) { // clear random tb_random_clear(tb_random_generator()); // done tb_size_t i = 0; tb_size_t r = 0; tb_char_t s[256] = {0}; for (i = 0; i < count; i++) { // the value tb_long_t value = tb_random(); // format it tb_snprintf(s, sizeof(s) - 1, "%ld", value); // set value to filter if (!tb_bloom_filter_set(filter, s)) { // repeat++ r++; } } // trace #ifdef TB_CONFIG_TYPE_FLOAT tb_trace_i("cstr: count: %lu, repeat: %lu, repeat_p ~= p: %lf", count, r, (tb_double_t)r / count); #else tb_trace_i("cstr: count: %lu, repeat: %lu", count, r); #endif // exit filter tb_bloom_filter_exit(filter); } }
static tb_void_t tb_sort_int_test_func_bubble() { // init __tb_volatile__ tb_size_t i = 0; __tb_volatile__ tb_size_t n = 20; // init data tb_long_t* data = (tb_long_t*)tb_nalloc0(n, sizeof(tb_long_t)); tb_assert_and_check_return(data); // init iterator tb_array_iterator_t array_iterator; tb_iterator_ref_t iterator = tb_iterator_make_for_long(&array_iterator, data, n); // trace tb_trace_i(""); // put tb_random_clear(tb_null); for (i = 0; i < n; i++) { data[i] = tb_random_range(tb_null, TB_MINS16, TB_MAXS16); tb_trace_i("bubble_put: %ld", data[i]); } // sort tb_heap_sort_all(iterator, tb_null); // trace tb_trace_i(""); // pop for (i = 0; i < n; i++) tb_trace_i("bubble_pop: %ld", data[i]); // free tb_free(data); }
static tb_void_t tb_sort_str_test_perf_heap(tb_size_t n) { __tb_volatile__ tb_size_t i = 0; // init data tb_char_t** data = (tb_char_t**)tb_nalloc0(n, sizeof(tb_char_t*)); tb_assert_and_check_return(data); // init iterator tb_array_iterator_t array_iterator; tb_iterator_ref_t iterator = tb_iterator_make_for_str(&array_iterator, data, n); // make tb_random_clear(tb_null); tb_char_t s[256] = {0}; for (i = 0; i < n; i++) { tb_long_t r = tb_snprintf(s, 256, "%x", tb_random_range(tb_null, 0, TB_MAXU32)); s[r] = '\0'; data[i] = tb_strdup(s); } // sort tb_hong_t time = tb_mclock(); tb_heap_sort_all(iterator, tb_null); time = tb_mclock() - time; // time tb_trace_i("tb_heap_sort_str_all: %lld ms", time); // check for (i = 1; i < n; i++) tb_assert_and_check_break(tb_strcmp(data[i - 1], data[i]) <= 0); // free data for (i = 0; i < n; i++) tb_free(data[i]); tb_free(data); }
static tb_void_t tb_test_heap_min_func() { // init heap tb_heap_ref_t heap = tb_heap_init(16, tb_element_uint32()); tb_assert_and_check_return(heap); // clear rand tb_random_clear(tb_null); // make heap tb_size_t i = 0; for (i = 0; i < 100; i++) { // the value tb_uint32_t val = tb_random_range(tb_null, 0, 50); // trace // tb_trace_i("heap_min: put: %u", val); // put it tb_heap_put(heap, tb_u2p(val)); } // clear rand tb_random_clear(tb_null); // remove some values for (i = 0; i < 100; i++) { // the value tb_uint32_t val = tb_random_range(tb_null, 0, 50); // remove it? if (!(i & 3)) { tb_size_t itor = tb_find_all(heap, tb_u2p(val)); if (itor != tb_iterator_tail(heap)) tb_heap_remove(heap, itor); } } // append heap for (i = 0; i < 30; i++) { // the value tb_uint32_t val = tb_random_range(tb_null, 0, 50); // put it tb_heap_put(heap, tb_u2p(val)); } // trace tb_trace_i(""); // dump heap while (tb_heap_size(heap)) { // put it tb_uint32_t val = (tb_uint32_t)(tb_size_t)tb_heap_top(heap); // trace tb_trace_i("heap_min: pop: %u", val); // pop it tb_heap_pop(heap); } // exit heap tb_heap_exit(heap); }