Stress Test - Instructions

+1 vote
Hello! I was wondering if, perhaps, you would have a set of scripts or tools to help stress test your network after you adjusted certain parameters?

If that doesn't turn out to be possible, could you give a direction on what would you do (and use)/how would you architect such a test?

Thanks!
asked Feb 12, 2019 by tloriato

1 Answer

+1 vote
 
Best answer
Here's one of the (command line) PHP scripts we use for stress testing and benchmarking publishing items to streams. You'll need to adjust some constants at the top for your chain:

<?php

    define('CONST_MULTICHAIND_HOST', '127.0.0.1');
    define('CONST_MULTICHAIND_PORT', '[enter rpc port]');
    define('CONST_MULTICHAIND_USER', 'multichainrpc');
    define('CONST_MULTICHAIND_PASS', '[enter password]');
    define('CONST_PUBLISH_FROM', '[enter address]');
    define('CONST_PUBLISH_STREAM', '[enter name of stream you created]');
    define('CONST_PUBLISH_TXS', 15625*4);
    define('CONST_PUBLISH_PER_TX', 32);
    define('CONST_PUBLISH_KEYS', 1000);
    define('CONST_PUBLISH_SIZE_MIN', 1);
    define('CONST_PUBLISH_SIZE_MAX', 32);
    define('CONST_PUBLISH_OFFCHAIN', false);
    
    $start=get_microtime();
        
    for ($tx=1; $tx<=CONST_PUBLISH_TXS; $tx++) {
        $items=array();

        for ($item=0; $item<CONST_PUBLISH_PER_TX; $item++) {
            $hexlength=2*mt_rand(CONST_PUBLISH_SIZE_MIN, CONST_PUBLISH_SIZE_MAX);

            $data='';
            for ($build=ceil($hexlength/4); $build--; $build>=0)
                $data.=str_pad(dechex(mt_rand(0, 65535)), 4, '0', STR_PAD_LEFT);

            $items[]=array(
                'for' => CONST_PUBLISH_STREAM,
                'key' => str_pad(mt_rand(0, CONST_PUBLISH_KEYS-1), 9, '0', STR_PAD_LEFT),
                'data' => substr($data, 0, $hexlength),
                'options' => CONST_PUBLISH_OFFCHAIN ? 'offchain' : ''
            );
        }

        $response=curlless_json_rpc_send(
            CONST_MULTICHAIND_HOST,
            CONST_MULTICHAIND_PORT,
            CONST_MULTICHAIND_USER,
            CONST_MULTICHAIND_PASS,
            'createrawsendfrom',
            array(
                CONST_PUBLISH_FROM,
                (object)array(),
                $items,
                'send',
            )
        );
        
        $txid=@$response['result'];
        $valid=(strlen($txid)==64);
        
        if ($valid) {
            $elapsed=get_microtime()-$start;
            printf("%9d tx / %9d items / %9.3f sec: %s (%6.1f items/sec)\n", $tx, $tx*CONST_PUBLISH_PER_TX,
                $elapsed, $txid, $tx*CONST_PUBLISH_PER_TX/$elapsed);
        } else {
            print_r($response);
            usleep(100000);
        }
    }
    
    function curlless_json_rpc_send($host, $port, $user, $password, $method, $params=array())
    {
        $url='http://'.$host.':'.$port.'/';
               
        $strUserPass64= base64_encode($user.':'.$password);
        $payload=json_encode(array(
            'id' => time(),
            'method' => $method,
            'params' => $params,
        ));

        $header=  'Content-Type: application/json'."\r\n".
                  'Content-Length: '.strlen($payload)."\r\n".
                  'Connection: close'."\r\n".
                  'Authorization: Basic '.$strUserPass64."\r\n";
               
        $options = array(
            'http' => array(
                'header'  => $header,
                'method'  => 'POST',
                'content' => $payload
            )
        );       
       
        $context  = stream_context_create($options);
        $response = file_get_contents($url, false, $context);
       
        $result=json_decode($response, true);
       
        if (!is_array($result)) {
            $result=array('error' => array(
                'code' => 'HTTP 500',
                'message' => strip_tags($response).' '.$url
            ));
        }

        return $result;       
    }

    function get_microtime()
    {
        return microtime(true);
    }
answered Feb 12, 2019 by MultiChain
selected Feb 12, 2019 by tloriato
thanks! is this script used with a tool or as a stand-alone application with php?
This is a standalone PHP script.
...