当前位置 : 主页 > 网络编程 > PHP >

redis vs ssdb, hmset效率对比

来源:互联网 收集:自由互联 发布时间:2021-06-30
?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;
 
<?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('127.0.0.1', 6379);
        $this->test($redis);
   
        //测试ssdb->hmset()
        $this->printf("ssdb test start");
        $redis = new redis();
        $redis->connect('127.0.0.1', 8888);
        $this->test($redis);
    }
   
    function test($redis)
    {
        //导出一条测试记录
        $row = array (
          'id' => '1',
          'product' => '1',
          'imei' => '000000000000000',
          'model' => 'Galaxy Note 3 - 4.4.2 - API 19 - 1080x1920',
          'vcode' => '6',
          'vcoded' => '11',
          'channel' => '10001',
          'download' => '0',
          'ctime' => '1395992425',
          'year' => '2014',
          'month' => '201403',
          'week' => '201413',
          'day' => '20140328',
          'day1' => '1',
          'day2' => '1',
          'day3' => '1',
          'day4' => '1',
          'day5' => '1',
          'day6' => '1',
          'day7' => '1',
          'day14' => '1',
          'day30' => '0',
          'day60' => '0',
          'vcode_1' => '0',
          'vcode_2' => '0',
          'vcode_3' => '0',
          'vcode_6' => '20140710',
          'vcode_7' => '20140331',
          'vcode_8' => '0',
          'vcode_9' => '20140414',
          'vcode_10' => '0',
          'vcode_11' => '20140710',
          'vcode_12' => '0',
          'vcode_13' => '0',
          'vcode_14' => '0',
          'vcode_15' => '0',
          'vcode_16' => '0',
          'vcode_17' => '0',
          'vcode_18' => '0',
          'vcode_19' => '0',
          'last_act_year' => '2014',
          'last_act_month' => '201407',
          'last_act_week' => '201428',
          'last_act_day' => '20140710',
          'offday' => '77',
          'lose' => '1',
        );
   
        $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('b ','kb','mb','gb','tb','pb');
        return sprintf("%02f", @round($size/pow(1024,($i=floor(log($size,1024)))), 2)).' '.$unit[$i];
    }
}

网友评论