int del(int count) { acl::string key, val; acl::redis cmd(&cluster_); struct timeval last, now; gettimeofday(&last, NULL); int i; for (i = 0; i < count; i++) { key.format("key-%d-%d", acl_fiber_self(), i); acl::fiber_sem_guard guard(sem_); if (cmd.del_one(key) < 0) { printf("fiber-%d: del error: %s, key: %s\r\n", acl_fiber_self(), cmd.result_error(), key.c_str()); break; } cmd.clear(); ndel_++; } gettimeofday(&now, NULL); double spent = stamp_sub(&now, &last); printf("---del spent %.2f ms, count %d, speed: %.2f----\r\n", spent, i, (i * 1000) / (spent > 0 ? spent : 1)); return i; }
// @override void run(void) { redis_oper oper(cluster_, sem_); int n = oper.set(__oper_count); (void) oper.get(n); (void) oper.del(n); static long long __total = 0; __total += oper.nset() + oper.nget() + oper.ndel(); if (--__fibers_count == 0) { gettimeofday(&__end, NULL); double spent = stamp_sub(&__end, &__begin); printf("-------- All fibers over now --------\r\n"); printf("fibers: %d, count: %lld, spent: %.2f, speed: %.2f\r\n", __fibers_max, __total, spent, (__total * 1000) / (spent > 0 ? spent : 1)); acl::fiber::schedule_stop(); } }
static void fiber_client(const char* addr, const char* user, const char* to, int loop) { acl::socket_stream conn; if (conn.open(addr, 0, 10) == false) { printf("connect %s error %s\r\n", addr, acl::last_serror()); } else { printf("fiber-%d, connect %s ok\r\n", acl_fiber_self(), addr); user_client* client = new user_client(conn, user, to, loop); go[=] { fiber_reader(client); }; client->wait_exit(); printf("--- client %s exit now ----\r\n", client->get_name()); delete client; } __cur_client--; if (__cur_client == 0) { printf("-----All fibers over!-----\r\n"); struct timeval end; gettimeofday(&end, NULL); double spent = stamp_sub(&end, &__begin); printf("---Total read: %d, spent: %.2f, speed: %.2f---\r\n", __total_read, spent, (1000 * __total_read) / (spent < 1 ? 1 : spent)); acl_fiber_schedule_stop(); } }
int set(int count) { acl::string key, val; acl::redis cmd(&cluster_); struct timeval last, now; gettimeofday(&last, NULL); int i; for (i = 0; i < count; i++) { key.format("key-%d-%d", acl_fiber_self(), i); val.format("val-%d-%d", acl_fiber_self(), i); acl::fiber_sem_guard guard(sem_); if (cmd.set(key, val) == false) { printf("fiber-%d: set error: %s, key: %s\r\n", acl_fiber_self(), cmd.result_error(), key.c_str()); break; } else if (i < 5) printf("fiber-%d: set ok, key: %s\r\n", acl_fiber_self(), key.c_str()); cmd.clear(); nset_++; } gettimeofday(&now, NULL); double spent = stamp_sub(&now, &last); printf("---set spent %.2f ms, count %d, speed: %.2f----\r\n", spent, i, (i * 1000) / (spent > 0 ? spent : 1)); return i; }
static void run(const char *local_addr, const char *peer_addr, int count, int dlen, int inter, int need_read, int quit) { double spent; int ret, i; char buf[4096], data[4096]; struct timeval begin, end; ACL_VSTREAM *stream = acl_vstream_bind(local_addr, 2); /* 绑定 UDP 套接口 */ if (stream == NULL) { printf("acl_vstream_bind %s error %s\r\n", local_addr, acl_last_serror()); return; } if (dlen > (int) sizeof(data) - 1) dlen = (int) sizeof(data) - 1; for (i = 0; i < dlen; i++) data[i] = 'X'; data[dlen] = 0; gettimeofday(&begin, NULL); acl_vstream_set_peer(stream, peer_addr); ACL_VSTREAM_SET_RWTIMO(stream, 1); for (i = 0; i < count; i++) { /* 如果服务端的地址是变化的,则应该在写每次前都需要调用 * acl_vstream_set_peer */ ret = acl_vstream_write(stream, data, dlen); if (ret == ACL_VSTREAM_EOF) { printf("acl_vtream_write error %s\r\n", acl_last_serror()); break; } if (need_read) { ret = acl_vstream_read(stream, buf, sizeof(buf) - 1); if (ret == ACL_VSTREAM_EOF) { if (errno == ETIMEDOUT) { printf("timeout read\r\n"); continue; } printf("acl_vstream_read error %s\r\n", acl_last_serror()); break; } else buf[ret] = 0; if (i % inter == 0) printf("result: %s\r\n", buf); } if (i % inter == 0) { snprintf(buf, sizeof(buf), "total: %d, curr: %d", count, i); ACL_METER_TIME(buf); } } gettimeofday(&end, NULL); spent = stamp_sub(&end, &begin); printf("thread: %lu, total: %d, curr: %d, spent: %.2f, speed: %.2f\r\n", (unsigned long) acl_pthread_self(), count, i, spent, (i * 1000) / (spent > 1 ? spent : 1)); printf("thread: %lu, local addr: %s, peer addr: %s\r\n", (unsigned long) acl_pthread_self(), ACL_VSTREAM_LOCAL(stream), ACL_VSTREAM_PEER(stream)); if (quit) acl_vstream_write(stream, "quit", 4); acl_vstream_close(stream); }
int main(int argc, char* argv[]) { char buf[8192], filepath[256]; int ret, n; ACL_VSTREAM* fp; ACL_XML* xml; struct timeval begin, end; double spent; int ch, use_slice = 0, cache_count = 1000; filepath[0] = 0; while ((ch = getopt(argc, argv, "hmc:f:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 'm': use_slice = 1; break; case 'c': cache_count = atoi(optarg); if (cache_count <= 0) cache_count = 1000; break; case 'f': snprintf(filepath, sizeof(filepath), "%s", optarg); break; default: break; } } if (use_slice) acl_mem_slice_init(8, 1024, 100000, ACL_SLICE_FLAG_GC2 | ACL_SLICE_FLAG_RTGC_OFF | ACL_SLICE_FLAG_LP64_ALIGN); if (filepath[0] == 0) { usage(argv[0]); return 1; } xml = acl_xml_alloc(); if (cache_count > 0) acl_xml_cache(xml, cache_count); fp = acl_vstream_fopen(filepath, O_RDONLY, 0600, 8192); if (fp == NULL) { printf("open file %s error %s\r\n", filepath, acl_last_serror()); acl_xml_free(xml); return 1; } gettimeofday(&begin, NULL); n = 0; ACL_METER_TIME("------begin------"); while (1) { ret = acl_vstream_fgets(fp, buf, sizeof(buf) - 1); if (ret == ACL_VSTREAM_EOF) break; buf[ret] = 0; acl_xml_parse(xml, buf); if (++n % 10000 == 0) { printf("line: %d\r\n", n); ACL_METER_TIME("-------ok------"); } if (n % cache_count == 0) { printf("reset xml, line: %d\r\n", n); acl_xml_reset(xml); } } gettimeofday(&end, NULL); spent = stamp_sub(&end, &begin); printf("\r\ntotal spent: %0.2f ms\r\n", spent); acl_xml_free(xml); acl_vstream_fclose(fp); return 0; }
static void fiber_redis(FIBER *fiber, void *ctx) { acl::redis_client_cluster *cluster = (acl::redis_client_cluster *) ctx; acl::redis cmd(cluster); acl::string key, val; int i = 0; struct timeval last, now; gettimeofday(&last, NULL); for (; i < __oper_count; i++) { key.format("key-%d-%d", fiber_id(fiber), i); val.format("val-%d-%d", fiber_id(fiber), i); if (cmd.set(key, val) == false) { printf("fiber-%d: set error: %s, key: %s\r\n", fiber_id(fiber), cmd.result_error(), key.c_str()); break; } else if (i < 5) printf("fiber-%d: set ok, key: %s\r\n", fiber_id(fiber), key.c_str()); cmd.clear(); } gettimeofday(&now, NULL); double spent = stamp_sub(&now, &last); printf("---set spent %.2f ms, count %d, speed: %.2f----\r\n", spent, i, (i * 1000) / (spent > 0 ? spent : 1)); gettimeofday(&last, NULL); for (int j = 0; j < i; j++) { key.format("key-%d-%d", fiber_id(fiber), j); if (cmd.get(key, val) == false) { printf("fiber-%d: get error: %s, key: %s\r\n", fiber_id(fiber), cmd.result_error(), key.c_str()); break; } val.clear(); cmd.clear(); } gettimeofday(&now, NULL); spent = stamp_sub(&now, &last); printf("---get spent %.2f ms, count %d, speed: %.2f----\r\n", spent, i, (i * 1000) / (spent > 0 ? spent : 1)); gettimeofday(&last, NULL); for (int j = 0; j < i; j++) { key.format("key-%d-%d", fiber_id(fiber), j); if (cmd.del_one(key) < 0) { printf("fiber-%d: del error: %s, key: %s\r\n", fiber_id(fiber), cmd.result_error(), key.c_str()); break; } cmd.clear(); } gettimeofday(&now, NULL); spent = stamp_sub(&now, &last); printf("---del spent %.2f ms, count %d, speed: %.2f----\r\n", spent, i, (i * 1000) / (spent > 0 ? spent : 1)); if (--__fibers_count == 0) { long long total = __fibers_max * i * 3; gettimeofday(&__end, NULL); spent = stamp_sub(&__end, &__begin); printf("fibers: %d, count: %lld, spent: %.2f, speed: %.2f\r\n", __fibers_max, total, spent, (total * 1000) / (spent > 0 ? spent : 1)); fiber_io_stop(); } }
#include "fiber/lib_fiber.h" #include "stamp.h" static int __timer_sleep = 1000; static int __max_fiber = 1000; static __thread struct timeval __begin; static __thread int __left_fiber = 1000; static void timer_main(ACL_FIBER *fiber, void *ctx acl_unused) { struct timeval now; double spent; gettimeofday(&now, NULL); spent = stamp_sub(&now, &__begin); printf("thread-%lu, timer-%d wakeup, spend: %.2f ms\r\n", (unsigned long) acl_pthread_self(), acl_fiber_id(fiber), spent); if (--__left_fiber == 0) { printf("All are over!\r\n"); //acl_fiber_schedule_stop(); } } static void *thread_main(void *ctx acl_unused) { int i; gettimeofday(&__begin, NULL);