基于PHP客户端的TokyoTyrant(TCH, TCB, TCT), Memcache, Mysql性能测试(测试脚本)

Mysql测试表创建

DROP TABLE IF EXISTS userinfo; CREATE TABLE userinfo ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), sex VARCHAR(10), province VARCHAR(30), city VARCHAR(30) ); ALTER TABLE userinfo ADD INDEX name_index (name); ALTER TABLE userinfo ADD INDEX sex_index (sex); ALTER TABLE userinfo ADD INDEX province_index (province); ALTER TABLE userinfo ADD INDEX city_index (city); DROP PROCEDURE IF EXISTS autoinsert; DELIMITER / CREATE PROCEDURE autoinsert(in num int, in base int) begin declare name_seed char(63) default "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; declare sex_seed char(2) default "mw"; declare province_seed char(10) default "ABCDEFGHIJ"; declare city_seed char(10) default "qrstuvwxyz"; declare i int default base; declare name_rd int default 0; declare sex_rd int default 0; declare province_rd int default 0; declare city_rd int default 0; while(i < num) do set name_rd = floor(rand() * 63) + 1; set sex_rd = floor(rand() * 2) + 1; set province_rd = floor(rand() * 10) + 1; set city_rd = floor(rand() * 10) + 1; INSERT INTO userinfo VALUES(i, concat(substring(name_seed, floor(rand() * 63), 1), substring(name_seed, floor(rand() * 63), 1), substring(name_seed, floor(rand() * 63), 1), substring(name_seed, floor(rand() * 63), 1), substring(name_seed, floor(rand() * 63), 1), substring(name_seed, floor(rand() * 63), 1)), substring(sex_seed, sex_rd, 1), substring(province_seed, province_rd, 1), substring(city_seed, city_rd, 1) ); set i = i + 1; end while; end / DELIMITER ;

 

TT使用tcrtest write -port 9001 localhost 200000000插入测试数据

 

基础功能函数

