TruAPI function reference

The following functions can be used as part of TruAPI.

svc.logger

The logger service is a JavaScript object enabling you to write messages to the log.

Method svc.logger.error

Add an error message to the log.

svc.logger.warn
Add a warning message to the log

svc.logger.info
Add an informational message to the log

Arguments and parameters

The Message ID is an optional parameter for every logger method. Add this ID to the first parameter of your logger method.

The Message ID helps you search for types of logger lines.

Note: User defined message range 0 thru 50000

50000 + (N) is reserved.

Code 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();
  });
};

svc.datapoint

The datapoint service is a JavaScript object enabling you to add a data point to the result report.

Method svc.datapoint.add(name,value)

Add a new data point to the result report.

Arguments and parameters

name. The data point name

value. The data point value

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

svc.thinkTime

The thinkTime service is a JavaScript function enabling you to pause execution in the TruAPI script.

Method scv.ThinkTime(delay,callback)

Arguments and parameters

delay. The length of the pause, in milliseconds.

callback. The JavaScript function invoked when the script resumes.

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

svc.transaction

The transaction service is a JavaScript object enabling you to add a transaction block to your script. The transaction duration and status statistics are added to the report.

Method svc.transaction.start(name)

Specifies the beginning of a transaction.

svc.transaction.end(name,transactionStatus)
Specifies the end of a transaction.

svc.transaction.thinkTime(name,delay,callback)
Pauses the execution of the script and the delay value is added to the results of the transaction.

Arguments and parameters

name. The name of the transaction.

transactionStatus. The values of the transaction status:

  • transaction.PASS Transaction passed
  • transaction.FAIL Transaction failed

  • transaction.STOP Transaction stopped

delay.The length of the pause, in milliseconds.

callback.The JavaScript function invoked when the script resumes.

Code 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();
    });
  });
};

svc.http

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

Method svc.http.get(options, [callback])

For implementation details, see http.get(options,[callback])

svc.http.request(options, [callback])
For implementation details, see http.request(options, [callback])

Code 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();
  });
};

svc.request

The request service is a JavaScript function, a wrapper of the Node.js request module. This wrapped request function adds the HTTP response status and data points to the results report.

Method svc.request(options, callback)

For implementation details, see https://github.com/mikeal/request

Code 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();
    });
  });
};