testable-utils
Advanced tools
Comparing version 0.0.19 to 0.0.20
@@ -22,14 +22,14 @@ var _ = require("lodash"); | ||
debug: function() { | ||
sendLog(writeStream, "Debug", Array.prototype.join.call(arguments, '')); | ||
return sendLog(writeStream, "Debug", Array.prototype.join.call(arguments, '')); | ||
}, | ||
info: function() { | ||
sendLog(writeStream, "Info", Array.prototype.join.call(arguments, '')); | ||
return sendLog(writeStream, "Info", Array.prototype.join.call(arguments, '')); | ||
}, | ||
error: function(msg) { | ||
var args = _.map(arguments, function(arg) { toErrorContent(arg); }); | ||
sendLog(writeStream, "Error", msg.stack || Array.prototype.join.call(args, '')); | ||
return sendLog(writeStream, "Error", msg.stack || Array.prototype.join.call(args, '')); | ||
}, | ||
fatal: function(msg) { | ||
var args = _.map(arguments, function(arg) { toErrorContent(arg); }); | ||
sendLog(writeStream, "Fatal", msg.stack || Array.prototype.join.call(args, '')); | ||
return sendLog(writeStream, "Fatal", msg.stack || Array.prototype.join.call(args, '')); | ||
} | ||
@@ -40,16 +40,19 @@ }; | ||
function sendLog(writeStream, level, msg) { | ||
if (writeStream) { | ||
writeStream.write(JSON.stringify({ | ||
type: "Log", | ||
data: { | ||
level: level, | ||
message: toErrorContent(msg), | ||
timestamp: Date.now() | ||
} | ||
}) + '\n'); | ||
} else { | ||
console.log('[' + level + '] ' + toErrorContent(msg)); | ||
} | ||
return new Promise(function(resolve, reject) { | ||
if (writeStream) { | ||
writeStream.write(JSON.stringify({ | ||
type: "Log", | ||
data: { | ||
level: level, | ||
message: toErrorContent(msg), | ||
timestamp: Date.now() | ||
} | ||
}) + '\n', 'utf8', resolve); | ||
} else { | ||
console.log('[' + level + '] ' + toErrorContent(msg)); | ||
resolve(); | ||
} | ||
}); | ||
} | ||
module.exports = createLog; |
@@ -7,2 +7,3 @@ | ||
const csvLocal = require('./csv-local.js'); | ||
const wdio = require('./wdio-commands'); | ||
@@ -65,11 +66,14 @@ var LocalInfo = { | ||
var timing = function(code, metricName, resource) { | ||
var stopwatch = function(code, metricName, resource) { | ||
const start = Date.now(); | ||
const _this = this; | ||
var done = function() { | ||
var result = results(resource); | ||
if (result !== null) | ||
result.timing(metricName, Date.now() - start, 'ms'); | ||
}; | ||
code(done); | ||
return new Promise(function(resolve, reject) { | ||
var done = function() { | ||
var result = results(resource); | ||
if (result !== null) | ||
result.timing(metricName, Date.now() - start, 'ms'); | ||
resolve(result); | ||
}; | ||
code(done); | ||
}); | ||
}; | ||
@@ -82,4 +86,8 @@ | ||
execute: function(codeToExecute) { | ||
if (codeToExecute) | ||
codeToExecute(); | ||
return new Promise(function(resolve, reject) { | ||
if (codeToExecute) | ||
codeToExecute(resolve); | ||
else | ||
resolve(); | ||
}); | ||
} | ||
@@ -89,9 +97,17 @@ }; | ||
var csv = isLocal ? csvLocal.initialize() : csvRemote.initialize(info, log); | ||
var log = createLog(writeStream); | ||
var results = createResults(writeStream); | ||
wdio.registerLogCommands(log); | ||
wdio.registerCsvCommands(csv); | ||
wdio.registerResultsCommands(results); | ||
wdio.registerInfoCommands(info); | ||
wdio.registerStopwatchCommands(stopwatch); | ||
module.exports.isLocal = isLocal; | ||
module.exports.info = info; | ||
module.exports.log = createLog(writeStream); | ||
module.exports.results = createResults(writeStream); | ||
module.exports.timing = timing; | ||
module.exports.log = log; | ||
module.exports.results = results; | ||
module.exports.stopwatch = stopwatch; | ||
module.exports.testable = testable; | ||
module.exports.dataTable = csv; |
@@ -6,3 +6,3 @@ | ||
timing: function(name, val, units) { | ||
sendResult(writeStream, { | ||
return sendResult(writeStream, { | ||
type: 'Timing', | ||
@@ -13,3 +13,3 @@ data: { resource: resource, url: url, name: name, val: val, units: units } | ||
counter: function(name, val, units) { | ||
sendResult(writeStream, { | ||
return sendResult(writeStream, { | ||
type: 'Counter', | ||
@@ -20,3 +20,3 @@ data: { resource: resource, url: url, name: name, val: val, units: units } | ||
histogram: function(name, key, val) { | ||
sendResult(writeStream, { | ||
return sendResult(writeStream, { | ||
type: 'Histogram', | ||
@@ -31,9 +31,12 @@ data: { resource: resource, url: url, name: name, key: key, val: val } | ||
function sendResult(writeStream, result) { | ||
if (writeStream) { | ||
writeStream.write(JSON.stringify(result) + '\n'); | ||
} else { | ||
console.log('[Result] ' + JSON.stringify(result, null, '\t')); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
if (writeStream) { | ||
writeStream.write(JSON.stringify(result) + '\n', 'utf8', resolve); | ||
} else { | ||
console.log('[Result] ' + JSON.stringify(result, null, '\t')); | ||
resolve(); | ||
} | ||
}); | ||
} | ||
module.exports = createResults; |
{ | ||
"name": "testable-utils", | ||
"version": "0.0.19", | ||
"version": "0.0.20", | ||
"description": "Utilities for Testable scripts", | ||
@@ -5,0 +5,0 @@ "author": "Avi Stramer", |
124
README.md
@@ -44,3 +44,3 @@ # Testable Script Utilities | ||
`results([resource], [url]).timing(name, timeMs, [units])` | ||
`results([resource], [url]).timing(name, timing, [units])` | ||
@@ -78,9 +78,11 @@ Capture a timing. Units defaults to `ms`. Resource and url default to blank and are included with the "overall results". Testable will calculate various aggergations like min, max, average, standard deviation, and the percentiles defined in your test configuration. | ||
```javascript | ||
var timing = require('testable-utils').timing; | ||
var stopwatch = require('testable-utils').stopwatch; | ||
timing(function(done) { | ||
stopwatch(function(done) { | ||
// some code we want to time here | ||
// call done() once it is done, can be async | ||
done(); | ||
}, 'myCustomTimer'); | ||
}, 'myCustomTimer').then(function() { | ||
// called after done() is invoked | ||
}); | ||
``` | ||
@@ -97,2 +99,3 @@ | ||
log.debug("Some useful debug"); | ||
log.info("Some useful info"); | ||
@@ -184,15 +187,104 @@ log.error("Whoops something went wrong"); | ||
#### Webdriver.io Compatibility | ||
## Webdriver.io Commands | ||
Webdriver.io expects synchronous code. To handle async code like this CSV API, the <a target="blank" href="http://webdriver.io/api/utility/call.html">call()</a> function is provided. | ||
All of the API calls above are registered as <a target="_blank" href="http://webdriver.io/guide/usage/customcommands.html">custom commands</a> with Webdriver.io. | ||
```javascript | ||
var symbol = browser.call(function() { | ||
return dataTable | ||
.open('data.csv') | ||
.next() | ||
.then(function(results) { | ||
return results[0].data['Symbol']; | ||
}); | ||
}); | ||
``` | ||
Simply include `var testableUtils = require('testable-utils');` in you test spec or in one of the <a target="_blank" href="http://webdriver.io/guide/testrunner/configurationfile.html">configuration file hooks</a> like `onPrepare()` and all the below custom commands will automatically get registered. | ||
Note that all the Webdriver.io commands can be used in a synchronous fashion. | ||
<table> | ||
<tr> | ||
<th>Webdriver.io</th> | ||
<th>testable-utils</th> | ||
</tr> | ||
<tr> | ||
<td><pre>browser.testableInfo()</pre></td> | ||
<td><a href="#execution-info"><pre>info</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>var result = | ||
browser.testableCsvGet(csvFile, index);</pre></td> | ||
<td><a href="#get-row-by-index"><pre>dataTable | ||
.open(csvFile) | ||
.get(index) | ||
.then(function(result) { ... }</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>var result = | ||
browser.testableCsvRandom(csvFile);</pre></td> | ||
<td><a href="#get-random-row"><pre>dataTable | ||
.open(csvFile) | ||
.random() | ||
.then(function(result) { ... }</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>var results = | ||
browser.testableCsvNext(csvFile[, options]);</pre></td> | ||
<td><a href="#get-random-row"><pre>dataTable | ||
.open(csvFile) | ||
.random([options]) | ||
.then(function(results) { ... }</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>var result = | ||
browser.testableResult([resource], [url]); | ||
browser.testableCounter( | ||
result, | ||
name, | ||
[increment], | ||
[units]);</pre></td> | ||
<td><a href="#counter"><pre>results([resource], [url]) | ||
.counter(name, [increment], [units]);</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>var result = | ||
browser.testableResult([resource], [url]); | ||
browser.testableTiming( | ||
result, | ||
name, | ||
timing, | ||
[units]);</pre></td> | ||
<td><a href="#timing"><pre>results([resource], [url]) | ||
.timing(name, timing, [units]);</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>var result = | ||
browser.testableResult([resource], [url]); | ||
browser.testableHistogram( | ||
result, | ||
name, | ||
bucket, | ||
[increment]);</pre></td> | ||
<td><a href="#histogram"><pre>results([resource], [url]) | ||
.histogram(name, bucket, [increment]);</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>browser.testableLogDebug(msg);</pre></td> | ||
<td><a href="#logging"><pre>log.debug(msg);</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>browser.testableLogInfo(msg);</pre></td> | ||
<td><a href="#logging"><pre>log.info(msg);</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>browser.testableLogError(msg);</pre></td> | ||
<td><a href="#logging"><pre>log.error(msg);</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>browser.testableLogFatal(msg);</pre></td> | ||
<td><a href="#logging"><pre>log.fatal(msg);</pre></a></td> | ||
</tr> | ||
<tr> | ||
<td><pre>// blocks until done() is called | ||
browser.testableStopwatch(function(done) { | ||
// some code to time | ||
done(); | ||
}, 'myCustomMetricMs');</pre></td> | ||
<td><a href="#stopwatch"><pre>// returns Promise immediately | ||
stopwatch(function(done) { | ||
// some code to time | ||
done(); | ||
}, 'myCustomMetricMs');</pre></a></td> | ||
</tr> | ||
</table> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22803
9
400
287