add new redis test files

This commit is contained in:
Guobao Jiang 2014-02-27 19:55:22 +08:00
parent 39ffc2ca4b
commit 67c432dcb5
16 changed files with 680 additions and 0 deletions

21
phpd/50x.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the <a href="http://nginx.org/r/error_log">error log</a> for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

9
phpd/create.php Normal file
View File

@ -0,0 +1,9 @@
<?php
require_once('mysql2redis.php');
$items_number=20000;
$tab_redis = new mysql2redis();
$tab_redis->setTBname('bench');
$tab_redis->setDBname('test');
$tab_redis->bench($items_number);
$tab_redis->bench($items_number, 'bench_no_index');
?>

19
phpd/create.sql Normal file
View File

@ -0,0 +1,19 @@
use test;
DROP TABLE IF EXISTS bench;
DROP TABLE IF EXISTS bench_no_index;
CREATE TABLE bench (
idx INT(10) NOT NULL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
des VARCHAR(100) NOT NULL
);
insert into bench value(0,'000','shanghai');
CREATE TABLE bench_no_index (
idx INT(10) NOT NULL,
name VARCHAR(20) NOT NULL,
des VARCHAR(100) NOT NULL
);
insert into bench_no_index value(0,'000','shanghai');

118
phpd/dbops.php Normal file
View File

@ -0,0 +1,118 @@
<?php
/***
* INTRO:
* this class is used to operate mysql
* database such as connect, query, release etc.
*
* USAGE: $idbops = new dbops();
* $dataArr = $idbops->query("sql sentence")
*
* VERSION:
* v0.1
* Guobao Jiang 2014-1-15
*/
class dbops {
private $host = '127.0.0.1:3306';
private $user = 'root';
private $pwd = 'linuxabornmysql';
private $dbname = 'test';
private $tbname = 'temp';
private $data = array();
private $mysqli = null;
public function connect()
{
$this->mysqli = new mysqli($this->host, $this->user, $this->pwd, $this->dbname);
if (mysqli_connect_errno()) {
echo "connect mysql and use table failed.";
die("connect error".mysql_error());
return false;
} else {
return true;
}
}
public function query($sql,$tag='select')
{
if ($this->connect()) {
if ($tag != 'select')
{
$this->mysqli->query($sql);
return true;
}
$result = $this->mysqli->query($sql);
while($row = $result->fetch_array()){
$this->data[] = $row;
}
return $this->getData();
} else {
return false;
}
}
public function close()
{
$this->mysqli->close();
}
public function getData()
{
return $this->data;
}
public function getDBname(){
return $this->dbname;
}
public function getTBname()
{
return $this->tbname;
}
public function setDBname($dbname)
{
$this->dbname = $dbname;
}
public function setTBname($tbname)
{
$this->tbname = $tbname;
}
public function connect_old()
{ # this function use old api, it will deprecated in the future
$link=mysql_connect($this->host,$this->user,$this->pwd);
if (!$link) {
echo "connect mysql failed.";
return false ;
}
if (!mysql_select_db($this->dbname, $link)) {
echo "select database failed.";
return false;
}
return $link;
}
public function query_old($mysql, $link)
{ # corresponding connect_old, it will deprecated in the future
$result = mysql_query($mysql, $link);
if (!$result) {
die('Query error.' . mysql_error());
return false;
}
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$this->data[] = $row;
}
mysql_free_result($result);
return $this->getData();
}
public function release($link)
{
mysql_close($link);
}
}
?>

25
phpd/index.html Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

12
phpd/index.php Normal file
View File

@ -0,0 +1,12 @@
<html>
<meta charset="utf-8">
<head>
<title>数据库调用测试页面</title>
</head>
<body>
<?php
require_once('main.php');
?>
</body>
</html>

89
phpd/iredis.php Normal file
View File

@ -0,0 +1,89 @@
<?php
/***
* INTRO:
* This class provides an interface to redis.
*
* VERSION
* 2014-02-27 1.0
*
* NOTE
* You should install phpredis first from
* https://code.google.com/p/phpredis/ or
* https://github.com/nicolasff/phpredis
*
* AUTHOR:
* Aborn Jiang
*
***/
class iredis
{
private $redis;
private $redis_server = '127.0.0.1';
private $redis_port = '6379';
public function __construct()
{
$this->redis = new redis();
$this->redis->pconnect($this->redis_server, $this->redis_port);
}
public function __destruct()
{
$this->redis->close();
}
public function configure($redis_server, $redis_port)
{
$this->redis_server = $redis_server;
$this->redis_port = $redis_port;
}
public function set($key, $value)
{
return $this->redis->set($key,$value);
}
public function get($key)
{
return $this->redis->get($key);
}
public function release($key)
{
return $this->redis->delete($key);
}
public function close()
{
$this->redis->close();
}
public function append($key, $value)
{
return $this->redis->append($key, $value);
}
public function hset($key, $keysub, $value)
{
return $this->redis->hSet($key, $keysub, $value);
}
public function del($key)
{
return $this->redis->delete($key);
}
public function delete($key)
{
return $this->redis->delete($key);
}
public function hgetall($key)
{
return $this->redis->hGetAll($key);
}
}
?>

