sa / web-benchmark
用于网页基准测试的包
dev-master
2017-10-05 17:48 UTC
Requires
- phpmailer/phpmailer: ^6.0
This package is not auto-updated.
Last update: 2024-09-25 02:29:13 UTC
README
安装
composer require "sa/web-benchmark":"dev-master"
配置
配置文件: Configs/app.php
<?php return [ 'mail' => [ 'driver' => 'smtp', 'smtp' => [ // SMTP configs 'host' => 'smtp.mailtrap.io', 'port' => 2525, 'username' => '', 'password' => '', ], 'from' => [ // Sender 'email' => 'report@web.benchmark', 'name' => 'Web Benchmark', ], 'to' => [ // Send reports to 'email@example.com' ], ], 'sms' => [ 'api_key' => 'API_KEY_EXAMPLE', 'from' => 'Web Benchmark', ] ];
使用方法
创建实例
use Sa\WebBenchmark\WebBenchmark; use Sa\WebBenchmark\Exceptions\InvalidArgumentException; $url = "https://www.google.com"; $competitors = [ "https://laravel.net.cn", "https://symfony.com.cn", "https://github.com", ]; try { $webBenchmark = new WebBenchmark($url, $competitors); } catch (InvalidArgumentException $e) {}
创建事件管理器
use Sa\WebBenchmark\EventManager; use Sa\WebBenchmark\Listeners\NotifyViaEmailEventListener; use Sa\WebBenchmark\Listeners\NotifyViaSmsEventListener; $eventManager = new EventManager(); $eventManager->attach(NotFastestEvent::class, new NotifyViaEmailEventListener(['email@example.com'])); $eventManager->attach(TwoTimesSlowestEvent::class, new NotifyViaSmsEventListener(['123456789'])); $webBenchmark->setEventManager($eventManager);
可用事件管理器
NotifyViaEmailEventListener
- 如果基准测试的网站加载速度比竞争对手慢,则发送电子邮件NotifyViaSmsEventListener
- 如果基准测试的网站加载速度是竞争对手的两倍慢,则发送短信
运行基准测试
try { $webBenchmark->run(); } catch (\Exception $e) { }
输出数据
use Sa\WebBenchmark\Outputs\JsonOutput; $webBenchmark->setOutput(new JsonOutput); $json = $webBenchmark->output();
可用输出
JsonOutput
HtmlOutput
ConsoleOutput
FileOutput
PlainTextOutput
示例
创建 index.php
<?php require "../vendor/autoload.php"; use Sa\WebBenchmark\Outputs\ConsoleOutput; use Sa\WebBenchmark\WebBenchmark; /** * @param $resource * @param array $competitors */ function main($resource, array $competitors) { $webBenchmark = new WebBenchmark($resource, $competitors, new ConsoleOutput); $webBenchmark->run(); echo $webBenchmark->output(); } main("https://www.google.com", ["https://laravel.net.cn", "https://github.com", "https://#"]);
$ php index.php