accurate cpu usage in diagnostic tool

Former-commit-id: 8ee7584cffc5c5cacfb7ad20fc964112974683e4
This commit is contained in:
christianEQ 2021-06-17 18:32:11 +00:00
parent ea12db4433
commit fd72894fbf

View File

@ -863,6 +863,27 @@ int extractPropertyFromInfo(const char *info, const char *key, double &val) {
return 0; return 0;
} }
double getServerCpuTime(redisContext *ctx) {
redisReply *reply = (redisReply*)redisCommand(ctx, "INFO");
if (reply->type != REDIS_REPLY_STRING) {
freeReplyObject(reply);
printf("Error executing INFO command. Exiting.\r\n");
return -1;
}
double used_cpu_user, used_cpu_sys;
if (extractPropertyFromInfo(reply->str, "used_cpu_user", used_cpu_user)) {
printf("Error reading user CPU usage from INFO command. Exiting.\r\n");
return -1;
}
if (extractPropertyFromInfo(reply->str, "used_cpu_sys", used_cpu_sys)) {
printf("Error reading system CPU usage from INFO command. Exiting.\r\n");
return -1;
}
freeReplyObject(reply);
return used_cpu_user + used_cpu_sys;
}
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
int i; int i;
@ -898,11 +919,12 @@ int main(int argc, const char **argv) {
const char *set_value = "abcdefghijklmnopqrstuvwxyz"; const char *set_value = "abcdefghijklmnopqrstuvwxyz";
int threads_used = 0; int threads_used = 0;
unsigned int period = 5;
char command[63]; char command[63];
initBenchmarkThreads(); initBenchmarkThreads();
redisContext *ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket); redisContext *ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket);
double cpu_usage; double cpu_usage, last_cpu_usage = getServerCpuTime(ctx);
while (threads_used < config.max_threads) { while (threads_used < config.max_threads) {
printf("Creating %d clients for thread %d...\n", config.numclients, threads_used); printf("Creating %d clients for thread %d...\n", config.numclients, threads_used);
@ -920,20 +942,14 @@ int main(int argc, const char **argv) {
} }
threads_used++; threads_used++;
sleep(1); sleep(period);
redisReply *reply = (redisReply*)redisCommand(ctx, "INFO"); cpu_usage = getServerCpuTime(ctx);
if (reply->type != REDIS_REPLY_STRING) { if (cpu_usage < 0) {
freeReplyObject(reply);
printf("Error executing INFO command. Exiting.\r\n");
break; break;
} }
if (extractPropertyFromInfo(reply->str, "used_cpu_sys", cpu_usage)) { printf("CPU Usage: %.1f%%\r\n", (cpu_usage - last_cpu_usage) * 100 / period);
printf("Error reading CPU usage from INFO command. Exiting.\r\n"); last_cpu_usage = cpu_usage;
break;
}
printf("CPU Usage: %f\r\n", cpu_usage);
freeReplyObject(reply);
} }
printf("Done.\n"); printf("Done.\n");