2020-07-20 17:27:32 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static acl::string __keypre("test_key_cluster");
|
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
static bool test_del(acl::redis& cmd, int i)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
cmd.clear();
|
|
|
|
|
|
|
|
acl::string key;
|
|
|
|
|
|
|
|
key.format("%s_%d", __keypre.c_str(), i);
|
|
|
|
int ret = cmd.del(key.c_str());
|
2021-01-01 20:35:07 +08:00
|
|
|
if (ret < 0) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("del key: %s error: %s\r\n",
|
|
|
|
key.c_str(), cmd.result_error());
|
|
|
|
return false;
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("del ok, key: %s\r\n", key.c_str());
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
2020-07-20 17:27:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
static bool test_expire(acl::redis& cmd, int i)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
cmd.clear();
|
|
|
|
|
|
|
|
acl::string key;
|
|
|
|
|
|
|
|
key.format("%s_%d", __keypre.c_str(), i);
|
2021-01-01 20:35:07 +08:00
|
|
|
if (cmd.expire(key.c_str(), 100) < 0) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("expire key: %s error: %s\r\n",
|
|
|
|
key.c_str(), cmd.result_error());
|
|
|
|
return false;
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("expire ok, key: %s\r\n", key.c_str());
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:27:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
static bool test_ttl(acl::redis& cmd, int i)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
cmd.clear();
|
|
|
|
|
|
|
|
acl::string key;
|
|
|
|
int ttl;
|
|
|
|
|
|
|
|
key.format("%s_%d", __keypre.c_str(), i);
|
2021-01-01 20:35:07 +08:00
|
|
|
if ((ttl = cmd.ttl(key.c_str())) < 0) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("get ttl key: %s error: %s\r\n",
|
|
|
|
key.c_str(), cmd.result_error());
|
|
|
|
return false;
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("ttl ok, key: %s, ttl: %d\r\n", key.c_str(), ttl);
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
2020-07-20 17:27:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
static bool test_exists(acl::redis& cmd, int i)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
cmd.clear();
|
|
|
|
|
|
|
|
acl::string key;
|
|
|
|
|
|
|
|
key.format("%s_%d", __keypre.c_str(), i);
|
2021-01-01 20:35:07 +08:00
|
|
|
if (!cmd.exists(key.c_str())) {
|
|
|
|
if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("no exists key: %s\r\n", key.c_str());
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("exists key: %s\r\n", key.c_str());
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
2020-07-20 17:27:32 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
static bool test_type(acl::redis& cmd, int i)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
cmd.clear();
|
|
|
|
|
|
|
|
acl::string key;
|
|
|
|
|
|
|
|
key.format("%s_%d", __keypre.c_str(), i);
|
|
|
|
acl::redis_key_t ret = cmd.type(key.c_str());
|
2021-01-01 20:35:07 +08:00
|
|
|
if (ret == acl::REDIS_KEY_NONE) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("unknown type key: %s\r\n", key.c_str());
|
|
|
|
return false;
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("type ok, key: %s, ret: %d\r\n", key.c_str(), ret);
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
2020-07-20 17:27:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
static bool test_set(acl::redis& cmd, int i)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
cmd.clear();
|
|
|
|
|
|
|
|
acl::string key;
|
|
|
|
key.format("%s_%d", __keypre.c_str(), i);
|
|
|
|
|
|
|
|
acl::string value;
|
|
|
|
value.format("value_%s", key.c_str());
|
|
|
|
|
|
|
|
bool ret = cmd.set(key.c_str(), value.c_str());
|
|
|
|
return ret;
|
2021-01-01 20:35:07 +08:00
|
|
|
if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("set key: %s, value: %s %s\r\n", key.c_str(),
|
|
|
|
value.c_str(), ret ? "ok" : "error");
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
2020-07-20 17:27:32 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
static bool test_get(acl::redis& cmd, int i)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
cmd.clear();
|
|
|
|
|
|
|
|
acl::string key;
|
|
|
|
key.format("%s_%d", __keypre.c_str(), i);
|
|
|
|
|
|
|
|
acl::string value;
|
|
|
|
|
|
|
|
bool ret = cmd.get(key.c_str(), value);
|
2021-01-01 20:35:07 +08:00
|
|
|
if (i < 10) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("get key: %s, value: %s %s, len: %d\r\n",
|
|
|
|
key.c_str(), value.c_str(), ret ? "ok" : "error",
|
|
|
|
(int) value.length());
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
2020-07-20 17:27:32 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __threads_exit = 0;
|
|
|
|
|
|
|
|
class test_thread : public acl::thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
test_thread(acl::locker& locker, acl::redis_client_pipeline& conns,
|
2022-11-21 20:27:09 +08:00
|
|
|
const char* cmd, int n)
|
2020-07-20 17:27:32 +08:00
|
|
|
: locker_(locker)
|
|
|
|
, conns_(conns)
|
|
|
|
, cmd_(cmd)
|
|
|
|
, n_(n)
|
|
|
|
{}
|
|
|
|
|
2021-01-01 20:35:07 +08:00
|
|
|
~test_thread(void) {}
|
2020-07-20 17:27:32 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// @override
|
2021-01-01 20:35:07 +08:00
|
|
|
void* run(void)
|
2020-07-20 17:27:32 +08:00
|
|
|
{
|
|
|
|
bool ret;
|
2021-01-31 15:43:29 +08:00
|
|
|
acl::redis cmd_key;
|
2022-11-21 20:27:09 +08:00
|
|
|
cmd_key.set_pipeline(&conns_);
|
2020-07-20 17:27:32 +08:00
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
acl::redis cmd_string;
|
2022-11-21 20:27:09 +08:00
|
|
|
cmd_string.set_pipeline(&conns_);
|
2020-07-20 17:27:32 +08:00
|
|
|
|
2021-01-01 20:35:07 +08:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
if (cmd_ == "set") {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = test_set(cmd_string, i);
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (cmd_ == "get") {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = test_get(cmd_string, i);
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (cmd_ == "del") {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = test_del(cmd_key, i);
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (cmd_ == "expire") {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = test_expire(cmd_key, i);
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (cmd_ == "ttl") {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = test_ttl(cmd_key, i);
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (cmd_ == "exists") {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = test_exists(cmd_key, i);
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (cmd_ == "type") {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = test_type(cmd_key, i);
|
2021-01-01 20:35:07 +08:00
|
|
|
} else if (cmd_ == "all") {
|
|
|
|
if (!test_set(cmd_string, i)
|
|
|
|
|| !test_get(cmd_string, i)
|
|
|
|
|| !test_exists(cmd_key, i)
|
|
|
|
|| !test_type(cmd_key, i)
|
|
|
|
|| !test_expire(cmd_key, i)
|
|
|
|
|| !test_ttl(cmd_key, i)
|
|
|
|
|| !test_del(cmd_key, i)) {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = false;
|
2021-01-01 20:35:07 +08:00
|
|
|
} else {
|
2020-07-20 17:27:32 +08:00
|
|
|
ret = true;
|
2021-01-01 20:35:07 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("unknown cmd: %s\r\n", cmd_.c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:35:07 +08:00
|
|
|
if (!ret) {
|
2020-07-20 17:27:32 +08:00
|
|
|
printf("cmd: %s error, tid: %lu\r\n",
|
|
|
|
cmd_.c_str(), thread_self());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-08 14:35:25 +08:00
|
|
|
/*
|
2021-01-01 20:35:07 +08:00
|
|
|
if (i > 0 && i % 1000 == 0) {
|
2020-07-20 17:27:32 +08:00
|
|
|
char tmp[128];
|
|
|
|
acl::safe_snprintf(tmp, sizeof(tmp), "%d", i);
|
|
|
|
acl::meter_time(__FILE__, __LINE__, tmp);
|
|
|
|
}
|
2021-01-08 14:35:25 +08:00
|
|
|
*/
|
2020-07-20 17:27:32 +08:00
|
|
|
|
|
|
|
cmd_string.clear();
|
|
|
|
cmd_key.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
locker_.lock();
|
|
|
|
__threads_exit++;
|
|
|
|
locker_.unlock();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
acl::locker& locker_;
|
|
|
|
acl::redis_client_pipeline& conns_;
|
|
|
|
acl::string cmd_;
|
2021-01-31 15:43:29 +08:00
|
|
|
int n_;
|
2020-07-20 17:27:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
2021-01-08 14:35:25 +08:00
|
|
|
"-s one_redis_addr[127.0.0.1:6379]\r\n"
|
2020-07-20 17:27:32 +08:00
|
|
|
"-n count[default: 10]\r\n"
|
2021-01-09 10:48:53 +08:00
|
|
|
"-t max_threads[default: 10]\r\n"
|
2020-07-20 17:27:32 +08:00
|
|
|
"-r retry_for_cluster_resnum[default: 10]\r\n"
|
2021-01-09 10:48:53 +08:00
|
|
|
"-p password [set the password of redis cluster]\r\n"
|
2021-01-31 15:43:29 +08:00
|
|
|
"-m [if use mbox in pipeline mode, default: false]\r\n"
|
2020-07-20 17:27:32 +08:00
|
|
|
"-a cmd[set|get|expire|ttl|exists|type|del]\r\n",
|
|
|
|
procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2021-01-09 10:48:53 +08:00
|
|
|
int ch, n = 1;
|
2020-07-20 17:55:12 +08:00
|
|
|
int max_threads = 10;
|
2022-11-21 20:27:09 +08:00
|
|
|
acl::box_type_t btype = acl::BOX_TYPE_TBOX;
|
2021-01-09 10:48:53 +08:00
|
|
|
acl::string addr("127.0.0.1:6379"), cmd("del"), passwd;
|
2020-07-20 17:27:32 +08:00
|
|
|
|
2021-01-31 15:43:29 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hs:n:t:a:p:m")) > 0) {
|
2021-01-01 20:35:07 +08:00
|
|
|
switch (ch) {
|
2020-07-20 17:27:32 +08:00
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
2021-01-08 14:35:25 +08:00
|
|
|
addr = optarg;
|
2020-07-20 17:27:32 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
n = atoi(optarg);
|
|
|
|
break;
|
2021-01-09 10:57:42 +08:00
|
|
|
case 't':
|
2020-07-20 17:27:32 +08:00
|
|
|
max_threads = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
cmd = optarg;
|
|
|
|
break;
|
2021-01-08 14:35:25 +08:00
|
|
|
case 'p':
|
2020-07-20 17:27:32 +08:00
|
|
|
passwd = optarg;
|
2021-01-31 15:43:29 +08:00
|
|
|
break;
|
|
|
|
case 'm':
|
2022-11-21 20:27:09 +08:00
|
|
|
btype = acl::BOX_TYPE_MBOX;
|
2021-01-31 15:43:29 +08:00
|
|
|
break;
|
2020-07-20 17:27:32 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
2022-11-21 20:27:09 +08:00
|
|
|
acl::redis_client_pipeline pipeline(addr, btype);
|
2021-01-09 10:48:53 +08:00
|
|
|
if (!passwd.empty()) {
|
|
|
|
pipeline.set_password(passwd);
|
|
|
|
}
|
2021-01-08 14:35:25 +08:00
|
|
|
pipeline.start_thread();
|
2020-07-20 17:27:32 +08:00
|
|
|
|
|
|
|
struct timeval begin;
|
|
|
|
gettimeofday(&begin, NULL);
|
|
|
|
|
|
|
|
acl::locker locker;
|
|
|
|
|
|
|
|
std::vector<test_thread*> threads;
|
2021-01-01 20:35:07 +08:00
|
|
|
for (int i = 0; i < max_threads; i++) {
|
2020-07-20 17:27:32 +08:00
|
|
|
test_thread* thread = new test_thread(locker, pipeline,
|
2022-11-21 20:27:09 +08:00
|
|
|
cmd.c_str(), n);
|
2020-07-20 17:27:32 +08:00
|
|
|
threads.push_back(thread);
|
|
|
|
thread->set_detachable(true);
|
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:35:07 +08:00
|
|
|
while (true) {
|
2020-07-20 17:27:32 +08:00
|
|
|
locker.lock();
|
2021-01-01 20:35:07 +08:00
|
|
|
if (__threads_exit == max_threads) {
|
2020-07-20 17:27:32 +08:00
|
|
|
locker.unlock();
|
|
|
|
printf("All threads over now!\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
locker.unlock();
|
|
|
|
|
|
|
|
//printf("max_threads: %d, threads_exit: %d\r\n",
|
|
|
|
// max_threads, __threads_exit);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<test_thread*>::iterator it = threads.begin();
|
2021-01-01 20:35:07 +08:00
|
|
|
for (; it != threads.end(); ++it) {
|
2020-07-20 17:27:32 +08:00
|
|
|
//(*it)->wait();
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval end;
|
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
|
|
|
|
long long int total = max_threads * n;
|
|
|
|
double inter = util::stamp_sub(&end, &begin);
|
|
|
|
printf("total %s: %lld, spent: %0.2f ms, speed: %0.2f\r\n", cmd.c_str(),
|
|
|
|
total, inter, (total * 1000) /(inter > 0 ? inter : 1));
|
|
|
|
|
2021-01-09 10:48:53 +08:00
|
|
|
pipeline.stop_thread();
|
|
|
|
|
2020-07-20 17:27:32 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
printf("enter any key to exit\r\n");
|
|
|
|
getchar();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|