sa/web-benchmark

Package for benchmarking web pages

dev-master 2017-10-05 17:48 UTC

This package is not auto-updated.

Last update: 2024-05-08 00:50:35 UTC


README

Installation

composer require "sa/web-benchmark":"dev-master"

Configuration

Configuration file: 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',
    ]
];

Usage

Creating instance

use Sa\WebBenchmark\WebBenchmark;
use Sa\WebBenchmark\Exceptions\InvalidArgumentException;

$url = "https://www.google.com";

$competitors = [
    "https://laravel.com",
    "https://symfony.com",
    "https://github.com",
];

try {
    $webBenchmark = new WebBenchmark($url, $competitors);
} catch (InvalidArgumentException $e) {}

Create event manager

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);

Available event managers:

  • NotifyViaEmailEventListener - Send email, if benchmarked website is loaded slower than at least one of the competitors
  • NotifyViaSmsEventListener - Send SMS, if benchmarked website is loaded twice as slow as at least one of the competitors

Run benchmark

try {
    $webBenchmark->run();
} catch (\Exception $e) {
}

Output data

use Sa\WebBenchmark\Outputs\JsonOutput;

$webBenchmark->setOutput(new JsonOutput);

$json = $webBenchmark->output();

Available output:

  • JsonOutput
  • HtmlOutput
  • ConsoleOutput
  • FileOutput
  • PlainTextOutput

Example

Create 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.com", "https://github.com", "https://www.facebook.com"]);

$ php index.php