virtual void* run() { bool ret; acl::redis_client* conn; acl::redis_zset redis; acl::redis_key key_redis; acl::string key; for (int i = 0; i < n_; i++) { // 从全局线程池中获取一个 redis 连接对象 conn = (acl::redis_client*) pool_.peek(); if (conn == NULL) { printf("peek redis_client failed\r\n"); break; } // 每个线程一个 ID 号,做为键值组成部分 key.format("%s_%d_%d", __keypre.c_str(), id_, i); redis.clear(); // 将 redis 连接对象与 redis 命令操作类对象进行绑定关联 redis.set_client(conn); if (cmd_ == "zadd") ret = test_zadd(redis, i, key.c_str(), __big_data, __big_data_length, __base_length); else if (cmd_ == "zcard") ret = test_zcard(redis, i, key); else if (cmd_ == "zrange") ret = test_zrange(redis, i, key, __hmac); else if (cmd_ == "del") { key_redis.set_client(conn); ret = test_del(key_redis, i, key); } else if (cmd_ != "all") { printf("unknown cmd: %s\r\n", cmd_.c_str()); ret = false; } else if (test_zadd(redis, i, key.c_str(), __big_data, __big_data_length, __base_length) == false || test_zcard(redis, i, key) == false || test_zrange(redis, i, key, __hmac) == false) { ret = false; } else ret = true; // 将 redis 连接对象归还给连接池,是否保持该连接, // 通过判断该连接是否断开决定 pool_.put(conn, !conn->eof()); if (ret == false) break; } return NULL; }
int main(int argc, char* argv[]) { int ch, n = 1, conn_timeout = 10, rw_timeout = 10; acl::string addr("127.0.0.1:6379"), cmd; while ((ch = getopt(argc, argv, "hs:n:C:T:a:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 's': addr = optarg; break; case 'n': n = atoi(optarg); break; case 'C': conn_timeout = atoi(optarg); break; case 'T': rw_timeout = atoi(optarg); break; case 'a': cmd = optarg; break; default: break; } } acl::acl_cpp_init(); acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout); acl::redis_zset redis(&client); bool ret; if (cmd == "zadd") ret = test_zadd(redis, n); else if (cmd == "zcard") ret = test_zcard(redis, n); else if (cmd == "zcount") ret = test_zcount(redis, n); else if (cmd == "zincrby") ret = test_zincrby(redis, n); else if (cmd == "zrange") ret = test_zrange(redis, n); else if (cmd == "zrangebyscore") ret = test_zrangebyscore(redis, n); else if (cmd == "zrank") ret = test_zrank(redis, n); else if (cmd == "zrem") ret = test_zrem(redis, n); else if (cmd == "zscore") ret = test_zscore(redis, n); else if (cmd == "zunionstore") ret = test_zunionstore(redis, n); else if (cmd == "zinterstore") ret = test_zinterstore(redis, n); else if (cmd == "zscan") ret = test_zscan(redis, n); else if (cmd == "zrangebylex") ret = test_zrangebylex(redis, n); else if (cmd == "zlexcount") ret = test_zlexcount(redis, n); else if (cmd == "zremrangebylex") ret = test_zremrangebylex(redis, n); else if (cmd == "all") { ret = test_zadd(redis, n) && test_zcard(redis, n) && test_zcount(redis, n) && test_zincrby(redis, n) && test_zrange(redis, n) && test_zrangebyscore(redis, n) && test_zrank(redis, n) && test_zrem(redis, n) && test_zscore(redis, n); } else { ret = false; printf("unknown cmd: %s\r\n", cmd.c_str()); } if (ret == true) printf("test OK!\r\n"); else printf("test failed!\r\n"); #ifdef WIN32 printf("enter any key to exit\r\n"); getchar(); #endif return 0; }