用于 hget 和 hset 命令的 Redis 基准测试

Posted

技术标签:

【中文标题】用于 hget 和 hset 命令的 Redis 基准测试【英文标题】:Redis benchmarking for hget and hset commands 【发布时间】:2014-05-14 09:22:57 【问题描述】:

我找不到使用 redis 对 HGET 、 HSET (哈希表命令)进行基准测试的示例。任何示例或资源都会对此有用。

【问题讨论】:

【参考方案1】:

我刚刚意识到redis-benchmark 命令不能对hSethGet 命令进行基准测试。 (我使用的是 v2.8.5)

你可以做的是编写一个小程序来对性能进行基准测试:

<?php

$redis = new Redis();
$redis->pconnect("127.0.0.1");

$count = 10000;

$start_t = microtime(true);
for ($i = 1; $i < $count; $i++) 
    $redis->hSet("h$i", 'f', $i);

$end_t = microtime(true);

echo "Time taken for hSet = " . round(1000 * ($end_t - $start_t)) . "ms (for " . number_format($count) . " keys)\n";

$start_t = microtime(true);
$pipeline1 = $redis->pipeline();
for ($i = 1; $i < $count; $i++) 
    $pipeline1->hSet("h$i", 'f', $i);

$result2 = $pipeline1->exec();
$end_t = microtime(true);

echo "Time taken for hSet (bulk) = " . round(1000 * ($end_t - $start_t)) . "ms (for " . number_format($count) . " keys)\n";

$start_t = microtime(true);
for ($i = 1; $i < $count; $i++) 
    $redis->hGet("h$i", 'f');

$end_t = microtime(true);

echo "Time taken for hGet = " . round(1000 * ($end_t - $start_t)) . "ms (for " . number_format($count) . " keys)\n";

$start_t = microtime(true);
$pipeline2 = $redis->pipeline();
for ($i = 1; $i < $count; $i++) 
    $pipeline2->hGet("h$i", 'f');

$result2 = $pipeline2->exec();
$end_t = microtime(true);

echo "Time taken for hGet (bulk) = " . round(1000 * ($end_t - $start_t)) . "ms (for " . number_format($count) . " keys)\n";


$start_t = microtime(true);
$pipeline3 = $redis->pipeline();
for ($i = 1; $i < $count; $i++) 
    $pipeline3->hDel("h$i", 'f');

$result3 = $pipeline3->exec();
$end_t = microtime(true);

echo "Time taken for hDel (bulk) = " . round(1000 * ($end_t - $start_t)) . "ms (for " . number_format($count) . " keys)\n";

在我的测试服务器上,结果如下:

$ php redis/benchmark_redis.php Time taken for hSet = 557ms (for 10,000 keys) Time taken for hSet (bulk) = 51ms (for 10,000 keys) Time taken for hGet = 483ms (for 10,000 keys) Time taken for hGet (bulk) = 43ms (for 10,000 keys) Time taken for hDel (bulk) = 49ms (for 10,000 keys)

【讨论】:

【参考方案2】:

我用的是3.0.2,下面的命令可以对hset redisredis-benchmark -h 192.168.22.58 -p 6379 -d 100 -c 10 -n 1000000 hset myhash rand_int rand_string进行基准测试

【讨论】:

-d 开关在指定命令时无效 - 它仅用于 redis-benchmark 中包含的测试

以上是关于用于 hget 和 hset 命令的 Redis 基准测试的主要内容,如果未能解决你的问题,请参考以下文章

PHP-redis命令之 散列(hashes)

redis --- Hash类型的使用

Redis之Hash

hset和hget

redis 常用命令

redis语法