run.js 8.18 KiB
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const _ = require('lodash');
const core = require('artillery-core');
const runner = core.runner;
const fs = require('fs');
const path = require('path');
const async = require('async');
const csv = require('csv-parse');
const util = require('util');
const cli = require('cli');
const YAML = require('yamljs');
const defaultOptions = require('rc')('artillery');
const moment = require('moment');
module.exports = run;
function run(scriptPath, options) {
  let logfile;
  if (options.output) {
    logfile = options.output;
    if (!logfile.match(/\.json$/)) {
      logfile += '.json';
  } else {
    if (defaultOptions.output) {
      logfile = moment().format(defaultOptions.output);
    } else {
      logfile = moment().format('[artillery_report_]YMMDD_HHmmSS[.json]');
  function log() {
    if (options.quiet) { return; }
    console.log.apply(console, arguments);
  async.waterfall([
    function readScript(callback) {
      fs.readFile(scriptPath, 'utf-8', function(err, data) {
        if (err) {
          const msg = util.format('File not found: %s', scriptPath);
          return callback(new Error(msg), null);
        let script;
        try {
          if (/\.ya?ml$/.test(path.extname(scriptPath))) {
            script = YAML.parse(data);
          } else {
            script = JSON.parse(data);
        } catch (e) {
          const msg2 = util.format(
            'File %s does not appear to be valid JSON', scriptPath);
          return callback(new Error(msg2), null);
        if (options.target && script.config) {
          script.config.target = options.target;
        if (!script.config.target && !options.environment) {