31
phpd/iredis_test.php Normal file
View File

@ -0,0 +1,31 @@
<?php
/***
* use to test the clasee redis_face.php
*/
require_once('iredis.php');
function judge($status, $cmd)
{ if ($status==0 || $status == true) {
echo "<br/>";
echo $cmd." success.";
} else {
echo "<br/>";
echo $cmd." failed.";
var_dump($status);
}
}
echo "下面为测试redis的php接口类redis_face的各种命令.";
$rf = new iredis();
$status=$rf->set('lenovo','shanghai');
judge($status, "set");
$status=$rf->del('lenovo');
judge($status, "del");
$status=$rf->hset('go','shanghai','e2e2');
judge($status, "hset");
$status=$rf->delete('go');
judge($status, "delete");
?>

53
phpd/looptest.php Normal file
View File

@ -0,0 +1,53 @@
<?php
/***
* loop to test the redis
*
*/
require_once('dbops.php');
class looptest
{
private $tbname;
private $dbname;
public function configure($tbname,$dbname)
{
$this->tbname = $tbname;
$this->dbname = $dbname;
}
public function test_mysql($loop_times)
{
$tbname = $this->tbname;
$dbname = $this->dbname;
$start_time=microtime(true);
for ($i = 0; $i < $loop_times; $i++) {
$ops = new dbops();
$ops->setTBname($tbname);
$ops->setDBname($dbname);
$sql = sprintf("select * from %s", $ops->getTBname());
$data = $ops->query($sql);
$ops->close();
}
$end_time=microtime(true);
$dbtime=$end_time - $start_time;
return $dbtime;
}
public function test_redis($loop_times)
{
$start_time=microtime(true);
for ($i = 0; $i < $loop_times; $i++) {
## for ($j = 0; $j < 5; $j++){
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->hGetAll("$tbname");
## }
}
$end_time=microtime(true);
$rstime=$end_time - $start_time;
return $rstime;
}
}
?>

69
phpd/main.php Normal file
View File

@ -0,0 +1,69 @@
<?php
/***
* this main.php is main process and call other
* class and functions
*/
#require_once('mysql_info.php');
require_once('dbops.php');
echo "数据库测试页面\n <br/>";
date_default_timezone_set('PRC');
echo "当前时间:".date("Y-m-d H:i:s")."<br/>";
echo "数据库表如下:<br/>";
## following show the mysql database operation
$ops = new dbops();
#$ops->setDBname("greentea"); # change database
#$ops->setTBname("light_app");
$sql = sprintf("select * from %s", $ops->getTBname());
$data = $ops->query($sql);
$ops->close();
## show the $data in a form
$rows=count($data);
$cols=count($data[0])/2; # two kinds of index: id and type
#$cols=count($data[0]);
$td_width=floor(100/$cols)."%";
$tab_str="<table border=\"1\" width=\"80%\" align=\"left\">\n";
var_dump($data);
for($i=0;$i<$rows;$i++){
$tab_str.="<tr>\n";
$row_i = $data[$i];
for($k=0;$k<$cols;$k++){
$tab_str.="<td width=\"$td_width\">$row_i[$k]</td>\n";
}
$tab_str.="</tr>\n";
}
$tab_str.="</table>\n <br/>";
#echo "name * id * title <br/>";
#echo $_SERVER['DOCUMENT_ROOT'];
$image="test.jpg";
echo "<br/>";
echo '<img src="'.$image.'" height ="100"/><br />';
print $tab_str;
#echo "<p/>";
#var_dump($data);
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "follow test redis:";
$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
#var_dump($result); //结果bool(true)
$redis->set('test', 'hello world!');
echo $redis->get('test');
?>

116
phpd/mysql2redis.php Normal file
View File

