acl/lib_acl_cpp/samples/redis/redis_pipeline2/redis_pipeline.cpp

229 lines
5.1 KiB
C++
Raw Normal View History

2021-01-09 10:33:51 +08:00
#include "stdafx.h"
#include "util.h"
static int __threads_exit = 0;
class redis_command {
public:
2021-01-09 14:14:41 +08:00
redis_command(acl::redis_client_pipeline& pipeline, const char* key)
: key_(key)
, msg_(cmd_.get_pipeline_message())
2021-01-09 10:33:51 +08:00
{
cmd_.set_pipeline(&pipeline);
argc_ = 2;
argv_[0] = "del";
2021-01-09 14:14:41 +08:00
argv_[1] = key_;
2021-01-09 10:33:51 +08:00
lens_[0] = strlen(argv_[0]);
lens_[1] = strlen(argv_[1]);
2021-01-09 14:08:40 +08:00
// computer the hash slot for redis cluster node
cmd_.hash_slot(argv_[1]);
// build redis request command with the given args.
2021-01-09 10:33:51 +08:00
cmd_.build_request(argc_, argv_, lens_);
}
~redis_command(void) {}
acl::redis_pipeline_message& get_message(void) {
return msg_;
}
2021-01-09 14:08:40 +08:00
void clear(void) {
// we want to reuse the hash slot in next operation,
// so the parameter save_slot was set to true.
cmd_.clear(true);
}
2021-01-09 10:33:51 +08:00
private:
2021-01-09 14:14:41 +08:00
acl::string key_;
acl::redis cmd_;
2021-01-09 10:33:51 +08:00
acl::redis_pipeline_message& msg_;
size_t argc_;
const char* argv_[2];
size_t lens_[2];
};
class test_thread : public acl::thread {
public:
test_thread(acl::locker& locker, acl::redis_client_pipeline& pipeline,
2021-01-09 10:57:42 +08:00
int once_count, int count)
2021-01-09 10:33:51 +08:00
: locker_(locker)
, pipeline_(pipeline)
2021-01-09 10:57:42 +08:00
, once_count_(once_count)
, count_(count)
2021-01-09 10:33:51 +08:00
{
}
~test_thread(void) {}
protected:
// @override
void* run(void) {
2021-01-09 14:14:41 +08:00
acl::string key;
2021-01-09 14:08:40 +08:00
// parepare for a lot of redis commands in one request
2021-01-09 10:33:51 +08:00
std::vector<redis_command*> commands;
2021-01-09 10:57:42 +08:00
for (size_t i = 0; i < (size_t) once_count_; i++) {
2021-01-09 14:14:41 +08:00
key.format("test-key-%d", (int) i);
redis_command* command = new redis_command(pipeline_, key);
2021-01-09 10:33:51 +08:00
commands.push_back(command);
}
2021-01-09 10:57:42 +08:00
for (size_t i = 0; i < (size_t) count_; i++) {
2021-01-09 10:33:51 +08:00
request(commands);
}
2021-01-09 14:08:40 +08:00
// free all requests commands
2021-01-09 10:33:51 +08:00
for (std::vector<redis_command*>::iterator it = commands.begin();
it != commands.end(); ++it) {
delete *it;
}
locker_.lock();
__threads_exit++;
locker_.unlock();
return NULL;
}
private:
void request(std::vector<redis_command*>& commands) {
// send all request commands
for (std::vector<redis_command*>::iterator it = commands.begin();
it != commands.end(); ++it) {
acl::redis_pipeline_message& msg =
(*it)->get_message();
pipeline_.push(&msg);
}
// wait for all results
for (std::vector<redis_command*>::iterator it = commands.begin();
it != commands.end(); ++it) {
acl::redis_pipeline_message& msg =
(*it)->get_message();
const acl::redis_result* result = msg.wait();
if (result == NULL) {
printf("wait result error\r\n");
break;
}
2021-01-09 14:08:40 +08:00
// clear the temp memroy internal allocated by dbuf
(*it)->clear();
2021-01-09 10:33:51 +08:00
}
}
private:
acl::locker& locker_;
acl::redis_client_pipeline& pipeline_;
acl::string cmd_;
2021-01-09 10:57:42 +08:00
int once_count_;
int count_;
2021-01-09 10:33:51 +08:00
};
static void usage(const char* procname) {
printf("usage: %s -h[help]\r\n"
"-s one_redis_addr[127.0.0.1:6379]\r\n"
2021-01-09 10:57:42 +08:00
"-N once_count[default: 10]\r\n"
"-n loop_count[default: 10]\r\n"
"-t max_threads[default: 10]\r\n"
2021-01-09 10:33:51 +08:00
"-w wait_for_cluster_resume[default: 500 ms]\r\n"
"-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-09 10:33:51 +08:00
"-a cmd[set|get|expire|ttl|exists|type|del]\r\n",
procname);
}
int main(int argc, char* argv[]) {
2021-01-09 14:55:58 +08:00
int ch, count = 10, once_count = 10;
2021-01-09 10:33:51 +08:00
int max_threads = 10;
2021-01-09 10:57:42 +08:00
acl::string addr("127.0.0.1:6379"), passwd;
acl::string cmd("del");
2021-01-09 10:33:51 +08:00
2021-01-09 10:57:42 +08:00
while ((ch = getopt(argc, argv, "ha:s:N:n:t:p:")) > 0) {
2021-01-09 10:33:51 +08:00
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
2021-01-09 10:57:42 +08:00
case 'a':
cmd = optarg;
break;
2021-01-09 10:33:51 +08:00
case 's':
addr = optarg;
break;
2021-01-09 10:57:42 +08:00
case 'N':
once_count = atoi(optarg);
break;
2021-01-09 10:33:51 +08:00
case 'n':
2021-01-09 14:55:58 +08:00
count = atoi(optarg);
2021-01-09 10:33:51 +08:00
break;
2021-01-09 10:57:42 +08:00
case 't':
2021-01-09 10:33:51 +08:00
max_threads = atoi(optarg);
break;
case 'p':
passwd = optarg;
break;;
default:
break;
}
}
acl::acl_cpp_init();
acl::log::stdout_open(true);
acl::redis_client_pipeline pipeline(addr);
2021-01-09 10:48:53 +08:00
if (!passwd.empty()) {
pipeline.set_password(passwd);
}
2021-01-09 10:33:51 +08:00
pipeline.start_thread();
struct timeval begin;
gettimeofday(&begin, NULL);
acl::locker locker;
std::vector<test_thread*> threads;
for (int i = 0; i < max_threads; i++) {
test_thread* thread = new test_thread(locker, pipeline,
2021-01-09 14:55:58 +08:00
once_count, count);
2021-01-09 10:33:51 +08:00
threads.push_back(thread);
thread->set_detachable(true);
thread->start();
}
while (true) {
locker.lock();
if (__threads_exit == max_threads) {
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();
for (; it != threads.end(); ++it) {
//(*it)->wait();
delete (*it);
}
struct timeval end;
gettimeofday(&end, NULL);
2021-01-09 14:55:58 +08:00
long long int total = max_threads * once_count * count;
2021-01-09 10:33:51 +08:00
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();
2021-01-09 10:33:51 +08:00
#ifdef WIN32
printf("enter any key to exit\r\n");
getchar();
#endif
return 0;
}