truapi

A pure JavasSript API load testing tool

Usage no npm install needed!

<script type="module">
  import truapi from 'https://cdn.skypack.dev/truapi';
</script>

README

TruAPI

TruAPI enables you to create load testing scripts in JavaScript. The scripts can be uploaded to Performance Testing as a Service.

How to install TruAPI

This task describes how to install TruAPI package.

  1. Install Node.js.
  2. Download and copy the TruAPI package to a directory.
  3. Install the TruAPI package.
cd <truapi directory>
npm install -g <truapi directory>\truapi-1.0.0.tgz.

How to create a script with TruAPI

This task describes how to create a script with TruAPI.

  1. Create a standard npm package.
Example
  mkdir MyTruAPITest
  cd MyTruAPITest
  npm init 

The set up questions are optional. 2. Implement the script.
Write codes as usual to implement your test.
You can also use 'truapi-gen' to generate skeleton codes.

Example:
  cd MyTruAPITest
  truapi-gen -template default

Refer to samples included in TruAPI package for more details. 3. After you have written the TruAPI script, test it locally to verify that it runs without error.

Example:
  cd MyTruAPITest
  truapi-cli --testdir ./
  1. Generate the TruAPI script package.
Example:
  cd MyTruAPITest
  npm pack

A *.tgz is created. This file can be uploaded to PTaaS and added to a test definition.

TruAPI Function Reference

logger

The logger service is a JavaScript Object. In TruAPI script, customer can write the log messages by this service.

logger.error

Adds an error message. The arguments are the same as the Node.js built-in function "util.format" ( http://Nodejs.org/api/util.html#util_util_format_format )

logger.warn

Adds a warning message. The arguments are the same as the Node.js built-in function "util.format" ( http://Nodejs.org/api/util.html#util_util_format_format )

logger.info

Adds a info message. The arguments are the same as the Node.js built-in function "util.format" ( http://Nodejs.org/api/util.html#util_util_format_format )

Example:

exports = module.exports = function (vuser) {
  /* init action */
  vuser.init('Base Vuser init', function (svc, done) {
    /* adds an info log */
    svc.logger.error('Vuser %s error', vuser.getVUserId());
    svc.logger.warn('Vuser %s warn', vuser.getVUserId());
    svc.logger.info('Vuser %s info', vuser.getVUserId());
    done();
  });
};

datapoint

The datapoint service is a JavaScript Object. In TruAPI script, customer can add a data point to the result report by this service.

datapoint.add(name, value)

Adds a new data point to the result report. Arguments:

  1. name - The data point name
  2. value - The data point value

Example:

exports = module.exports = function (vuser) {
  /* main action */
  vuser.action('Base Vuser action', function (svc, done) {
    /* adds a new datapoint */
    svc.datapoint.add('my-data', 100);
  });
};

thinkTime

The thinkTime service is a JavaScript Function. It pauses execution in the TruAPI script.

thinkTime(delay, callback)

To schedule execution of a one-time callback after delay milliseconds. Arguments:

  1. delay - The length of the pause, in milliseconds.
  2. callback - It's a JavaScript function. It will be invoked when the test script is resumed.

Example:

exports = module.exports = function (vuser) {
  /* main action */
  vuser.action('Base Vuser action', function (svc, done) {
    function test() {
      svc.logger.info('test function');
      done();
    }

    /* Invoke the test() function after 500 milliseconds */
    svc.thinkTime(500, test);
  });
};

transaction

The transaction service is a JavaScript Object. You can add transaction block to your test script. The transaction duration and status will be added to the report.

transaction.start(name)

Marks the beginning of a transaction. Arguments:

  1. name - The name of the transaction.

transaction.end(name, transactionStatus)

Marks the end of a transaction. Arguments:

  1. name - The name of the transaction.
  2. transactionStatus - The Transaction Status
    The values of transaction status:
    • PASS - Transaction passed
    • FAIL - Transaction failed
    • STOP - Transaction stopped

transaction.thinkTime(name, delay, callback)

It pauses execution in the TruAPI script and the "delay" value is added to the results of the transaction.

Arguments:

  1. name - The name of the transaction.
  2. delay - The length of the pause, in milliseconds.
  3. callback - It's a JavaScript function. It will be invoked when the test script is resumed.

Example:

exports = module.exports = function (vuser) {
  /* main action */
  vuser.action('Vuser main action', function (svc, done) {
    svc.logger.info('Vuser %s running', vuserId);

    /* send request to server */
    function sendRequest() {
      svc.request(requestOptions, function (err, res, body) {
        if (err) {
          svc.logger.error('request error %s', err.toString());
          svc.transaction.end('requestTest', svc.transaction.FAIL);
          done();
          return;
        }
        /* close the transaction */
        svc.transaction.end('requestTest', svc.transaction.PASS);
        done();
      });
    }
    
    svc.transaction.start('requestTest');
    svc.transaction.thinkTime('requestTest', 1000 * 5, function () {
      sendRequest();
    });
  });
};

http

The http service is a JavaScript Object. It's a wrapper of Node.js built-in http client object. This wrapped http client object adds the HTTP response status and data points to the results report.

http.get(options, [callback])

It's the same as the Node.js built-in function "http.get" ( http://Nodejs.org/api/http.html#http_http_get_options_callback )

http.request(options, [callback])

It's the same as the Node.js built-in function "http.request" ( http://Nodejs.org/api/http.html#http_http_request_options_callback )

Example:

exports = module.exports = function (vuser) {
  /* main action */
  vuser.action('simple HTTP request tests', function (svc, done) {
    var requestOptions;
    requestOptions = { hostname: 'Node.js.org', port: 80, path: '/' };
    svc.logger.info('Start http test %s', vuser.getVUserId());
    svc.http.request(requestOptions, function (res) {
      svc.logger.info('http response statusCode = %d', res.statusCode);
      res.on('end', function () {
        svc.logger.info('http test passed');
        done();
      }).on('error', function () {
        svc.logger.error('http client I/O error');
        done();
      });
    }).on('error', function (err) {
      svc.logger.error('http error %s', err.toString());
      done();
    }).end();
  });
};

request

The request service is a JavaScript function. It's a wrapper of Node.js request module ( https://github.com/mikeal/request ). This wrapped request function adds the HTTP response status and data points to the report.

request(options, callback)

It's the same as the "request" function in the Node.js request module ( https://github.com/mikeal/request ).

Example

exports = module.exports = function (vuser) {
  /* main action */
  vuser.action('simple HTTP request tests', function (svc, done) {
    var requestOptions;
    requestOptions = { url: 'http://Node.js.org' };
    svc.logger.info('Start http test %s', vuser.getVUserId());
    svc.request(requestOptions, function (err, res, body) {
      if (err) {
        svc.logger.error('request error %s', err.toString());
        done();
        return;
      }
      svc.logger.info('request test passed');
      done();
    });
  });
};