@ -0,0 +1,116 @@
<?php
/***
* INTRO:
* this class provide an interface to redis
*
* 2014-02-27
*/
require_once('dbops.php');
require_once('iredis.php');
class mysql2redis
{
private $dbname = 'test';
private $tbname = 'account';
private $keycol = 0; # for mysql key
private $mysql;
private $mysql_data;
private $redis_data;
private $no_of_row;
private $no_of_col;
private $iredis;
public function __construct()
{
$this->iredis = new iredis();
$this->mysql = new dbops();
}
function __destruct()
{
if ( $this->mysql != NULL )
$this->mysql->close();
}
public function bench($items_number, $bench='bench')
{
$del_sql = sprintf("delete from %s where idx>0;", $bench);
$this->mysql->query($del_sql,'del');
for ($i=1; $i < $items_number; $i++)
{
$value = sprintf("(%d,\"name%d\",\"shanghai\")",$i,$i);
$insert_sql = sprintf("insert into %s values%s;",$bench,$value);
# echo $insert_sql;
$this->mysql->query($insert_sql,'del');
}
}
public function mysql2redis($keycol = 0)
{
$dbname = $this->dbname;
$tbname = $this->tbname;
$iredis = $this->iredis;
$this->mysql->setDBname($dbname);
$this->mysql->setTBname($tbname);
$sql = sprintf("select * from %s", $tbname);
$this->mysql_data = $this->mysql->query($sql);
if ($this->mysql_data == NULL)
{
return NULL;
}
$this->no_of_row = count($this->mysql_data);
$this->no_of_col = count($this->mysql_data[0])/2;
$iredis->del($tbname);
for($i = 0; $i < $this->no_of_row; $i++){
$row_i = $this->mysql_data[$i];
$keyi = $row_i[$this->keycol];
$coni = "";
for($k = 0; $k < $this->no_of_col; $k++){
$coni .= " $row_i[$k]";
}
$iredis->hset($tbname, $keyi, $coni);
}
$this->redis_data=$iredis->hgetall($tbname);
return $this->redis_data;
}
public function print_mysql_table()
{
$td_width = floor(100/$this->no_of_col)."%";
$tab_str = "<table border=\"1\" width=\"80%\" align=\"left\">\n";
for($i = 0; $i < $this->no_of_row; $i++){
$tab_str .= "<tr>\n";
$row_i = $this->mysql_data[$i];
for($k = 0; $k < $this->no_of_col; $k++){
$tab_str .= "<td width=\"$td_width\">$row_i[$k]</td>\n";
}
$tab_str .= "</tr>\n";
}
$tab_str.="</table>\n <br/>";
print $tab_str;
for($i = 0; $i < $this->no_of_row + 5; $i++){
echo "<br/>";
}
}
public function setTBname($tbname)
{
return $this->tbname = $tbname;
}
public function setDBname($dbname)
{
$this->dbname = $dbname;
}
}
?>

7
phpd/mysql_info.php Normal file
View File

@ -0,0 +1,7 @@
<?php
define(DB_HOST,'127.0.0.1:8801');
define(DB_USER,'root');
define(DB_PWD,'root');
define(DB_BASENAME,'test');
define(DB_TABLENAME,'temp');
?>

14
phpd/redis.php Normal file
View File

@ -0,0 +1,14 @@
<html>
<meta charset="utf-8">
<head>
<title>[redis]性能测试页面</title>
</head>
<body>
<?php
require_once('iredis_test.php');
require_once('redis_action.php');
?>
</body>
</html>

46
phpd/redis_action.php Normal file
View File

@ -0,0 +1,46 @@
<?php
/***
* redis.php is used to test redis's benchmark
* 2014-02-25
*/
require_once('dbops.php');
require_once('mysql2redis.php');
require_once('looptest.php');
#$tbname="account";
$tbname="bench_no_index";
$dbname="test";
$loop_times=10;
$items_number=30000;
$tab_redis = new mysql2redis();
$tab_redis->setTBname($tbname);
$tab_redis->setDBname($dbname);
$tab_redis->mysql2redis($tbname);
#$tab_redis->bench($items_number);
$lptest = new looptest();
$lptest->configure($tbname, $dbname);
echo "<br/>";
echo "redis性能测试页面";
$dbtime=$lptest->test_mysql($loop_times);
echo "<br/>"; echo "<br/>";
echo "[数据库".$dbname."".$tbname."]执行 $loop_times 次循环时间为:";
echo $dbtime;
$rstime=$lptest->test_mysql($loop_times);
echo "<br/>"; echo "<br/>";
echo "[redis对应表".$tbname." ]执行 $loop_times 次循环时间为:";
echo $rstime;
echo "<br/>"; echo "<br/>";
echo "item=$items_number [数据库]/[redis]比值为:";
echo $dbtime/$rstime;
#$tab_redis->print_mysql_table();
# var_dump($data);
?>

51
phpd/temp.sql Normal file
View File

@ -0,0 +1,51 @@
-- MySQL dump 10.13 Distrib 5.5.33, for Linux (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.5.33-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `temp`
--
DROP TABLE IF EXISTS `temp`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `temp` (
`name` varchar(50) DEFAULT NULL,
`id` int(10) DEFAULT NULL,
`title` varchar(100) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `temp`
--
LOCK TABLES `temp` WRITE;
/*!40000 ALTER TABLE `temp` DISABLE KEYS */;
INSERT INTO `temp` VALUES ('cat',1,'this is a cat'),('dog',2,'dog is animal'),('tiger',100,'tigher is the king of forest'),('lion',101,'lion eat dog'),('kitty',101,'carton animal');
/*!40000 ALTER TABLE `temp` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2014-02-13 14:29:22

BIN
phpd/test.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 KiB