/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t tb_demo_platform_thread_pool_main(tb_int_t argc, tb_char_t** argv) { #if 0 // post task: 60s tb_thread_pool_task_post(tb_thread_pool(), "60000ms", tb_demo_task_time_done, tb_null, (tb_cpointer_t)60000, tb_false); // post task: 10s tb_thread_pool_task_post(tb_thread_pool(), "10000ms", tb_demo_task_time_done, tb_null, (tb_cpointer_t)10000, tb_false); // post task: 1s tb_thread_pool_task_post(tb_thread_pool(), "1000ms", tb_demo_task_time_done, tb_demo_task_time_exit, (tb_cpointer_t)1000, tb_false); // wait some time getchar(); #else // done tb_size_t count = tb_random_range(tb_null, 1, 16); tb_size_t total = count; while (count-- && total < 1000) { // the time tb_size_t time = tb_random_range(tb_null, 0, 500); // trace tb_trace_i("post: %lu ms, total: %lu", time, total); // post task: time ms tb_thread_pool_task_post(tb_thread_pool(), tb_null, tb_demo_task_time_done, tb_demo_task_time_exit, (tb_pointer_t)time, !(time & 15)? tb_true : tb_false); // finished? wait some time and update count if (!count) { // wait some time tb_msleep(100); // update count count = tb_random_range(tb_null, 1, 16); total += count; } } // wait all tb_thread_pool_task_wait_all(tb_thread_pool(), -1); #endif // trace tb_trace_i("end"); return 0; }
/* ////////////////////////////////////////////////////////////////////////////////////// * 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); }
/* ////////////////////////////////////////////////////////////////////////////////////// * 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; // done tb_hong_t t = tb_mclock(); for (i = 0; i < n; i++) rand += tb_random_range(b, e); t = tb_mclock() - t; // trace tb_trace_i("time: %lld, average: %d, range: %ld - %ld", t, (rand + (n >> 1)) / 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_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); }