<?php require 'jpgraph/jpgraph.php'; require 'jpgraph/jpgraph_line.php'; function randChar() { static $chars = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ); return $chars[rand(0, 61)]; } function createKey($length = 20) { $str = ''; while($length -- > 0) $str .= randChar(); return $str; } function createValue($length = 200) { $str = ''; while($length -- > 0) $str .= randChar(); return $str; } function createNumberValue() { return rand(0, 2147483647); } function generateSeed($length) { $length = $length * $length; $result = ""; while($length -- > 0) $result .= randChar(); return $result; } function randString($length, &$seed, $n = 10) { return substr($seed, rand(0, strlen($seed) - $length - 1), $length); } function drawline($data1, $legend1, $data2, $legend2, $title, $xtitle, $ytitle) { // Create the graph and specify the scale for both Y-axis $graph = new Graph(800,600); $graph->SetScale('textlin'); $graph->SetY2Scale('lin'); $graph->SetShadow(); // Adjust the margin $graph->img->SetMargin(40,140,20,40); // Create the two linear plot $lineplot=new LinePlot($data1); $lineplot2=new LinePlot($data2); // Add the plot to the graph $graph->Add($lineplot); $graph->AddY2($lineplot2); $lineplot2->SetColor('orange'); $lineplot2->SetWeight(2); // Adjust the axis color $graph->y2axis->SetColor('orange'); $graph->yaxis->SetColor('blue'); $graph->title->Set($title); $graph->xaxis->title->Set($xtitle); $graph->yaxis->title->Set($ytitle); $graph->title->SetFont(FF_FONT1,FS_BOLD); $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD); $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD); // Set the colors for the plots $lineplot->SetColor('blue'); $lineplot->SetWeight(2); $lineplot2->SetColor('orange'); $lineplot2->SetWeight(2); // Set the legends for the plots $lineplot->SetLegend($legend1); $lineplot2->SetLegend($legend2); // Adjust the legend position $graph->legend->Pos(0.05,0.5,'right','center'); // Display the graph $graph->Stroke(); } function drawline1($datas) { $width = 800; $height = 600; $graph = new Graph($width, $height); $graph->SetScale('intlin'); $lineplot = new LinePlot($datas); $graph->Add($lineplot); $graph->Stroke(); } function drawline2($title, $xtitle, $ytitle) { $width = 1200; $height = 600; // Create the graph and set a scale. // These two calls are always required $graph = new Graph($width,$height); $graph->SetScale('intlin'); $graph->SetShadow(); $graph->img->SetMargin(60,350,20,40); // Setup margin and titles $graph->title->Set($title); $graph->xaxis->title->Set($xtitle); $graph->yaxis->title->Set($ytitle); $graph->yaxis->title->SetFont( FF_FONT1 , FS_BOLD ); $graph->xaxis->title->SetFont( FF_FONT1 , FS_BOLD ); $arg_num = func_num_args(); $i = 3; while($i < $arg_num) { $data = func_get_arg($i); $legend = $data['legend']; $data = $data['data']; $lineplot = new LinePlot($data); $lineplot->SetWeight(1); $lineplot->SetLegend($legend); $graph->Add($lineplot); $i ++; } $graph->legend->SetPos(0.05, 0.5, 'right', 'center'); // Display the graph $graph->Stroke(); } function getCurrentMemoryStr() { $a = system('free -m'); $matches = NULL; preg_match('/(/d+)/D+(/d+)/D+(/d+)/', $a, $matches); return "内存总量(M): ".$matches[1].", 已使用(M): ".$matches[2].", 空闲(M): ".$matches[3].";"; } function getCurrentMemory() { $a = NULL; exec('free -m', $a); $matches = NULL; preg_match('/(/d+)/D+(/d+)/D+(/d+)/', $a[1], $matches); return (int)$matches[2]; } ?>

Mysql工具类

<?php /** * Mysql工具类 * @author selfimpr * @blog http://blog.csdn.net/lgg201 * @email [email protected] * */ class Mysql { private $host; private $username; private $password; private $db_name; private $character_set; private $conn; public function __construct( $host = "localhost", $username = "root", $password = "root", $db_name = "test", $character_set = "GBK") { $this->host = $host; $this->username = $username; $this->password = $password; $this->db_name = $db_name; $this->character_set = $character_set; $this->createConn(); } public function createConn() { $this->conn = mysql_connect($this->host, $this->username, $this->password) or die('数据库连接失败'); mysql_select_db($this->db_name, $this->conn); mysql_set_charset($this->character_set, $this->conn); } public function getConn() { return $this->getConn(); } /** * * @param $sql 查询用的sql * @param $flag 标记是否把查询结果集遍历到数组 * @return */ public function execute($sql, $flag = true) { $resultset = mysql_query($sql, $this->conn) or die(mysql_error($this->conn)); if($flag) return ; if(!is_bool($resultset)) { while($line = mysql_fetch_assoc($resultset)) { $result[] = $line; } } else { $result = $resultset; } // echo "【".$sql."】 excute success!<br />"; return $result; } public function explain($sql) { return $this->execute('EXPLAIN '.$sql, false); } } ?>

TCT 设置索引

<?php function getClientTct($host = 'localhost', $port = 9003) { $tt = new TokyoTyrantTable(); $tt->connect($host, $port); return $tt; } $client = getClientTct(); $client->setIndex('name', TokyoTyrant::RDBIT_LEXICAL); $client->setIndex('sex', TokyoTyrant::RDBIT_LEXICAL); $client->setIndex('province', TokyoTyrant::RDBIT_LEXICAL); $client->setIndex('city', TokyoTyrant::RDBIT_LEXICAL); echo '索引设置成功'; ?>

所有产品写 入性能对比

<?php set_time_limit(0); require_once 'mysql_util.php'; require 'basic_funcs.php'; $current_test = $_GET['case']; $num = (int)$_GET['num']; $length = (int)$_GET['length']; function getClientMemcache($host = 'localhost', $port = 11211) { $memcache = new Memcache(); $memcache->connect($host, $port); return $memcache; } function getClientTch($host = 'localhost', $port = 9001) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTcb($host = 'localhost', $port = 9002) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTct($host = 'localhost', $port = 9003) { $tt = new TokyoTyrantTable(); $tt->connect($host, $port); return $tt; } function getClientMysql($host = "localhost", $username = "root", $password = "root", $db_name = "test", $character_set = "GBK") { $client = new Mysql($host, $username, $password, $db_name, $character_set); return $client; } function test_tch($num, $length) { $limit = floor($num / 20); $client = getClientTch(); $value = createValue($length); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->put($num, $value); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCH set test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tcb($num, $length) { $limit = floor($num / 20); $client = getClientTcb(); $value = createValue($length); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->put($num, $value); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCB set test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tct($num, $length) { $limit = floor($num / 20); $client = getClientTct(); $sexies = array('m', 'w'); $provinces = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $cities = array('q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $value = array('name' => createValue(6), 'sex' => $sexies[rand(0, 1)], 'province' => $provinces[rand(0, 9)], 'city' => $cities[rand(0, 9)]); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->put($num, $value); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCT set test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_memcache($num, $length) { $limit = floor($num / 20); $client = getClientMemcache(); $value = createValue($length); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->set($num, $value); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Memcache set test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_mysql($num, $length) { $limit = floor($num / 20); $client = getClientMysql(); $name = createValue($length); $sexies = array('m', 'w'); $provinces = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $cities = array('q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->execute("INSERT INTO userinfo VALUES(NULL, '".$name."', '".$sexies[rand(0, 1)]."', '".$provinces[rand(0, 9)]."', '".$cities[rand(0, 9)]."')"); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Mysql set test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test($num, $length) { $limit = floor($num / 20); drawline2("Mysql, TCT, TCB, TCH, Memcache compare in set", "Test case number", "run time per $limit record(S)", test_tch($num, $length), test_tcb($num, $length), test_tct($num, $length), test_memcache($num, $length), test_mysql($num, $length) ); } if(!$current_test) { test($num, $length); } else { $limit = floor($num / 20); drawline2("Mysql, TCT, TCB, TCH, Memcache compare in set", "Test case number", "run time per $limit record(S)", $current_test($num, $length) ); } ?>

所有产品读取性能对比

<?php set_time_limit(0); require_once 'mysql_util.php'; require 'basic_funcs.php'; $current_test = $_GET['case']; $num = (int)$_GET['num']; function getClientMemcache($host = 'localhost', $port = 11211) { $memcache = new Memcache(); $memcache->connect($host, $port); return $memcache; } function getClientTch($host = 'localhost', $port = 9001) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTcb($host = 'localhost', $port = 9002) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTct($host = 'localhost', $port = 9003) { $tt = new TokyoTyrantTable(); $tt->connect($host, $port); return $tt; } function getClientMysql($host = "localhost", $username = "root", $password = "root", $db_name = "test", $character_set = "GBK") { $client = new Mysql($host, $username, $password, $db_name, $character_set); return $client; } function test_tch($num) { $limit = floor($num / 20); $client = getClientTch(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->get($num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCH get test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tcb($num) { $limit = floor($num / 20); $client = getClientTcb(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->get($num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCB get test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tct($num) { $limit = floor($num / 20); $client = getClientTct(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->get($num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCT get test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_memcache($num) { $limit = floor($num / 20); $client = getClientMemcache(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->get($num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Memcache get test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_mysql($num) { $limit = floor($num / 20); $client = getClientMysql(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->execute("SELECT * FROM userinfo WHERE id = ".$num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Mysql get test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test($num) { $limit = floor($num / 20); drawline2("Mysql, TCT, TCB, TCH, Memcache compare in get", "Test case number", "run time per $limit record(S)", test_tch($num), test_tcb($num), test_tct($num), test_memcache($num), test_mysql($num) ); } test($num); ?>

搜索比较

<?php set_time_limit(0); require_once 'mysql_util.php'; require 'basic_funcs.php'; $current_test = $_GET['case']; $num = (int)$_GET['num']; $debug = $_GET['debug']; function getClientTct($host = '192.168.2.22', $port = 9003) { $tt = new TokyoTyrantTable(); $tt->connect($host, $port); return $tt; } function getClientMysql($host = "localhost", $username = "root", $password = "root", $db_name = "test", $character_set = "GBK") { $client = new Mysql($host, $username, $password, $db_name, $character_set); return $client; } function test_tct_name($num) { global $debug; $limit = floor($num / 20); $client = getClientTct(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $query = $client->getQuery(); $query->addCond('name', TokyoTyrant::RDBQC_STRINC, createValue(rand(1, 5))); $query->setLimit(10, 0); $begin = microtime(true); $query->search(); $end = microtime(true); if($debug) echo $query->hint().'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCT search by name test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_mysql_name($num) { global $debug; $limit = floor($num / 20); $client = getClientMysql(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $name = createValue(rand(1, 5)); $sql = "SELECT * FROM userinfo WHERE name LIKE '%$name%' limit 0, 10"; $begin = microtime(true); $client->execute($sql); $end = microtime(true); if($debug) echo $sql.'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Mysql search by name test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tct_sex($num) { global $debug; $limit = floor($num / 20); $client = getClientTct(); $times = array(); $tmp = $num; $total = 0; $sexies = array('m', 'w'); while($num -- >= 0) { $query = $client->getQuery(); $query->addCond('sex', TokyoTyrant::RDBQC_STREQ, $sexies[rand(0, 1)]); $query->setLimit(10, 0); $begin = microtime(true); $query->search(); $end = microtime(true); if($debug) echo $query->hint().'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCT search by sex test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_mysql_sex($num) { global $debug; $limit = floor($num / 20); $client = getClientMysql(); $times = array(); $tmp = $num; $total = 0; $sexies = array('m', 'w'); while($num -- >= 0) { $sex = $sexies[rand(0, 1)]; $sql = "SELECT * FROM userinfo WHERE sex = '$sex' limit 0, 10"; $begin = microtime(true); $client->execute($sql); $end = microtime(true); if($debug) echo $sql.'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Mysql search by sex test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tct_location($num) { global $debug; $limit = floor($num / 20); $client = getClientTct(); $times = array(); $tmp = $num; $total = 0; $provinces = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $cities = array('q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); while($num -- >= 0) { $query = $client->getQuery(); $query->addCond('province', TokyoTyrant::RDBQC_STREQ, $provinces[rand(0, 9)]); $query->addCond('city', TokyoTyrant::RDBQC_STREQ, $cities[rand(0, 9)]); $query->setLimit(10, 0); $begin = microtime(true); $query->search(); $end = microtime(true); if($debug) echo $query->hint().'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCT search by location test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_mysql_location($num) { global $debug; $limit = floor($num / 20); $client = getClientMysql(); $times = array(); $tmp = $num; $total = 0; $provinces = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $cities = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'); while($num -- >= 0) { $province = $provinces[rand(0, 9)]; $city = $cities[rand(0, 9)]; $sql = "SELECT * FROM userinfo WHERE province = '$province' AND city = '$city' limit 0, 10"; $begin = microtime(true); $client->execute($sql); $end = microtime(true); if($debug) echo $sql.'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Mysql search by location test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tct_all($num) { global $debug; $limit = floor($num / 20); $client = getClientTct(); $times = array(); $tmp = $num; $total = 0; $sexies = array('m', 'w'); $provinces = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $cities = array('q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); while($num -- >= 0) { $query = $client->getQuery(); $query->addCond('province', TokyoTyrant::RDBQC_STREQ, $provinces[rand(0, 9)]); $query->addCond('city', TokyoTyrant::RDBQC_STREQ, $cities[rand(0, 9)]); $query->addCond('sex', TokyoTyrant::RDBQC_STREQ, $sexies[rand(0, 1)]); $query->addCond('name', TokyoTyrant::RDBQC_STRINC, createValue(rand(1, 5))); $query->setLimit(10, 0); $begin = microtime(true); $query->search(); $end = microtime(true); if($debug) echo $query->hint().'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCT search by all test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_mysql_all($num) { global $debug; $limit = floor($num / 20); $client = getClientMysql(); $times = array(); $tmp = $num; $total = 0; $sexies = array('m', 'w'); $provinces = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $cities = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'); while($num -- >= 0) { $province = $provinces[rand(0, 9)]; $city = $cities[rand(0, 9)]; $sex = $sexies[rand(0, 1)]; $name = createValue(rand(1, 5)); $sql = "SELECT * FROM userinfo WHERE province = '$province' AND city = '$city' AND sex = '$sex' AND name like '%$name%' limit 0, 10"; $begin = microtime(true); $client->execute($sql); $end = microtime(true); if($debug) echo $sql.'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Mysql search by location test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_tct_regexp($num) { global $debug; $limit = floor($num / 20); $client = getClientTct(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $query = $client->getQuery(); $query->addCond('name', TokyoTyrant::RDBQC_STRRX, '.*'.createValue(rand(1, 5)).'.*'); $query->setLimit(10, 0); $begin = microtime(true); $query->search(); $end = microtime(true); if($debug) echo $query->hint().'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "TCT search by regexp test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_mysql_regexp($num) { global $debug; $limit = floor($num / 20); $client = getClientMysql(); $times = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $name = createValue(rand(1, 5)); $sql = "SELECT * FROM userinfo WHERE name REGEXP '.*$name.*' limit 0, 10"; $begin = microtime(true); $client->execute($sql); $end = microtime(true); if($debug) echo $sql.'<br />'; $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); $total = 0; } } return array( 'legend' => "Mysql search by regexp test result ".(array_sum($times) / $tmp), 'data' => $times, ); } function test_name($num) { $limit = floor($num / 20); $data1 = test_tct_name($num); $data2 = test_mysql_name($num); global $debug; if($debug) return ; drawline2("Mysql, TCT compare in get by name", "Test case number", "run time per $limit record(S)", $data1, $data2 ); } function test_sex($num) { $limit = floor($num / 20); $data1 = test_tct_sex($num); $data2 = test_mysql_sex($num); global $debug; if($debug) return ; drawline2("Mysql, TCT compare in get by sex", "Test case number", "run time per $limit record(S)", $data1, $data2 ); } function test_location($num) { $limit = floor($num / 20); $data1 = test_tct_location($num); $data2 = test_mysql_location($num); global $debug; if($debug) return ; drawline2("Mysql, TCT compare in get by location", "Test case number", "run time per $limit record(S)", $data1, $data2 ); } function test_all($num) { $limit = floor($num / 20); $data1 = test_tct_all($num); $data2 = test_mysql_all($num); global $debug; if($debug) return ; drawline2("Mysql, TCT compare in get by all", "Test case number", "run time per $limit record(S)", $data1, $data2 ); } function test_regexp($num) { $limit = floor($num / 20); $data1 = test_tct_regexp($num); $data2 = test_mysql_regexp($num); global $debug; if($debug) return ; drawline2("Mysql, TCT compare in get by name", "Test case number", "run time per $limit record(S)", $data1, $data2 ); } $current_test($num); ?>

TCH, TCB, TCT的内存压力写入测试

<?php set_time_limit(0); require 'basic_funcs.php'; $current_test = $_GET['case']; $num = $_GET['num']; $length = $_GET['length']; function getClientTch($host = 'localhost', $port = 9001) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTcb($host = 'localhost', $port = 9002) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTct($host = 'localhost', $port = 9003) { $tt = new TokyoTyrantTable(); $tt->connect($host, $port); return $tt; } function test_tch($num, $length) { $limit = floor($num / 20); $value = createValue($length); $client = getClientTch(); $times = array(); $mems = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->put($num, $value); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); array_push($mems, getCurrentMemory()); $total = 0; } } drawline($times, "Time for ".$limit." record.", $mems, "Current memory used(M)", "TCH test for full memory(".(array_sum($times) / $tmp).")", "test case number", "Time or MemoryUsed"); } function test_tcb($num, $length) { $limit = floor($num / 20); $value = createValue($length); $client = getClientTcb(); $times = array(); $mems = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->put($num, $value); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); array_push($mems, getCurrentMemory()); $total = 0; } } drawline($times, "Time for ".$limit." record.", $mems, "Current memory used(M)", "TCB test for full memory(".(array_sum($times) / $tmp).")", "test case number", "Time or MemoryUsed"); } function test_tct($num, $length) { $limit = floor($num / 20); $value = array('value' => createValue($length)); $client = getClientTct(); $times = array(); $mems = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->put($num, $value); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); array_push($mems, getCurrentMemory()); $total = 0; } } drawline($times, "Time for ".$limit." record.", $mems, "Current memory used(M)", "TCT test for full memory(".(array_sum($times) / $tmp).")", "test case number", "Time or MemoryUsed"); } $current_test($num, $length); ?>

TCH, TCB, TCT的内存压力读取测试

<?php set_time_limit(0); require 'basic_funcs.php'; $current_test = $_GET['case']; $num = (int)$_GET['num']; function getClientTch($host = 'localhost', $port = 9001) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTcb($host = 'localhost', $port = 9002) { $tt = new TokyoTyrant(); $tt->connect($host, $port); return $tt; } function getClientTct($host = 'localhost', $port = 9003) { $tt = new TokyoTyrantTable(); $tt->connect($host, $port); return $tt; } function test_tch($num) { $limit = floor($num / 20); $client = getClientTch(); $times = array(); $mems = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->get($num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); array_push($mems, getCurrentMemory()); $total = 0; } } drawline($times, "Time for ".$limit." record.", $mems, "Current memory used(M)", "TCH test for full memory(".(array_sum($times) / $tmp).")", "test case number", "Time or MemoryUsed"); } function test_tcb($num) { $limit = floor($num / 20); $client = getClientTcb(); $times = array(); $mems = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->get($num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); array_push($mems, getCurrentMemory()); $total = 0; } } drawline($times, "Time for ".$limit." record.", $mems, "Current memory used(M)", "TCB test for full memory(".(array_sum($times) / $tmp).")", "test case number", "Time or MemoryUsed"); } function test_tct($num) { $limit = floor($num / 20); $client = getClientTct(); $times = array(); $mems = array(); $tmp = $num; $total = 0; while($num -- >= 0) { $begin = microtime(true); $client->get($num); $end = microtime(true); $total += $end - $begin; if(!($num % $limit)) { array_push($times, $total); array_push($mems, getCurrentMemory()); $total = 0; } } drawline($times, "Time for ".$limit." record.", $mems, "Current memory used(M)", "TCT test for full memory(".(array_sum($times) / $tmp).")", "test case number", "Time or MemoryUsed"); } $current_test($num); ?>

Mysql并发测试(ab -n 10000 -c 10000 http://localhost/concurrent_mysql.php)

#! /usr/local/bin/php <?php function test($num) { $times = array(); while($num -- > 0) { $begin = microtime(true); $conn = mysql_connect('localhost', 'root', 'root', 'test'); mysql_select_db('test', $conn); mysql_set_charset('GBK', $conn); $end = microtime(true); $times['open'] += $end - $begin; $begin = microtime(true); mysql_query("UPDATE userinfo SET name = 'hello' WHERE id <= 10"); $end = microtime(true); $times['update'] += $end - $begin; $begin = microtime(true); mysql_query("SELECT * FROM userinfo LIMIT 0, 10"); $end = microtime(true); $times['query'] += $end - $begin; $begin = microtime(true); mysql_query("INSERT INTO userinfo VALUES(NULL, 'hello', 'm', 'A', 'x'"); $end = microtime(true); $times['insert'] += $end - $begin; $begin = microtime(true); mysql_close($conn); $end = microtime(true); $times['close'] += $end - $begin; } echo "{open: ".$times['open'].", update: ".$times['update'].", query: ".$times['query'].", insert: ".$times['insert'].", close: ".$times['close']."}"; } test($_GET['num'] ? $_GET['num'] : 100000); ?>

你可能感兴趣的:(mysql,PHP,function,脚本,测试,character)