PHP+redis实现的限制抢购防止商品超发功能详解(2)
文件function.php
<?php
//获取商品key名称
function getKeyName($v)
{
return "send_goods_".$v;
}
//日志写入方法
function writeLog($msg,$v)
{
$log = $msg.PHP_EOL;
file_put_contents("log/$v.log",$log,FILE_APPEND);
}
1.ab工具并发测试way1方法
[root@localhost oversend]# ab -c 100 -n 200 http://192.168.0.213:8083/index.php?v=way1
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.0.213 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software: nginx
Server Hostname: 192.168.0.213
Server Port: 8083
Document Path: /index.php?v=way1
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 0.089 seconds
Complete requests: 200
Failed requests: 0
Write errors: 0
Total transferred: 30600 bytes
HTML transferred: 0 bytes
Requests per second: 2243.13 [#/sec] (mean)
Time per request: 44.581 [ms] (mean)
Time per request: 0.446 [ms] (mean, across all concurrent requests)
Transfer rate: 335.16 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 2.2 5 17
Processing: 2 28 16.3 25 55
Waiting: 1 26 15.2 24 50
Total: 5 34 16.3 30 60
Percentage of the requests served within a certain time (ms)
50% 30
66% 35
75% 54
80% 56
90% 57
95% 60
98% 60
99% 60
100% 60 (longest request)
v1方法日志分析
[root@localhost log]# less -N way1.log 1 抢到商品 2 抢到商品 3 抢到商品 4 抢到商品 5 抢到商品 6 抢到商品 7 没有抢到商品 8 没有抢到商品 9 没有抢到商品 10 没有抢到商品 11 没有抢到商品 12 没有抢到商品
观察日志发现 抢到商品的记录有6条超过正常的5条,说明超发了
2.ab工具并发测试way2方法
[root@localhost oversend]# ab -c 100 -n 200 http://192.168.0.213:8083/index.php?v=way2
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.0.213 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software: nginx
Server Hostname: 192.168.0.213
Server Port: 8083
Document Path: /index.php?v=way2
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 0.087 seconds
Complete requests: 200
Failed requests: 0
Write errors: 0
Total transferred: 31059 bytes
HTML transferred: 0 bytes
Requests per second: 2311.68 [#/sec] (mean)
Time per request: 43.259 [ms] (mean)
Time per request: 0.433 [ms] (mean, across all concurrent requests)
Transfer rate: 350.58 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 5.4 5 13
Processing: 3 31 16.6 30 70
Waiting: 1 30 16.6 30 70
Total: 5 37 18.5 32 82
Percentage of the requests served within a certain time (ms)
50% 32
66% 41
75% 45
80% 50
90% 68
95% 80
98% 81
99% 82
100% 82 (longest request)
内容版权声明:除非注明,否则皆为本站原创文章。
