ebpf的主从同步实现,QPS测试与内存池QPS测试。

This commit is contained in:
1iaan
2026-01-30 16:00:06 +00:00
parent 2bdb48d63d
commit fbdcff6878
23 changed files with 599 additions and 383 deletions

View File

@@ -5,7 +5,8 @@
#include <hiredis/hiredis.h>
#define TIME_SUB_MS(tv1, tv2) ((tv1.tv_sec - tv2.tv_sec) * 1000 + (tv1.tv_usec - tv2.tv_usec) / 1000)
// #define PRINT printf
#define PRINT
static void die(redisContext *c, const char *msg) {
fprintf(stderr, "%s: %s\n", msg, c && c->err ? c->errstr : "unknown");
@@ -38,6 +39,7 @@ static void must_bulk_eq(redisReply *r, const void *buf, size_t n, const char *w
if (!r) { fprintf(stderr, "%s: reply null\n", what); exit(1); }
if (r->type != REDIS_REPLY_STRING || r->len != n || memcmp(r->str, buf, n) != 0) {
fprintf(stderr, "%s: bulk mismatch. type=%d len=%zu\n", what, r->type, r->len);
fprintf(stderr, "expect:%s, truely:%s\n", (const char*)buf, r->str);
freeReplyObject(r);
exit(1);
}
@@ -111,17 +113,18 @@ void pipline_set_test(redisContext *c, int start, int countN, const char *op){
die(c, "redisAppendCommand SET failed");
}
if(i%10000 == 0) printf("%d\n", i);
if(i%10000 == 0) PRINT("SEND: %d\n", i);
}
/* 再一次性把 N 个回复读出来 */
for (int i = start; i < end; i++) {
redisReply *r = NULL;
if (redisGetReply(c, (void**)&r) != REDIS_OK || !r) die(c, "redisGetReply SET failed");
must_ok(r, "pipeline SET reply");
if(i%10000 == 0) PRINT("RECV: %d\n", i);
}
printf("[OK] SET pipeline batch %d\n", N);
PRINT("[OK] SET pipeline batch %d\n", N);
}
void pipline_get_test(redisContext *c, int start, int countN, const char *op){
@@ -138,7 +141,7 @@ void pipline_get_test(redisContext *c, int start, int countN, const char *op){
die(c, "redisAppendCommand GET failed");
}
if(i%10000 == 0) printf("%d\n", i);
if(i%10000 == 0) PRINT("SEND: %d\n", i);
}
for (int i = start; i < end; i++) {
redisReply *r = NULL;
@@ -146,9 +149,11 @@ void pipline_get_test(redisContext *c, int start, int countN, const char *op){
char expect[64];
int en = snprintf(expect, sizeof(expect), "v:%d", i);
must_bulk_eq(r, expect, (size_t)en, "pipeline GET reply");
if(i%10000 == 0) PRINT("RECV: %d\n", i);
}
printf("[OK] GET pipeline batch %d\n", N);
PRINT("[OK] GET pipeline batch %d\n", N);
}
void pipline_del_test(redisContext *c, int start, int countN, const char *op){
@@ -165,17 +170,44 @@ void pipline_del_test(redisContext *c, int start, int countN, const char *op){
die(c, "redisAppendCommand DEL failed");
}
if(i%10000 == 0) printf("%d\n", i);
if(i%10000 == 0) PRINT("SEND: %d\n", i);
}
for (int i = start; i < end; i++) {
redisReply *r = NULL;
if (redisGetReply(c, (void**)&r) != REDIS_OK || !r) die(c, "redisGetReply DEL failed");
freeReplyObject(r); /* DEL 返回 int这里不强制检查 */
if(i%10000 == 0) PRINT("RECV: %d\n", i);
}
printf("[OK] DEL pipeline batch %d\n", N);
PRINT("[OK] DEL pipeline batch %d\n", N);
}
long long test_nopersist_noreplica(redisContext *c, int rounds, long long batch_size){
struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
long long total_ops = batch_size*rounds;
for(int i = 0;i < total_ops ; i += batch_size){
pipline_set_test(c, i, batch_size, "RSET");
}
for(int i = 0;i < total_ops ; i += batch_size){
pipline_get_test(c, i, batch_size, "RGET");
}
for(int i = 0;i < total_ops ; i += batch_size){
pipline_del_test(c, i, batch_size, "RDEL");
}
gettimeofday(&tv_end, NULL);
int time_used = TIME_SUB_MS(tv_end, tv_begin);
long long qps = total_ops *3*1000/time_used;
printf("BATCH (N=%lld) --> time_used=%d ms, qps=%lld\n", total_ops *3, time_used, qps);
return qps;
}
int main(int argc, char **argv) {
if(argc < 4) {
@@ -197,29 +229,18 @@ int main(int argc, char **argv) {
}else if(mode == 1){
basic_command_test(c);
}else if(mode == 3){
struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
long long ops = 100000;
pipline_set_test(c, 0, ops, "RSET");
int rounds = 10;
long long batch_size = 100000;
int testrounds = 50;
long long total_qps = 0;
gettimeofday(&tv_end, NULL);
int time_used = TIME_SUB_MS(tv_end, tv_begin);
long long qps = ops*1000/time_used;
printf("BATCH (N=%lld) --> time_used=%d ms, qps=%lld\n",
ops, time_used, qps);
for(int i = 0;i < testrounds; ++ i){
total_qps += test_nopersist_noreplica(c, rounds, batch_size);
}
printf("average qps:%lld\n", total_qps/testrounds);
}else if(mode == 4){
struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
long long ops = 100000;
pipline_del_test(c, 0, ops, "RDEL");
gettimeofday(&tv_end, NULL);
int time_used = TIME_SUB_MS(tv_end, tv_begin);
long long qps = ops*1000/time_used;
printf("BATCH (N=%lld) --> time_used=%d ms, qps=%lld\n",
ops, time_used, qps);
}else if(mode == 5){
}else if(mode == 10){
pipline_set_test(c, 0, 1000, "SET");