/**
  * 测试CPU消耗
  */
 public function testPerformance()
 {
     /**
      * 运行500次,要求在1秒内运行完
      */
     $times = 500;
     $file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid();
     $bf = new LtBloomFilter();
     $bf->setBucketSize(PHP_INT_MAX);
     $bf->setSyncThreshHold($times + 1);
     $bf->setImageFile($file);
     $bf->init();
     $base_memory_usage = memory_get_usage();
     $startTime = microtime(true);
     for ($i = 0; $i < $times; $i++) {
         $str = uniqid();
         $bf->add($str);
         $bf->has($str);
     }
     $endTime = microtime(true);
     $memory_usage = memory_get_usage() - $base_memory_usage;
     unlink($file);
     $totalTime = round($endTime - $startTime, 6);
     $averageTime = round($totalTime / $times, 6);
     $averageMemory = formatSize($memory_usage / $times);
     $memory_usage = formatSize($memory_usage);
     if (LOTUS_UNITTEST_DEBUG) {
         echo "\n---------------------Bloom Filter--------------------------\n";
         echo "times      \t{$times}\n";
         echo "totalTime   \t{$totalTime}s\taverageTime   \t{$averageTime}s\n";
         echo "memoryUsage \t{$memory_usage}\taverageMemory \t{$averageMemory}";
         echo "\n---------------------------------------------------------\n";
     }
     $this->assertTrue(1 > $totalTime);
 }
Ejemplo n.º 2
0
 public function testAddAndHas()
 {
     $bf = new LtBloomFilter();
     // 2. 设置属性
     $bf->setBucketSize(PHP_INT_MAX);
     $bf->setSyncThreshHold(PHP_INT_MAX);
     $file = sys_get_temp_dir() . "/bf-unittest-" . uniqid() . ".bloom";
     $bf->setImageFile($file);
     // 3. 调init()方法
     $bf->init();
     //初始化完毕,测试其效果
     for ($i = 0; $i < 500; $i++) {
         $str = $this->randomString(16);
         $this->assertFalse($bf->has($str));
         $bf->add($str);
         $this->assertTrue($bf->has($str));
     }
     unlink($file);
 }
Ejemplo n.º 3
0
 /**
  * @dataProvider dpBadHasDataType
  */
 public function testBadHasDataType($para)
 {
     $bf = new LtBloomFilter();
     $bf->setBucketSize(1024);
     $bf->init();
     $this->assertFalse($bf->has($para));
 }