PHP code example of bundeling / artilleryphp

1. Go to this page and download the library: Download bundeling/artilleryphp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bundeling / artilleryphp example snippets


use ArtilleryPhp\Artillery;

$artillery = Artillery::new('http://localhost:3000')
    ->addPhase(['duration' => 60, 'arrivalRate' => 5, 'rampTo' => 20], 'Warm up')
    ->addPhase(['duration' => 60, 'arrivalRate' => 20], 'Sustain')
    ->setPlugin('expect')
    ->setEnvironment('live', ['target' => 'https://www.example.com']);

$artillery = Artillery::fromArray([
    'config' => [
        'target' => 'http://localhost:3000',
        'phases' => [
            ['duration' => 60, 'arrivalRate' => 5, 'rampTo' => 20, 'name' => 'Warm up'],
            ['duration' => 60, 'arrivalRate' => 20, 'name' => 'Sustain'],
        ],
        'plugins' => [
            // To produce an empty object as "{  }", use stdClass.
            // This is automatic when using setPlugin(s), setEngine(s) and setJson(s).
            'expect' => new stdClass(),
        ],
        'environments' => [
            'live' => ['target' => 'https://www.example.com']
        ]
    ]
]);

$config = Artillery::fromYaml(__DIR__ . '/default-config.yml');
$environments = Artillery::fromYaml(__DIR__ . '/default-environments.yml');

// New instance from the config, and merging in environments from another file:
$artillery = Artillery::from($config)->merge($environments);

// Create some requests:
$loginRequest = Artillery::request('get', '/login')
    ->addCapture('token', 'json', '$.token')
    ->addExpect('statusCode', 200)
    ->addExpect('contentType', 'json')
    ->addExpect('hasProperty', 'token');
    
$inboxRequest = Artillery::request('get', '/inbox')
    ->setQueryString('token', '{{ token }}')
    ->addExpect('statusCode', 200);

// Create a flow with the requests, and a 500ms delay between:
$flow = Artillery::scenario()
    ->addRequest($loginRequest)
    ->addThink(0.5)
    ->addRequest($inboxRequest);

// Let's loop the flow 10 times:
$scenario = Artillery::scenario()->addLoop($flow, 10);

// Add the scenario to the Artillery instance:
$artillery->addScenario($scenario);

$loginRequest = Artillery::request('post', '/login')
    ->setQueryStrings([
        'username' => '{{ username }}',
        'password' => '{{ password }}'])
    ->addCaptures([
        ['json' => '$.token', 'as' => 'token'],
        ['json' => '$.id', 'as' => 'id']]);


// Without argument will build the YAML as the same name as the php file:
$artillery->build();

// Maybe even run the script right away (assumes `npm install -g artillery`):
$artillery->run();

$artillery = Artillery::new()
    ->addScenario(Artillery::request('get', 'http://www.google.com'));

// Base URL in the Scenario with relateve path in the request:
$artillery = Artillery::new('http://localhost:3000')
    ->addScenario(Artillery::request('get', '/home'));

// Without target, and fully qualified URL in Request:
$artillery = Artillery::new()
    ->addScenario(Artillery::request('get', 'http://localhost:3000/home'));
    
// Setting the target when initializing from another source:
$file = __DIR__ . '/default-config.yml';
$default = Artillery::fromYaml($file)
    ->setTarget('http://www.example.com');

$artillery = Artillery::from($default)
    ->setTarget('http://localhost:3000');

$local = Artillery::new('http://localhost:8080')
    ->addPhase(['duration' => 30, 'arrivalRate' => 1, 'rampTo' => 10])
    ->setHttpTimeout(60);

$production = Artillery::new('https://example.com')
    ->addPhase(['duration' => 300, 'arrivalRate' => 10, 'rampTo' => 100])
    ->setHttpTimeout(30);

$artillery = Artillery::new()
    ->setEnvironment('staging', ['target' => 'https://staging.example.com'])
    ->setEnvironment('production', $production)
    ->setEnvironment('local', $local);

$artillery = Artillery::new($targetUrl)
    ->addPhase(['duration' => 60, 'arrivalRate' => 10]);
    
$request = Artillery::request('get', '/login')
    ->addCapture('token', 'json', '$.token');
    
$scenario = Artillery::scenario('Logging in')->addRequest($request);

// This scenario will run once before any main scenarios/virtual users; here we're using a js function 
// from a processor to generate a variable available in all future scenarios and their virtual users:
$before = Artillery::scenario()->addFunction('generateSharedToken');

// One of the main scenarios, which has access to the shared token,
// and here we're generating a token unique to every main scenario that executed.
$scenario = Artillery::scenario()
    ->addFunction('generateVUToken')
    ->addLog('VU id: {{ $uuid }}')
    ->addLog('    shared token is: {{ sharedToken }}')
    ->addLog('    VU-specific token is: {{ vuToken }}')
    ->addRequest(
        Artillery::request('get', '/')
            ->setHeaders([
                'x-auth-one' => '{{ sharedToken }}',
                'x-auth-two' => '{{ vuToken }}'
            ]));

$artillery = Artillery::new('http://www.artillery.io')
    ->setProcessor('./helpers.js')
    ->setBefore($before)
    ->addScenario($scenario);

// Imagine we have an already defined Scenario as $defaultScenario
$scenario = Artillery::scenario()  
    ->setName('Request, pause 2 seconds, then default flow.')
    ->addRequest(Artillery::request('GET', '/'))  
    ->addThink(2)  
    ->addFlow($defaultScenario);

$getTarget = Artillery::request('get', '/inbox')  
    ->setJson('client_id', '{{ id }}')  
    ->addCapture('first_inbox_id', 'json', '$[0].id');  
$postResponse = Artillery::request('post', '/inbox')  
    ->setJsons(['user_id' => '{{ first_inbox_id }}', 'message' => 'Hello, world!']);

$stringScenario = Artillery::scenario('Sending a string')
    ->setEngine('ws')
    ->addRequest(Artillery::wsRequest('send', 'Artillery'));

$emitAndValidateResponse = Artillery::scenario('Emit and validate response')
    ->setEngine('socketio')
    ->addRequest(
        Artillery::anyRequest('emit')
            ->set('channel', 'echo')
            ->set('data', 'Hello from Artillery')
            ->set('response', ['channel' => 'echoResponse', 'data' => 'Hello from Artillery']));