自实现内存池:按大小分桶,8bit跨度,支持释放。

resp协议pipline测试。
This commit is contained in:
1iaan
2026-01-25 10:07:11 +00:00
parent ba2004c258
commit 9e757ece87
22 changed files with 515 additions and 11388 deletions

View File

@@ -4,6 +4,9 @@
#include <stdint.h>
#include <hiredis/hiredis.h>
#define TIME_SUB_MS(tv1, tv2) ((tv1.tv_sec - tv2.tv_sec) * 1000 + (tv1.tv_usec - tv2.tv_usec) / 1000)
static void die(redisContext *c, const char *msg) {
fprintf(stderr, "%s: %s\n", msg, c && c->err ? c->errstr : "unknown");
exit(1);
@@ -91,9 +94,9 @@ void save(redisContext *c){
printf("[OK] SAVE\n");
}
void pipline_set_test(redisContext *c, int start, const char *op){
void pipline_set_test(redisContext *c, int start, int countN, const char *op){
/* ---------- 3) Pipeline 批处理测试 ---------- */
const int N = 1000;
const int N = countN;
/* 一次塞 N 个 SET */
int end = start + N;
@@ -105,8 +108,11 @@ void pipline_set_test(redisContext *c, int start, const char *op){
op,
kk, (size_t)kn,
vv, (size_t)vn) != REDIS_OK) {
die(c, "redisAppendCommand SET failed");
die(c, "redisAppendCommand SET failed");
}
if(i%10000 == 0) printf("%d\n", i);
}
/* 再一次性把 N 个回复读出来 */
for (int i = start; i < end; i++) {
@@ -118,8 +124,8 @@ void pipline_set_test(redisContext *c, int start, const char *op){
printf("[OK] SET pipeline batch %d\n", N);
}
void pipline_get_test(redisContext *c, int start, const char *op){
const int N = 1000;
void pipline_get_test(redisContext *c, int start, int countN, const char *op){
const int N = countN;
/* pipeline GET + 校验 */
int end = start + N;
@@ -131,6 +137,8 @@ void pipline_get_test(redisContext *c, int start, const char *op){
kk, (size_t)kn) != REDIS_OK) {
die(c, "redisAppendCommand GET failed");
}
if(i%10000 == 0) printf("%d\n", i);
}
for (int i = start; i < end; i++) {
redisReply *r = NULL;
@@ -143,12 +151,12 @@ void pipline_get_test(redisContext *c, int start, const char *op){
printf("[OK] GET pipeline batch %d\n", N);
}
void pipline_del_test(redisContext *c, int start, const char *op){
const int N = 1000;
void pipline_del_test(redisContext *c, int start, int countN, const char *op){
const int N = countN;
/* cleanuppipeline DEL */
int end = start + N;
for (int i = start; i < end; i++) {
for (int i = start; i < end; i++) {
char kk[64];
int kn = snprintf(kk, sizeof(kk), "p:%d", i);
if (redisAppendCommand( c, "%s %b",
@@ -156,6 +164,8 @@ void pipline_del_test(redisContext *c, int start, const char *op){
kk, (size_t)kn) != REDIS_OK) {
die(c, "redisAppendCommand DEL failed");
}
if(i%10000 == 0) printf("%d\n", i);
}
for (int i = start; i < end; i++) {
redisReply *r = NULL;
@@ -186,27 +196,51 @@ int main(int argc, char **argv) {
save(c);
}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");
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 == 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 == 10){
pipline_set_test(c, 0, "SET");
pipline_set_test(c, 0, 1000, "SET");
}else if(mode == 11){
pipline_get_test(c, 0, "GET");
pipline_get_test(c, 0, 1000, "GET");
}else if(mode == 12){
pipline_del_test(c, 0, "DEL");
pipline_del_test(c, 0, 1000, "DEL");
}else if(mode == 20){
pipline_set_test(c, 0, "RSET");
pipline_set_test(c, 0, 1000, "RSET");
}else if(mode == 21){
pipline_get_test(c, 0, "RGET");
pipline_get_test(c, 0, 1000, "RGET");
}else if(mode == 22){
pipline_del_test(c, 0, "RDEL");
pipline_del_test(c, 0, 1000, "RDEL");
}else if(mode == 30){
pipline_set_test(c, 0, "HSET");
pipline_set_test(c, 0, 1000, "HSET");
}else if(mode == 31){
pipline_get_test(c, 0, "HGET");
pipline_get_test(c, 0, 1000, "HGET");
}else if(mode == 32){
pipline_del_test(c, 0, "HDEL");
pipline_del_test(c, 0, 1000, "HDEL");
}
redisFree(c);