首页 > 解决方案 > 为什么 redis-benchmark 性能与实际应用程序基准测试不同

问题描述

redis-benchmark -n 1000000 -t set -q -c 1

上述基准提供每秒 19k 的请求

但是我的 Nodejs Benchmarking for 'set' 命令每秒只提供 10k 请求

那么,与我的 nodejs 应用程序每秒 10k 请求相比,为什么 redis-benchmark 每秒有 19k 请求?

const Redis = require("ioredis");
const {
    performance
} = require('perf_hooks');

// Ready the Redis client A
const RedisAClient = new Redis({ host: "127.0.0.1", port: 6379 });

// Call our benchmarking
Benchmarking();


// Our benchmarking function
async function Benchmarking() {

    // Sleep async function
    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }


    const loopNumbers = 10000;
    let processedTasks = 0;
    const WholeTime = performance.now();

    for (let i = 0; i < loopNumbers; i++) {

        // Call redis for set command
         RedisAClient.set("testing", i, function (err, reply) {

            processedTasks++;
        });

        // If it is last loop then log the benchmark
        if (i == loopNumbers - 1) {

            // Wait for the all tasks to complete
            while (processedTasks != (loopNumbers)) { await sleep(1); }

            console.log((performance.now() - WholeTime) + "ms")

        }

    }

}

标签: node.jsredisbenchmarkingioredis

解决方案


推荐阅读