>  기사  >  php教程  >  redis vs ssdb, hmset效率对比

redis vs ssdb, hmset效率对比

PHP中文网
PHP中文网원래의
2016-05-23 16:40:301984검색

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];
    }
}
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.