1、使用fopen fwrite fclose
<?php
//使用fopen fwrite fclose【write_log1.php】
function write($con){
$path = "/data/logs";
$filedir = $path . "/log.log";
//清除缓存
clearstatcache();
// 文件不存在
if (!file_exists($filedir)) {
$size = 0;
}else{
$size = filesize($filedir);
}
if ($size > 1024000){
$num = count(scandir($path)) - 2;
$newname = "log.log.$num";
rename($filedir,$path."/".$newname);
}
$fp = @fopen($filedir, 'ab');
flock($fp, LOCK_EX|LOCK_NB);
fwrite($fp, $con);
flock($fp, LOCK_UN);
fclose($fp);
return;
}
$i = 0;
while ($i<=100){
$i ++;
$t = time();
$con = "start".$i."##".$t."进一步的,如果 url_args 中有过多字段,可能导致 Elasticsearch 集群因为频繁 update mapping 或者消耗太多内存在 cluster state 上而宕机。所以,更优的选择,是只保留明确有用的 url_args 内容,其他部分舍去。END\n";
write($con);
echo "##".$i;
}
2、使用file_put_contents
<?php
//使用file_put_contents【write_log.php】
function write($con){
$path = "/data/logs";
$filedir = $path . "/log.log";
//清除缓存
clearstatcache();
// 文件不存在
if (!file_exists($filedir)) {
$size = 0;
}else{
$size = filesize($filedir);
}
if ($size > 1024000){
$num = count(scandir($path)) - 2;
$newname = "log.log.$num";
rename($filedir,$path."/".$newname);
}
file_put_contents($filedir,$con,FILE_APPEND | LOCK_EX);
return;
}
$i = 0;
while ($i<=100){
$i ++;
$t = time();
$con = "start".$i."##".$t."进一步的,如果 url_args 中有过多字段,可能导致 Elasticsearch 集群因为频繁 update mapping 或者消耗太多内存在 cluster state 上而宕机。所以,更优的选择,是只保留明确有用的 url_args 内容,其他部分舍去。END\n";
write($con);
echo "##".$i;
}
3、使用SeasLog
<?php
//使用SeasLog【a.php】
function write($con){
$path = "/data/logs";
SeasLog::setBasePath($path);
$filedir = $path . "/log.log";
// 文件不存在则创建
clearstatcache();
if (!file_exists($filedir)) {
$size = 0;
}else{
$size = filesize($filedir);
}
if ($size > 1024000){
$num = count(scandir($path)) - 2;
$newname = "log.log.$num";
rename($filedir,$path."/".$newname);
}
SeasLog::notice($con);
return;
}
$i = 0;
while ($i<=100){
$i ++;
$t = time();
$con = "start".$i."##".$t."进一步的,如果 url_args 中有过多字段,可能导致 Elasticsearch 集群因为频繁 update mapping 或者消耗太多内存在 cluster state 上而宕机。所以,更优的选择,是只保留明确有用的 url_args 内容,其他部分舍去。END";
write($con);
echo "##".$i;
}
4、将文件放入/var/www/html/下执行一下语句
#使用apache自带的ab进行测试
/bin/ab -n 1000 -c 100 localhost/write_log1.php
/bin/ab -n 1000 -c 100 localhost/write_log.php
/bin/ab -n 1000 -c 100 localhost/a.php
结果如下:
--------------------- 以下是fwrite --------------------------
Document Path: /write_log1.php
Document Length: 397 bytes
Concurrency Level: 100
Time taken for tests: 5.361 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 621000 bytes
HTML transferred: 397000 bytes
Requests per second: 186.54 [#/sec] (mean)
Time per request: 536.065 [ms] (mean)
Time per request: 5.361 [ms] (mean, across all concurrent requests)
Transfer rate: 113.13 [Kbytes/sec] received
--------------------- 以下是file_put_contents --------------------------
Document Path: /write_log.php
Document Length: 397 bytes
Concurrency Level: 100
Time taken for tests: 4.150 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 621000 bytes
HTML transferred: 397000 bytes
Requests per second: 240.97 [#/sec] (mean)
Time per request: 414.987 [ms] (mean)
Time per request: 4.150 [ms] (mean, across all concurrent requests)
Transfer rate: 146.14 [Kbytes/sec] received
--------------------- 以下是 SeasLog --------------------------
Document Path: /a.php
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 0.926 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 241000 bytes
HTML transferred: 0 bytes
Requests per second: 1079.74 [#/sec] (mean)
Time per request: 92.615 [ms] (mean)
Time per request: 0.926 [ms] (mean, across all concurrent requests)
Transfer rate: 254.12 [Kbytes/sec] received