test redis redis_pubsub.

This commit is contained in:
zhengshuxin 2023-05-14 22:59:42 +08:00
parent 4a52f59b50
commit aef4edb85a

View File

@ -1,114 +1,99 @@
#include "stdafx.h"
static acl::string __channel_prefix("test_channel");
static int __count = 0;
static bool test_subscribe(acl::redis_pubsub& redis, int n)
static bool get_msg(acl::redis_pubsub& redis, int cnt)
{
acl::string channel1, channel2;
int ret, i;
acl::string channel, msg;
for (i = 0; i < n; i++)
{
channel1.format("%s_1_%d", __channel_prefix.c_str(), i);
channel2.format("%s_2_%d", __channel_prefix.c_str(), i);
redis.clear();
ret = redis.subscribe(channel1.c_str(), channel2.c_str(), NULL);
if (ret <= 0)
{
printf("subscribe %s %s error(%s), ret: %d\r\n",
channel1.c_str(), channel2.c_str(),
redis.result_error(), ret);
return false;
}
else if (i < 10)
printf("subscribe %s %s ok\r\n", channel1.c_str(),
channel2.c_str());
}
printf(">>>subscribe total: %d\r\n", i * 2);
acl::string msg;
for (i = 0; i < n; i++)
{
channel1.clear();
for (int i = 0; i < cnt; i++) {
channel.clear();
msg.clear();
redis.clear();
if ((redis.get_message(channel1, msg)) == false)
{
printf("get_message error(%s)\r\n",
redis.result_error());
if ((redis.get_message(channel, msg)) == false) {
printf("get_message error(%s)\r\n", redis.result_error());
return false;
}
else if (i < 10)
printf("get one message: %s, channel: %s\r\n",
msg.c_str(), channel1.c_str());
channel2.clear();
msg.clear();
redis.clear();
if ((redis.get_message(channel2, msg)) == false)
{
printf("get_message error(%s)\r\n",
redis.result_error());
return false;
if (++__count < 10) {
printf("get message from channel %s, msg=%s\r\n",
channel.c_str(), msg.c_str());
}
else if (i < 10)
printf("get one message: %s, channel: %s\r\n",
msg.c_str(), channel2.c_str());
}
printf(">>>message total: %d\r\n", i * 2);
return true;
}
static bool test_publish(acl::redis_pubsub& redis, int n)
static bool test_subscribe(acl::redis_pubsub& redis, int n, int cnt)
{
acl::string channel, msg;
int ret, i;
acl::string channel;
for (i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
channel.format("%s_1_%d", __channel_prefix.c_str(), i);
msg.format("msg_1_%s", channel.c_str());
redis.clear();
ret = redis.publish(channel.c_str(), msg.c_str(), msg.length());
if (ret <= 0)
{
printf("publish to %s %s error(%s), ret: %d\r\n",
channel.c_str(), msg.c_str(),
redis.result_error(), ret);
int ret = redis.subscribe(channel, NULL);
if (ret <= 0) {
printf("subscribe %serror(%s), ret: %d\r\n",
channel.c_str(), redis.result_error(), ret);
return false;
} else if (i < 10) {
printf("subscribe %s ok\r\n", channel.c_str());
}
else if (i < 10)
printf("publish to %s %s ok\r\n", channel.c_str(),
msg.c_str());
channel.format("%s_2_%d", __channel_prefix.c_str(), i);
msg.format("msg_2_%s", channel.c_str());
redis.clear();
ret = redis.publish(channel.c_str(), msg.c_str(), msg.length());
if (ret <= 0)
{
printf("publish to %s %s error(%s), ret: %d\r\n",
channel.c_str(), msg.c_str(),
redis.result_error(), ret);
return false;
}
else if (i < 10)
printf("publish to %s %s ok\r\n", channel.c_str(),
msg.c_str());
}
printf(">>>publish total: %d\r\n", i * 2);
printf(">>>subscribe channels total: %d\r\n", n);
for (int i = 0; i < n; i++) {
if (get_msg(redis, cnt) == false) {
printf("get_msg error\r\n");
return false;
}
}
printf(">>>message total: %d\r\n", n * cnt);
return true;
}
static bool pub_msg(acl::redis_pubsub& redis, const char* channel, int n)
{
acl::string msg;
for (int i = 0; i < n; i++) {
redis.clear();
msg.format("msg_1_%s", channel);
int ret = redis.publish(channel, msg.c_str(), msg.length());
if (ret <= 0) {
printf("publish to %s %s error(%s), ret: %d\r\n",
channel, msg.c_str(),
redis.result_error(), ret);
return false;
}
if (++__count < 10) {
printf("publish ok, channel=%s, msg=%s\r\n",
channel, msg.c_str());
}
}
return true;
}
static bool test_publish(acl::redis_pubsub& redis, int n, int cnt)
{
acl::string channel;
for (int i = 0; i < n; i++) {
channel.format("%s_1_%d", __channel_prefix.c_str(), i);
if (!pub_msg(redis, channel, cnt)) {
return false;
}
}
printf(">>>publish total: %d\r\n", n * cnt);
return true;
}
@ -118,6 +103,7 @@ static void usage(const char* procname)
"-s redis_addr[127.0.0.1:6379]\r\n"
"-p passwd\r\n"
"-n count\r\n"
"-m msg_count\r\n"
"-C connect_timeout[default: 10]\r\n"
"-I rw_timeout[default: 0]\r\n"
"-c [use cluster mode]\r\n"
@ -127,14 +113,12 @@ static void usage(const char* procname)
int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 0;
int ch, n = 1, cnt = 10, conn_timeout = 10, rw_timeout = 0;
acl::string addr("127.0.0.1:6379"), cmd, passwd;
bool cluster_mode = false;
while ((ch = getopt(argc, argv, "hs:n:C:I:a:cp:")) > 0)
{
switch (ch)
{
while ((ch = getopt(argc, argv, "hs:n:m:C:I:a:cp:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
@ -144,6 +128,9 @@ int main(int argc, char* argv[])
case 'n':
n = atoi(optarg);
break;
case 'm':
cnt = atoi(optarg);
break;
case 'C':
conn_timeout = atoi(optarg);
break;
@ -171,8 +158,7 @@ int main(int argc, char* argv[])
acl::redis_client_cluster cluster;
cluster.set(addr.c_str(), 100, conn_timeout, rw_timeout);
if (!passwd.empty())
{
if (!passwd.empty()) {
client.set_password(passwd);
cluster.set_password("default", passwd);
}
@ -180,27 +166,28 @@ int main(int argc, char* argv[])
acl::redis_pubsub redis;
if (cluster_mode)
if (cluster_mode) {
redis.set_cluster(&cluster);
else
} else {
redis.set_client(&client);
}
bool ret;
if (cmd == "subscribe")
ret = test_subscribe(redis, n);
else if (cmd == "publish")
ret = test_publish(redis, n);
else
{
if (cmd == "subscribe") {
ret = test_subscribe(redis, n, cnt);
} else if (cmd == "publish") {
ret = test_publish(redis, n, cnt);
} else {
ret = false;
printf("unknown cmd: %s\r\n", cmd.c_str());
}
if (ret == true)
if (ret == true) {
printf("test OK!\r\n");
else
} else {
printf("test failed!\r\n");
}
#ifdef WIN32
printf("enter any key to exit\r\n");