Skip to content

Commit

Permalink
report will now only accept a stream as input
Browse files Browse the repository at this point in the history
  • Loading branch information
catdad committed Apr 8, 2016
1 parent d7f4f3e commit 5094e14
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 91 deletions.
3 changes: 2 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var util = require('util');
var _ = require('lodash');
var glob = require('glob');
var rc = require('rc');
var globfile = require('glob-filestream');

var argv = require('./argv.js');
var bench = require('../index');
Expand Down Expand Up @@ -173,8 +174,8 @@ function help() {
function report() {
var glob = argv.glob || argv._[1];
var opts = getOptions();
opts.glob = glob;

opts.input = globfile(glob);
opts.output = getDestinationStream(opts);

grandma.report(opts, onDone);
Expand Down
148 changes: 58 additions & 90 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var util = require('util');
var _ = require('lodash');
var async = require('async');
var glob = require('glob');
var isGzip = require('is-gzip-stream');
var byline = require('byline');
var table = require('text-table');
var Duration = require('duration-js');
Expand Down Expand Up @@ -40,61 +39,19 @@ function Reporter() {
});
}

function readUtil(file, onLine, done) {
var filepath = path.resolve('.', file);

// determine if the file is zipped or not
isGzip(fs.createReadStream(filepath), function(err, isGzipped, stream) {
if (err) {
throw err;
}

if (isGzipped) {
readStream(stream.pipe(zlib.createUnzip()), onLine, done);
} else {
readStream(stream, onLine, done);
}
});
}

function readHelper(files, onFile, callback) {
async.map(files, onFile, callback);
}

function readSummary(file, done) {
var reports = [];
var header = {};

readUtil(file, function addLineSummary(line) {
var data = JSON.parse(line.toString());
function readJsonLines(stream, onLine, done) {
readStream(stream, function eachLine(line) {
var jsonLine;

if (data.type === 'header') {
header = data;
} else {
reports.push(_.reduce(data.report, function(seed, rep, name) {
seed[name] = rep.duration;
return seed;
}, {}));
}
}, function(err) {
if (err) {
return done(err);
try {
jsonLine = JSON.parse(line);
} catch(e) {
// skip this line
return;
}

done(undefined, {
header: header,
reports: reports
});
});
}

function lineReader(lineStream) {
return function readLines(file, done) {
readUtil(file, function(line) {
var data = JSON.parse(line);
lineStream.write(data);
}, done);
};

onLine(jsonLine);
}, done);
}

function jsonStats(data) {
Expand Down Expand Up @@ -142,17 +99,30 @@ function Reporter() {
return statsJson;
}

function readJsonStats(files, done) {
readHelper(files, readSummary, function(err, data) {
function readJsonStats(input, done) {

var reports = [];
var header = {};

function onJson(data) {
if (data.type === 'header') {
header = data;
} else {
reports.push(_.reduce(data.report, function(seed, rep, name) {
seed[name] = rep.duration;
return seed;
}, {}));
}
}

readJsonLines(input, onJson, function(err) {
if (err) {
return done(err);
}

var combinedData = {
header: data[0].header,
reports: _.reduce(data, function(seed, eachData) {
return seed.concat(eachData.reports);
}, [])
header: header,
reports: reports
};

var jsonSummary = jsonStats(combinedData);
Expand Down Expand Up @@ -210,10 +180,10 @@ function Reporter() {
return str + '\n';
}

this.text = function textReporter(files, output, done) {
this.text = function textReporter(input, output, done) {
var that = this;

readJsonStats(files, function(err, jsonSummary) {
readJsonStats(input, function(err, jsonSummary) {
if (err) {
return done(err);
}
Expand All @@ -228,10 +198,10 @@ function Reporter() {
});
};

this.json = function jsonReporter(files, output, done) {
this.json = function jsonReporter(input, output, done) {
var that = this;

readJsonStats(files, function(err, jsonSummary) {
readJsonStats(input, function(err, jsonSummary) {
if (err) {
return done(err);
}
Expand All @@ -246,7 +216,7 @@ function Reporter() {
});
};

this.plot = function plotReporter(files, output, done) {
this.plot = function plotReporter(input, output, done) {
var that = this;

fs.readFile(path.resolve(__dirname, '..', 'views', 'plot.html'), 'utf8', function (err, html) {
Expand All @@ -258,8 +228,6 @@ function Reporter() {

output.write(htmlParts[0]);

var lines = through.obj();

var template;
var keys;

Expand All @@ -281,7 +249,7 @@ function Reporter() {
output.write(util.format.apply(util, args));
}

lines.on('data', function(data) {
function onJson(data) {
if (data.type === 'report') {

if (!template) {
Expand Down Expand Up @@ -311,9 +279,10 @@ function Reporter() {
// For now, we will just write the full test report in the plot
output.write(util.format.apply(util, args));
}
});
}

readHelper(files, lineReader(lines), function(err) {
readJsonLines(input, onJson, function(err) {

output.on('drain', function() {
done();
});
Expand All @@ -329,8 +298,8 @@ function Reporter() {
}

function validateOpts(opts) {
if (!_.isString(opts.glob)) {
return new Error('no results files defined');
if (!opts.input || !opts.input.pipe || !opts.input.readable) {
return new Error('no results input stream defined');
}

return opts;
Expand All @@ -343,26 +312,25 @@ function report(opts, callback) {
return async.setImmediate(callback.bind(undefined, opts));
}

var input = opts.input;
var output = opts.output;

glob(opts.glob, function(err, files) {
if (err) {
return callback(err);
}

var reporter = new Reporter();

switch(opts.type) {
case 'text':
reporter.text(files, output, callback);
break;
case 'json':
reporter.json(files, output, callback);
break;
case 'plot':
reporter.plot(files, output, callback);
}

input.on('error', function(err) {
callback(err);
});

var reporter = new Reporter();

switch(opts.type) {
case 'text':
reporter.text(input, output, callback);
break;
case 'json':
reporter.json(input, output, callback);
break;
case 'plot':
reporter.plot(input, output, callback);
}
}

module.exports = report;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"duration-js": "^3.5.1",
"fractional": "^1.0.0",
"glob": "^7.0.3",
"glob-filestream": "^0.2.0",
"is-gzip-stream": "^0.1.0",
"lodash": "^4.6.1",
"rc": "^1.1.6",
Expand Down

0 comments on commit 5094e14

Please sign in to comment.