<?php

class mcTest {

    const 
KEYS_COUNT 1000;

    public function 
__construct($mc) {
        
$this->mc $mc;
    }
    
    public function 
run() {
        
$this->mc->flush();
        
$data          $this->makeData();
        
$keysExists    array_keys($data);
        
$keysNotExists $this->makeNotExistsKeys();
        
$this->result = array(
            
'set-not'       => $this->testSet($data),
            
'set-exists'    => $this->testSet($data),
            
'get-exists'    => $this->testGet($keysExists),
            
'get-not'       => $this->testGet($keysNotExists),
            
'delete-exists' => $this->testDelete($keysExists),
            
'delete-not'    => $this->testDelete($keysNotExists),
        );
        foreach (
$this->result as &$result) {
            
$result number_format(($result self::KEYS_COUNT) * 10000000','' ');
        }
        return 
true;
    }
    
    public function 
getResult() {
        return 
$this->result;
    }
    
    private function 
testSet($data) {
        
$mc $this->mc;
        
$mt microtime(true);
        foreach (
$data as $key => $value) {
            
$mc->set($key$value);
        }
        return 
microtime(true) - $mt;
    }    
    
    private function 
testGet($keys) {
        
$mc $this->mc;
        
$mt microtime(true);
        foreach (
$keys as $key) {
            
$r $mc->get($key);
        }
        return 
microtime(true) - $mt;
    }
    
    private function 
testDelete($keys) {
        
$mc $this->mc;
        
$mt microtime(true);
        foreach (
$keys as $key) {
            
$mc->delete($key);
        }
        return 
microtime(true) - $mt;
    }
    
    private function 
makeData() {
        
$data = array();
        for (
$i 0$i self::KEYS_COUNT$i++) {
            
$hash  md5($i);
            
$key   $this->hash2key($hash$i);
            
$value substr($key020);
            
$data[$key] = $value;
        }
        return 
$data;
    }
    
    private function 
makeNotExistsKeys() {
        
$keys = array();
        
$start self::KEYS_COUNT;
        
$stop  $start self::KEYS_COUNT;
        for (
$i $start$i $stop$i++) {
            
$hash   md5($i);
            
$key    $this->hash2key($hash$i);
            
$keys[] = $key;
        }
        return 
$keys;
    }
    
    private function 
hash2key($hash$i) {
        return 
'test:'.substr($hash05);
    }

    private 
$mc;
    
    private 
$result;
}

$mcd = new Memcached();
$mcd->addServer('localhost'11211);

$mce = new Memcache();
$mce->addServer('localhost'11211);

$testMemcached = new mcTest($mcd);
$testMemcache  = new mcTest($mce);

$testMemcached->run();
$testMemcache->run();


$rme $testMemcache->getResult();
$rmd $testMemcached->getResult();

?>
<table>
    <tr>
        <td></td>
        <td><b>Memcache</b></td>
        <td><b>Memcached</b></td>
    </tr>
    <tr>
        <td><b>set-new</b></td>
        <td><?php echo $rme['set-not']; ?></td>
        <td><?php echo $rmd['set-not']; ?></td>
    </tr>
    <tr>
        <td><b>set-exists</b></td>
        <td><?php echo $rme['set-exists']; ?></td>
        <td><?php echo $rmd['set-exists']; ?></td>
    </tr>
    <tr>
        <td><b>get-exists</b></td>
        <td><?php echo $rme['get-exists']; ?></td>
        <td><?php echo $rmd['get-exists']; ?></td>
    </tr>
    <tr>
        <td><b>get-empty</b></td>
        <td><?php echo $rme['get-not']; ?></td>
        <td><?php echo $rmd['get-not']; ?></td>
    </tr>
    <tr>
        <td><b>delete-exists</b></td>
        <td><?php echo $rme['delete-exists']; ?></td>
        <td><?php echo $rmd['delete-exists']; ?></td>
    </tr>
    <tr>
        <td><b>delete-empty</b></td>
        <td><?php echo $rme['delete-not']; ?></td>
        <td><?php echo $rmd['delete-not']; ?></td>
    </tr>
</table>