You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

<?php

$prev_time = 0;
function tick()
{
        global $prev_time;

        $cur_time = microtime(true);

        if ($prev_time) $diff = $cur_time - $prev_time;
        else $diff = "";

        echo $cur_time . " (" . $diff . ")\n";

        $prev_time = $cur_time;
}

function redis_set($host,$port,$key,$value,$thread=false)
{
        $obj = [ "v" => $value ];
        $value = str_replace( "\"", "\\\"", json_encode( $obj, JSON_UNESCAPED_UNICODE));
        $cmd="redis-cli -h {$host} -p {$port} set \"{$key}\" \"{$value}\"";
        if ($thread==true) $cmd .= " &";

        $resp=trim( shell_exec( $cmd));

        if ($resp=="OK") return NULL;
        else
        {
                echo "{$resp}\n";
                return $resp;
        }
}

function redis_get($host,$port,$key)
{
        $cmd = "redis-cli -h {$host} -p {$port} get \"{$key}\"";
        $resp=trim(shell_exec( $cmd));
        $obj=json_decode($resp,JSON_UNESCAPED_UNICODE);
        return $obj['v'];
}

echo "Error testing\n";
echo redis_set("10.0.1.10",6379,"chun","kang1");
echo redis_get("10.0.1.10",6379,"chun"). "\n";

echo "begin!!\n";
$prev_time = 0;
tick();
redis_set("10.0.1.11",6379,"chun","kang1");
redis_set("10.0.1.12",6379,"chun","kang2");
redis_set("10.0.1.13",6379,"chun","kang3");
tick();
echo redis_get("10.0.1.11",6379,"chun"). "\n";
echo redis_get("10.0.1.12",6379,"chun"). "\n";
echo redis_get("10.0.1.13",6379,"chun"). "\n";
tick();

echo "\n";
echo "Run in Thread\n";

$prev_time = 0;
tick();
redis_set("10.0.1.11",6379,"chun","thread-kang1",true);
redis_set("10.0.1.12",6379,"chun","thread-kang2",true);
redis_set("10.0.1.13",6379,"chun","thread-kang3",true);
tick();
shell_exec("wait"); // wait until all workers to complete the job
echo redis_get("10.0.1.11",6379,"chun"). "\n";
echo redis_get("10.0.1.12",6379,"chun"). "\n";
echo redis_get("10.0.1.13",6379,"chun"). "\n";
tick();

?>
  • No labels