首頁 > php教程 > PHP源码 > 主體

redis vs ssdb, hmset效率对比

PHP中文网
發布: 2016-05-23 16:40:30
原創
1981 人瀏覽過

php代码

<?php
$test = new redis_test();
$test->totalSize = 100000;
$test->run();
 
class redis_test
{
    public $totalSize=1000000;
 
    function printf()
    {
      $args = func_get_args();
      if(count($args) == 1){
        $msg = $args[0].PHP_EOL;
      }else{
        $args[0] = $args[0].PHP_EOL;
        $msg = call_user_func_array("sprintf", $args);
      }
 
      echo $msg;
    }
 
    function run()
    {
        //测试redis->hmset()
        $this->printf("redis test start");
        $redis = new redis();
        $redis->connect(&#39;127.0.0.1&#39;, 6379);
        $this->test($redis);
 
        //测试ssdb->hmset()
        $this->printf("ssdb test start");
        $redis = new redis();
        $redis->connect(&#39;127.0.0.1&#39;, 8888);
        $this->test($redis);
    }
 
    function test($redis)
    {
        //导出一条测试记录
        $row = array (
          &#39;id&#39; => &#39;1&#39;,
          &#39;product&#39; => &#39;1&#39;,
          &#39;imei&#39; => &#39;000000000000000&#39;,
          &#39;model&#39; => &#39;Galaxy Note 3 - 4.4.2 - API 19 - 1080x1920&#39;,
          &#39;vcode&#39; => &#39;6&#39;,
          &#39;vcoded&#39; => &#39;11&#39;,
          &#39;channel&#39; => &#39;10001&#39;,
          &#39;download&#39; => &#39;0&#39;,
          &#39;ctime&#39; => &#39;1395992425&#39;,
          &#39;year&#39; => &#39;2014&#39;,
          &#39;month&#39; => &#39;201403&#39;,
          &#39;week&#39; => &#39;201413&#39;,
          &#39;day&#39; => &#39;20140328&#39;,
          &#39;day1&#39; => &#39;1&#39;,
          &#39;day2&#39; => &#39;1&#39;,
          &#39;day3&#39; => &#39;1&#39;,
          &#39;day4&#39; => &#39;1&#39;,
          &#39;day5&#39; => &#39;1&#39;,
          &#39;day6&#39; => &#39;1&#39;,
          &#39;day7&#39; => &#39;1&#39;,
          &#39;day14&#39; => &#39;1&#39;,
          &#39;day30&#39; => &#39;0&#39;,
          &#39;day60&#39; => &#39;0&#39;,
          &#39;vcode_1&#39; => &#39;0&#39;,
          &#39;vcode_2&#39; => &#39;0&#39;,
          &#39;vcode_3&#39; => &#39;0&#39;,
          &#39;vcode_6&#39; => &#39;20140710&#39;,
          &#39;vcode_7&#39; => &#39;20140331&#39;,
          &#39;vcode_8&#39; => &#39;0&#39;,
          &#39;vcode_9&#39; => &#39;20140414&#39;,
          &#39;vcode_10&#39; => &#39;0&#39;,
          &#39;vcode_11&#39; => &#39;20140710&#39;,
          &#39;vcode_12&#39; => &#39;0&#39;,
          &#39;vcode_13&#39; => &#39;0&#39;,
          &#39;vcode_14&#39; => &#39;0&#39;,
          &#39;vcode_15&#39; => &#39;0&#39;,
          &#39;vcode_16&#39; => &#39;0&#39;,
          &#39;vcode_17&#39; => &#39;0&#39;,
          &#39;vcode_18&#39; => &#39;0&#39;,
          &#39;vcode_19&#39; => &#39;0&#39;,
          &#39;last_act_year&#39; => &#39;2014&#39;,
          &#39;last_act_month&#39; => &#39;201407&#39;,
          &#39;last_act_week&#39; => &#39;201428&#39;,
          &#39;last_act_day&#39; => &#39;20140710&#39;,
          &#39;offday&#39; => &#39;77&#39;,
          &#39;lose&#39; => &#39;1&#39;,
        );
 
        $this->printf("Job start, %s, %s", $this->memory_get_usage(), date("Y-m-d H:i:s"));
        for($i=1; $i<=$this->totalSize; $i++)
        {
            $row["id"]    = $i;
            $row["imei"]  = md5(rand(). microtime(true));
            $row["model"] = rand();
 
            $redis->hmset($row["imei"], $row);
 
            if($i % 10000 === 0)
                $this->printf("Job %s done, %s, %s", $i, $this->memory_get_usage(), date("Y-m-d H:i:s"));
        }
 
        $this->printf("Job start, %s, %s", $this->memory_get_usage(), date("Y-m-d H:i:s"));
    }
 
    function memory_get_usage()
    {
        $size = memory_get_usage(true);
        $unit=array(&#39;b &#39;,&#39;kb&#39;,&#39;mb&#39;,&#39;gb&#39;,&#39;tb&#39;,&#39;pb&#39;);
        return sprintf("%02f", @round($size/pow(1024,($i=floor(log($size,1024)))), 2)).&#39; &#39;.$unit[$i];
    }
}
登入後複製
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板