Skip to content

Instantly share code, notes, and snippets.

@ruckuus
Last active February 13, 2019 13:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruckuus/8683451 to your computer and use it in GitHub Desktop.
Save ruckuus/8683451 to your computer and use it in GitHub Desktop.
PHP Redis Client Test [Predis vs phpredis]
php redisTest.php
Keyword: free+book, score:736764
Keyword: pretty+picture, score:117600
Keyword: best+seller, score:411600
Keyword: stuff, score:2998800
Keyword: bedok, score:29400
Keyword: best+seller+free+book, score:176400
Keyword: best+seller, score:411600
Keyword: best+seller, score:411600
Keyword: free+book, score:736764
array(6) {
[0]=>
string(5) "bedok"
[1]=>
string(14) "pretty+picture"
[2]=>
string(21) "best+seller+free+book"
[3]=>
string(11) "best+seller"
[4]=>
string(9) "free+book"
[5]=>
string(5) "stuff"
}
Predis client test elapsed:69.226026535034 ms
Keyword: free+book, score:739270
Keyword: pretty+picture, score:118000
Keyword: best+seller, score:413000
Keyword: stuff, score:3009000
Keyword: bedok, score:29500
Keyword: best+seller+free+book, score:177000
Keyword: best+seller, score:413000
Keyword: best+seller, score:413000
Keyword: free+book, score:739270
array(4) {
["stuff"]=>
string(7) "3009000"
["free+book"]=>
string(6) "739270"
["best+seller"]=>
string(6) "413000"
["best+seller+free+book"]=>
string(6) "177000"
}
Redis client test elapsed: 13.724088668823 ms
<?php
require "vendor/autoload.php";
\Predis\Autoloader::register();
$redisConfig = array(
"scheme" => "tcp",
"host" => "127.0.0.1",
"port" => 6379
);
function emulateTopKeywords($c) {
$testData = array(
array(
"keywords" => "free+book",
"score" => 2303
),
array(
"keywords" => "pretty+picture",
"score" => 400
),
array(
"keywords" => "best+seller",
"score" => 1200
),
array(
"keywords" => "stuff",
"score" => 10200
),
array(
"keywords" => "bedok",
"score" => 100
),
array(
"keywords" => "best+seller+free+book",
"score" => 600
),
array(
"keywords" => "best+seller",
"score" => 100
),
array(
"keywords" => "best+seller",
"score" => 100
),
array(
"keywords" => "free+book",
"score" => 203
),
);
$sortedSets = "top_keywords";
if (is_object($c)) {
try {
// Delete sorted sets
/*
foreach($testData as $d) {
if ($c instanceOf \Predis\Client) {
$c->del($sortedSets);
} else {
$c->delete($sortedSets);
}
}*/
// Iterate thru test data, push to list
foreach($testData as $d) {
if ($c instanceOf \Predis\Client) {
$c->zincrby($sortedSets, $d["score"], $d["keywords"]);
} else {
$c->zIncrBy($sortedSets, $d["score"], $d["keywords"]);
}
}
foreach($testData as $d) {
echo "Keyword: ${d["keywords"]}, score:" . $c->zscore($sortedSets, $d["keywords"]) . "\n";
}
// Get whatever in the sets
if ($c instanceOf \Predis\Client) {
var_dump($c->zrangebyscore($sortedSets, 1, 10000000));
} else {
var_dump($c->zRevRangeByScore($sortedSets, "+inf", "-inf", array('withscores' => true, 'limit' => array(0, 4))));
}
} catch(Exception $e) {
echo $e->getMessage();
}
}
}
try {
$start = microtime(true);
$predis = new \Predis\Client($redisConfig);
emulateTopKeywords($predis);
$stop = microtime(true);
$el = ($stop - $start) * 1000;
echo "Predis client test elapsed:" . $el . " ms\n";
} catch (Exception $e) {
var_dump($e->getMessage());
}
try {
$start = microtime(true);
$redis = new Redis();
$redis->connect($redisConfig["host"], $redisConfig["port"]);
emulateTopKeywords($redis);
$stop = microtime(true);
echo "Redis client test elapsed: " . ((float)$stop - (float)$start) * 1000 . " ms\n";
} catch (Exception $e) {
var_dump($e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment