0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpredis で、hMSetは pipelineでhSetを大量に投げつけるのよりどのくらい早いかベンチマークとってみた。

Posted at

ハッシュ型のhMSetが、hSetをたくさん呼ぶのと比べてどのくらい早いのか気になったので、ベンチマークとってみました。

各種バージョンは以下の通り。

software version
PHP 5.6.24
phpredis 2.2.8
redis-server 2.8.19
// hMSet // 3.4 sec
bench(function($redis, $target){
  return $redis->hMSet("a", $target);
});

// foreach($arr){set} // 30 sec
bench(function($redis, $target){
  $res = true;
  foreach($target as $key => $value) {
    $res = ($res &&  $redis->hSet("a", $key, $value));
  }
  return $res;
});

// Redis::MULTI  // 33 sec
bench(function($redis, $target){
  $multi = $redis->multi();
  foreach($target as $key => $value) {
    $multi->hSet("a", $key, $value);
  }
  return $multi->exec();
});

// Redis::PIPELINE  // 7 sec
bench(function($redis, $target){
  $multi = $redis->multi(Redis::PIPELINE);
  foreach($target as $key => $value) {
    $multi->hSet("a", $key, $value);
  }
  return $multi->exec();
});

function bench($func) {
  $redis = new Redis();
  $redis->connect('/tmp/redis.sock');
  $redis->select(254);
  $target = range(1, 1000);

  $st = microtime(true);
  for($i = 0; $i < 100; $i++) {
    $redis->del("a");
    $res = $func($redis, $target);
  }
  echo microtime(true) - $st . PHP_EOL;
}

pipelineの倍くらい早いです。普通にhSetをforeachしただけだと、10倍位差がでてますねー。

使ったマシンのスペック

redis-benchmark の結果だけ貼っておくので、これで察してください。

[sitri@ndxbn:~]redis-benchmark -q
PING_INLINE: 64683.05 requests per second
PING_BULK: 58997.05 requests per second
SET: 56338.03 requests per second
GET: 61766.52 requests per second
INCR: 59136.61 requests per second
LPUSH: 59701.50 requests per second
LPOP: 60459.49 requests per second
SADD: 60168.47 requests per second
SPOP: 62774.64 requests per second
LPUSH (needed to benchmark LRANGE): 60569.35 requests per second
LRANGE_100 (first 100 elements): 30202.36 requests per second
LRANGE_300 (first 300 elements): 12763.24 requests per second
LRANGE_500 (first 450 elements): 8623.66 requests per second
LRANGE_600 (first 600 elements): 6710.96 requests per second
MSET (10 keys): 45662.10 requests per second

ほかやりたいこと

Set型のsAdd()とかZSetのzAdd()とかだと可変引数ですし、そっちもやりたい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?