Skip to content

Instantly share code, notes, and snippets.

@consatan
Created November 25, 2021 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save consatan/530dacaef5379649f2eff8df0b9ed80a to your computer and use it in GitHub Desktop.
Save consatan/530dacaef5379649f2eff8df0b9ed80a to your computer and use it in GitHub Desktop.
Redis pop multip benchmark
<?php
// redis lua script use LRANGE and LTRIM
// redis-cli SCRIPT LOAD "local res; res = redis.call('LRANGE', KEYS[1], 0, ARGV[1] - 1); redis.call('LTRIM', KEYS[1], ARGV[1], -1); return res"
// script sha1: 163b2d3e271c4b0ca837426d9c0fffd9fc5bf591
// redis lua script use LPOP loop
// SCRIPT LOAD "local res = {}; for i=ARGV[1],1,-1 do table.insert(res, redis.call('LPOP', KEYS[1])); end; return res"
// script sha1: 015204a29f59eca28a9079f0246ec9a25871b031
// Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 1x16G 2666MHz
// Windows 10 Pro 21H1 19043.1288
// Docker for Windows 3.5.2(66501) Docker Engine: 20.10.7 WSL2 Debian 10.10
// Redis 4.0.14 (sha256:191c4017dcdd3370f871a4c6e7e1d55c7d9abed2bebf3005fb3e7d12161262b8)
// 2.699604 transaction with LRANGE -- atomic
// 18.660099 transaction with LPOP -- atomic
// 0.669863 pipeline with LRANGE -- not atomic, does not use
// 0.719762 pipeline with LPOP -- not atomic, but does not matter in this case
// 0.672208 lua script with LRANGE -- atomic
// 0.676807 lua script with LPOP -- atomic
// v.a. https://rafaeleyng.github.io/redis-pipelining-transactions-and-lua-scripts
$redis = new Redis();
$redis->connect('127.0.0.1');
$max = 1000;
$key = 'fifolist';
function init()
{
global $redis;
global $key;
global $max;
$redis->del($key);
for ($i = 0; $i < $max; $i++) {
$redis->rpush($key, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
}
}
init();
$start = microtime(true);
for ($i = 0; $i < $max; $i++) {
// result [['a', 'b', 'c', ..., 'z'], true];
$redis->multi(Redis::MULTI)->lrange($key, 0, 25)->ltrim($key, 26, -1)->exec();
}
echo sprintf('%0.6f transaction with LRANGE', microtime(true) - $start) . PHP_EOL;
init();
$start = microtime(true);
for ($i = 0; $i < $max; $i++) {
$trans = $redis->multi(Redis::MULTI);
for ($j = 0; $j < 26; $j++) {
$trans = $redis->lpop($key);
}
// result ['a', 'b', 'c', ..., 'z'];
$trans->exec();
}
echo sprintf('%0.6f transaction with LPOP', microtime(true) - $start) . PHP_EOL;
init();
$start = microtime(true);
for ($i = 0; $i < $max; $i++) {
// result [['a', 'b', 'c', ..., 'z'], true];
$redis->multi(Redis::PIPELINE)->lrange($key, 0, 25)->ltrim($key, 26, -1)->exec();
}
echo sprintf('%0.6f pipeline with LRANGE', microtime(true) - $start) . PHP_EOL;
init();
$start = microtime(true);
for ($i = 0; $i < $max; $i++) {
$trans = $redis->multi(Redis::PIPELINE);
for ($j = 0; $j < 26; $j++) {
$trans = $trans->lpop($key);
}
// result ['a', 'b', 'c', ..., 'z'];
$trans->exec();
}
echo sprintf('%0.6f pipeline with LPOP', microtime(true) - $start) . PHP_EOL;
init();
$start = microtime(true);
for ($i = 0; $i < $max; $i++) {
// result ['a', 'b', 'c', ..., 'z'];
$redis->evalSha('163b2d3e271c4b0ca837426d9c0fffd9fc5bf591', [$key, 26], 1);
}
echo sprintf('%0.6f lua script with LRANGE', microtime(true) - $start) . PHP_EOL;
init();
$start = microtime(true);
for ($i = 0; $i < $max; $i++) {
// result ['a', 'b', 'c', ..., 'z'];
$redis->evalSha('015204a29f59eca28a9079f0246ec9a25871b031', [$key, 26], 1);
}
echo sprintf('%0.6f lua script with LPOP', microtime(true) - $start) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment