This test code is based on yii2 framework, apache server, apache, mysql, memcache are all installed on the same machine.
The test business content displays a list of articles, as shown below:
Test 1. Direct database access mode, test code.
class ListController extends Controller
{
public $layout = false;
public function actionDb()
{
$specialRecommend = new SpecialRecommend();
$page = empty($_GET['page']) == false ? intval($_GET['page']) : 1;
$pageSize = 10;
$rows = $specialRecommend->find()->offset(($page - 1) * $pageSize)->limit($pageSize)->orderBy("sid desc")->asArray()->all();
$html = $this->render('index', [
'rows' => $rows,
]);
return $html;
}
}
Pressure test code
ab -n 1000 -c 50 "http://testyii.com/index.php?r=list/db&page=1"
pressure test result
Throughput (rps): 159.52[#/sec]
Test 2. Add the cache and test the code
public function actionCache()
{
$page = empty($_GET['page']) == false ? intval($_GET['page']) : 1;
$pageSize = 10;
$key = "cacke_key";
$data = Yii::$app->cache->get($key);
$rows = json_decode($data, true);
if(empty($rows) == true){
$specialRecommend = new SpecialRecommend();
$rows = $specialRecommend->find()->offset(($page - 1) * $pageSize)->limit($pageSize)->orderBy("sid desc")->asArray()->all();
Yii::$app->cache->set($key, json_encode($rows));
}
$html = $this->render('index', [
'rows' => $rows,
]);
return $html;
}
Pressure test code
ab -n 1000 -c 50 "http://testyii.com/index.php?r=list/cache&page=1"
pressure test result
Throughput (rps): 220.66[#/sec]
Test 3. Turn on yii2 pseudo-static. When setting apache redirection, first determine whether the directory exists. If it exists, directly access the real directory
.htaccess
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Rewrite the yii2 route and specify the controller to handle when the file does not exist.
class UrlManagerSolr extends UrlManager
{
public function parseRequest($request)
{
$request_uri = $_SERVER['REQUEST_URI'];
$uris = explode('/', $request_uri);
$path = Yii::$app->basePath . "/web".$request_uri;
$fileExist = false;
if(file_exists($path) == false && strpos($request_uri, "/data/list/file") !== false){
$_SERVER['REQUEST_URI'] = "list/file.html";
$_GET['page'] = intval($uris[3]);
}
return parent::parseRequest($request);
}
}
Processed action: Get data from the database and generate a static file corresponding to the path.
public function actionFile()
{
$specialRecommend = new SpecialRecommend();
$page = empty($_GET['page']) == false ? intval($_GET['page']) : 1;
$pageSize = 10;
$rows = $specialRecommend->find()->offset(($page - 1) * $pageSize)->limit($pageSize)->orderBy("sid desc")->asArray()->all();
$html = $this->render('index', [
'rows' => $rows,
]);
$path = Yii::$app->basePath . "/web/data/list/file/";
if(file_exists($path) == false){
mkdir($path, 0755, true);
}
$file = $path . "{$page}.html";
file_put_contents($file, $html);
return $html;
}
Pressure test code
ab -n 1000 -c 50 "http://testyii.com/data/list/file/1.html"
pressure test result
Throughput (rps): 1327.14 [#/sec]
The test result is nearly 10 times higher than that of test one. The reason for the performance improvement is that apache directly returns the content of the file when it detects the existence of the file data/list/file/1.html, instead of entering the routing and php processing code of yii2. .