/* ////////////////////////////////////////////////////////////////////////////////////// * test */ static tb_void_t tb_find_int_test() { __tb_volatile__ tb_size_t i = 0; __tb_volatile__ tb_size_t n = 1000; // 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_array_iterator_init_long(&array_iterator, data, n); // make for (i = 0; i < n; i++) data[i] = i; // find tb_size_t itor = tb_iterator_tail(iterator); tb_hong_t time = tb_mclock(); for (i = 0; i < n; i++) itor = tb_find_all(iterator, (tb_pointer_t)data[800]); time = tb_mclock() - time; // item tb_long_t item = itor != tb_iterator_tail(iterator)? (tb_long_t)tb_iterator_item(iterator, itor) : 0; // time tb_trace_i("tb_find_int_all[%ld ?= %ld]: %lld ms", item, data[800], time); // free tb_free(data); }
static tb_void_t tb_fixed_test_sin() { #if 1 __tb_volatile__ tb_int_t i = 0; __tb_volatile__ tb_int_t n = 10000000 / 360; __tb_volatile__ tb_fixed_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { for (i = 0; i < 360; i++) r = tb_fixed_sin(tb_fixed_test_angle[i]); } t = tb_mclock() - t; for (i = 0; i < 360; i++) { r = tb_fixed_sin(tb_fixed_test_angle[i]); tb_printf("[fixed]: sin(%f) = %f, angle: %d\n", tb_fixed_to_float(tb_fixed_test_angle[i]), tb_fixed_to_float(r), i); } tb_printf("[fixed]: sin(0 - 360), %lld ms\n", t); #else tb_int_t i = 0; for (i = 0; i < 360; i++) { tb_printf(",\t0x%x\n", tb_float_to_fixed(i * TB_DOUBLE_PI / 180)); } #endif }
static tb_void_t tb_float_test_atan() { __tb_volatile__ tb_long_t i = 0; __tb_volatile__ tb_long_t n = 10000000 / 100; __tb_volatile__ tb_float_t r = 0; __tb_volatile__ tb_float_t a = 0; tb_hong_t t = tb_mclock(); while (n--) { for (i = -50; i <= 50; i++) { a = tb_float_test_atan_a[i + 50]; r = tb_atanf(a); } } t = tb_mclock() - t; for (i = -50; i <= 50; i++) { a = tb_float_test_atan_a[i + 50]; r = tb_atanf(a); tb_printf("[float]: atan(%f) = %f\n", (a), (r)); } tb_printf("[float]: atan, %lld ms\n", t); }
static tb_void_t tb_float_test_sin() { #if 1 __tb_volatile__ tb_long_t i = 0; __tb_volatile__ tb_long_t n = 10000000 / 360; __tb_volatile__ tb_float_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { for (i = 0; i < 360; i++) r = tb_sinf(tb_float_test_angle[i]); } t = tb_mclock() - t; for (i = 0; i < 360; i++) { r = tb_sinf(tb_float_test_angle[i]); tb_printf("[float]: sin(%f) = %f, angle: %ld\n", (tb_float_test_angle[i]), (r), i); } tb_printf("[float]: sin(0 - 360), %lld ms\n", t); #else tb_long_t i = 0; for (i = 0; i < 360; i++) { tb_printf(",\t%f\n", (i * TB_PI / 180)); } #endif }
static tb_size_t tb_vector_insert_tail_test() { // init tb_vector_ref_t vector = tb_vector_init(TB_VECTOR_GROW_SIZE, tb_element_long()); tb_assert_and_check_return_val(vector, 0); __tb_volatile__ tb_size_t i = 0; __tb_volatile__ tb_size_t n = 10000; tb_hong_t t = tb_mclock(); for (i = 0; i < n; i++) tb_vector_insert_tail(vector, (tb_pointer_t)0xf); t = tb_mclock() - t; // time tb_trace_i("tb_vector_insert_tail(%lu): %lld ms, size: %lu, maxn: %lu", n, t, tb_vector_size(vector), tb_vector_maxn(vector)); // check tb_assert(tb_vector_size(vector) == n); tb_assert(tb_vector_head(vector) == (tb_pointer_t)0xf); tb_assert(tb_vector_last(vector) == (tb_pointer_t)0xf); // clear it tb_vector_clear(vector); tb_assert(!tb_vector_size(vector)); // exit tb_vector_exit(vector); return n / ((tb_uint32_t)(t) + 1); }
static tb_size_t tb_vector_nreplace_last_test() { // init tb_vector_ref_t vector = tb_vector_init(TB_VECTOR_GROW_SIZE, tb_element_long()); tb_assert_and_check_return_val(vector, 0); tb_size_t n = 10000; tb_vector_ninsert_head(vector, (tb_pointer_t)0xf, n); tb_hong_t t = tb_mclock(); tb_vector_nreplace_last(vector, (tb_pointer_t)0xd, n); t = tb_mclock() - t; // time tb_trace_i("tb_vector_nreplace_last(%lu): %lld ms, size: %lu, maxn: %lu", n, t, tb_vector_size(vector), tb_vector_maxn(vector)); // check tb_assert(tb_vector_size(vector) == n); tb_assert(tb_vector_head(vector) == (tb_pointer_t)0xd); tb_assert(tb_vector_last(vector) == (tb_pointer_t)0xd); // exit tb_vector_exit(vector); return n / ((tb_uint32_t)(t) + 1); }
/* ////////////////////////////////////////////////////////////////////////////////////// * 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_test_wcsnlen(tb_wchar_t const* s, tb_size_t size) { __tb_volatile__ tb_long_t n = 1000000; __tb_volatile__ tb_long_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_wcsnlen(s, size); } t = tb_mclock() - t; tb_wprintf(L"%lld ms, tb_test_wcsnlen(%s, %ld) = %d\n", t, s, size, r); }
static tb_void_t tb_test_wcsnicmp(tb_wchar_t const* s1, tb_wchar_t const* s2, tb_size_t size) { __tb_volatile__ tb_long_t n = 1000000; __tb_volatile__ tb_long_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_wcsnicmp(s1, s2, size); } t = tb_mclock() - t; tb_wprintf(L"%lld ms, tb_test_wcsnicmp(%s, %s, %u) = %ld\n", t, s1, s2, size, r); }
static tb_void_t tb_fixed_test_exp(tb_fixed_t x) { __tb_volatile__ tb_int_t n = 10000000; __tb_volatile__ tb_fixed_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_fixed_exp(x); } t = tb_mclock() - t; tb_printf("[fixed]: exp(%f) = %f, %lld ms\n", tb_fixed_to_float(x), tb_fixed_to_float(r), t); }
static tb_void_t tb_fixed_test_ilog2(tb_fixed_t x) { __tb_volatile__ tb_int_t n = 10000000; __tb_volatile__ tb_uint32_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_fixed_ilog2(x); } t = tb_mclock() - t; tb_printf("[fixed]: ilog2(%f) = %d, %lld ms\n", tb_fixed_to_float(x), r, t); }
static tb_void_t tb_fixed_test_div(tb_fixed_t a, tb_fixed_t b) { __tb_volatile__ tb_int_t n = 10000000; __tb_volatile__ tb_fixed_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_fixed_div(a, b); } t = tb_mclock() - t; tb_printf("[fixed]: div(%f, %f): %f, %lld ms\n", tb_fixed_to_float(a), tb_fixed_to_float(b), tb_fixed_to_float(r), t); }
static tb_void_t tb_fixed_test_ceil(tb_fixed_t x) { __tb_volatile__ tb_int_t n = 10000000; __tb_volatile__ tb_int_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_fixed_ceil(x); } t = tb_mclock() - t; tb_printf("[fixed]: ceil(%f): %d, %lld ms\n", tb_fixed_to_float(x), r, t); }
static tb_void_t tb_test_strcpy(tb_char_t const* s2) { __tb_volatile__ tb_int_t n = 1000000; tb_char_t s1[4096]; tb_hong_t t = tb_mclock(); while (n--) { tb_strcpy(s1, s2); } t = tb_mclock() - t; tb_printf("%lld ms, tb_test_strcpy(%s) = %s\n", t, s2, s1); }
static tb_void_t tb_test_stricmp(tb_char_t const* s1, tb_char_t const* s2) { __tb_volatile__ tb_long_t n = 1000000; __tb_volatile__ tb_long_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_stricmp(s1, s2); } t = tb_mclock() - t; tb_printf("%lld ms, tb_test_stricmp(%s, %s) = %ld\n", t, s1, s2, r); }
static tb_void_t tb_test_wcslen(tb_wchar_t const* s) { __tb_volatile__ tb_long_t n = 1000000; __tb_volatile__ tb_long_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_wcslen(s); } t = tb_mclock() - t; tb_wprintf(L"%lld ms, tb_test_wcslen(%s) = %ld\n", t, s, r); }
static tb_void_t tb_float_test_exp(tb_float_t x) { __tb_volatile__ tb_long_t n = 10000000; __tb_volatile__ tb_float_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_expf(x); } t = tb_mclock() - t; tb_printf("[float]: exp(%f) = %f, %lld ms\n", (x), r, t); }
static tb_void_t tb_test_wcsncpy(tb_wchar_t const* s2, tb_size_t size) { __tb_volatile__ tb_int_t n = 1000000; tb_wchar_t s1[4096]; tb_hong_t t = tb_mclock(); while (n--) { tb_wcslcpy(s1, s2, size); } t = tb_mclock() - t; tb_wprintf(L"%lld ms, tb_test_wcsncpy(%s, %lu) = %s\n", t, s2, size, s1); }
static tb_void_t tb_float_test_ceil(tb_float_t x) { __tb_volatile__ tb_long_t n = 10000000; __tb_volatile__ tb_long_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = tb_ceil(x); } t = tb_mclock() - t; tb_printf("[float]: ceil(%f): %ld, %lld ms\n", (x), r, t); }
static tb_void_t tb_float_test_div(tb_float_t a, tb_float_t b) { __tb_volatile__ tb_long_t n = 10000000; __tb_volatile__ tb_float_t r = 0; tb_hong_t t = tb_mclock(); while (n--) { r = a / b; } t = tb_mclock() - t; tb_printf("[float]: div(%f, %f): %f, %lld ms\n", (a), (b), (r), t); }
static tb_bool_t tb_demo_sock_recv_func(tb_aice_t const* aice) { // check tb_assert_and_check_return_val(aice && aice->code == TB_AICE_CODE_RECV, tb_false); // the context tb_demo_context_t* context = (tb_demo_context_t*)aice->priv; tb_assert_and_check_return_val(context, tb_false); // ok? if (aice->state == TB_STATE_OK) { // trace tb_trace_d("recv[%p]: real: %lu, size: %lu", aice->aico, aice->u.recv.real, aice->u.recv.size); // post writ to file if (!tb_aico_writ(context->file, context->size, aice->u.recv.data, aice->u.recv.real, tb_demo_file_writ_func, context)) return tb_false; // save size context->size += aice->u.recv.real; // compute speed context->peak += aice->u.recv.real; if (!context->time) { context->time = tb_mclock(); context->base = tb_mclock(); context->sped = context->peak; } else if (tb_mclock() > context->time + 1000) { context->sped = context->peak; context->peak = 0; context->time = tb_mclock(); // trace tb_trace_i("recv[%p]: size: %llu, sped: %lu KB/s", aice->aico, context->size, context->sped / 1000); } } // closed or failed? else { // trace tb_trace_i("recv[%p]: state: %s", aice->aico, tb_state_cstr(aice->state)); // exit context tb_demo_context_exit(context); } // ok return tb_true; }
/* ////////////////////////////////////////////////////////////////////////////////////// * test */ static tb_void_t tb_dns_test_done(tb_char_t const* name) { // time tb_hong_t time = tb_mclock(); // done tb_ipaddr_t addr; if (tb_dns_looker_done(name, &addr)) { time = tb_mclock() - time; tb_trace_i("[demo]: %s => %{ipaddr}, %lld ms", name, &addr, time); } else tb_trace_i("[demo]: %s failed", name); }
tb_bool_t tb_aicp_exit(tb_aicp_ref_t aicp) { // check tb_aicp_impl_t* impl = (tb_aicp_impl_t*)aicp; tb_assert_and_check_return_val(impl, tb_false); // kill all first tb_aicp_kill_all((tb_aicp_ref_t)impl); // wait all exiting if (tb_aicp_wait_all((tb_aicp_ref_t)impl, 5000) <= 0) { // wait failed, trace left aicos tb_spinlock_enter(&impl->lock); if (impl->pool) tb_fixed_pool_walk(impl->pool, tb_aicp_walk_wait, tb_null); tb_spinlock_leave(&impl->lock); return tb_false; } // kill loop tb_aicp_kill((tb_aicp_ref_t)impl); // wait workers exiting tb_hong_t time = tb_mclock(); while (tb_atomic_get(&impl->work) && (tb_mclock() < time + 5000)) tb_msleep(500); // exit proactor if (impl->ptor) { tb_assert(impl->ptor && impl->ptor->exit); impl->ptor->exit(impl->ptor); impl->ptor = tb_null; } // exit aico pool tb_spinlock_enter(&impl->lock); if (impl->pool) tb_fixed_pool_exit(impl->pool); impl->pool = tb_null; tb_spinlock_leave(&impl->lock); // exit lock tb_spinlock_exit(&impl->lock); // free impl tb_free(impl); // ok return tb_true; }
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; // 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_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; // done tb_hong_t t = tb_mclock(); for (i = 0; i < n; i++) rand += tb_random_rangef(b, e); t = tb_mclock() - t; // trace tb_trace_i("time: %lld, average: %f, range: %f - %f", t, rand / n, b, e); }
/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t tb_demo_asio_dns_main(tb_int_t argc, tb_char_t** argv) { // check tb_assert_and_check_return_val(argv[1], 0); // done tb_aicp_ref_t aicp = tb_null; tb_aicp_dns_ref_t dns = tb_null; do { // init aicp aicp = tb_aicp_init(2); tb_assert_and_check_break(aicp); // init dns dns = tb_aicp_dns_init(aicp); tb_assert_and_check_break(dns); // sort server tb_dns_server_sort(); // init time tb_hong_t time = tb_mclock(); // trace tb_trace_i("dns: %s: ..", argv[1]); // done dns tb_aicp_dns_done(dns, argv[1], -1, tb_demo_sock_dns_done_func, tb_null); // loop aicp tb_aicp_loop(aicp); // exit time time = tb_mclock() - time; // trace tb_trace_i("dns: %s: time: %lld ms", argv[1], time); } while (0); // trace tb_trace_i("end"); // exit aicp if (aicp) tb_aicp_exit(aicp); return 0; }
/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t tb_demo_asio_aicpc_main(tb_int_t argc, tb_char_t** argv) { // check tb_assert_and_check_return_val(argv[1], 0); // init tb_aicp_ref_t aicp = tb_null; tb_demo_context_t context = {0}; do { // init aicp aicp = tb_aicp_init(2); tb_assert_and_check_break(aicp); // init data context.data = tb_malloc_bytes(TB_DEMO_SOCK_RECV_MAXN); tb_assert_and_check_break(context.data); // init sock aico context.sock = tb_aico_init(aicp); tb_assert_and_check_break(context.sock); // init addr tb_ipaddr_t addr; if (!tb_ipaddr_set(&addr, "127.0.0.1", 9090, TB_IPADDR_FAMILY_NONE)) break; // open sock aico if (!tb_aico_open_sock_from_type(context.sock, TB_SOCKET_TYPE_TCP, tb_ipaddr_family(&addr))) break; // init file aico context.file = tb_aico_init(aicp); tb_assert_and_check_break(context.file); // open file aico if (!tb_aico_open_file_from_path(context.file, argv[1], TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_BINARY | TB_FILE_MODE_TRUNC)) break; // init conn timeout tb_aico_timeout_set(context.sock, TB_AICO_TIMEOUT_CONN, 10000); // post conn tb_trace_i("conn: .."); if (!tb_aico_conn(context.sock, &addr, tb_demo_sock_conn_func, &context)) break; // loop aicp tb_aicp_loop(aicp); // trace if (tb_mclock() > context.base) tb_trace_i("size: %llu, sped: %llu KB/s", context.size, context.size / (tb_mclock() - context.base)); } while (0); // trace tb_trace_i("end"); // exit aicp if (aicp) tb_aicp_exit(aicp); return 0; }
static tb_int_t tb_demo_thread_local_test(tb_cpointer_t priv) { // self tb_size_t self = tb_thread_self(); // trace tb_trace_i("thread[%lx]: init", self); // init the thread local, only once static tb_thread_local_t s_local = TB_THREAD_LOCAL_INIT; if (!tb_thread_local_init(&s_local, tb_demo_thread_local_free)) return -1; // init start time tb_hong_t time = tb_mclock(); // done __tb_volatile__ tb_size_t count = 10000000; while (count--) { // attempt to get local variable tb_size_t local; if (!(local = (tb_size_t)tb_thread_local_get(&s_local))) { // init local variable if (tb_thread_local_set(&s_local, (tb_cpointer_t)self)) local = self; } // check if (local != self) { // trace tb_trace_i("thread[%lx]: invalid value: %lx", self, local); } } // compile the interval time time = tb_mclock() - time; // trace tb_trace_i("thread[%lx]: exit: %lld ms", self, time); // ok return 0; }
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); } }