Skip to content

Instantly share code, notes, and snippets.

@donato
Created May 4, 2016 15:38
Show Gist options
  • Save donato/70ee94709d828721a8a562db16156ee2 to your computer and use it in GitHub Desktop.
Save donato/70ee94709d828721a8a562db16156ee2 to your computer and use it in GitHub Desktop.
Example performance test for CSP Channels
/*
This is a simplified excerpt of a performance test used for channel throughput.
It reads from a file into an inputChannel, and pushes it's data through two other channels.
You can tune the test by how many bytes are sent at a time, and how long to wait between sending more data.
usage
$node channel-push-test.js my_video.mp4
*/
var parseArgs = require('minimist');
var fs = require('fs');
var csp = require('js-csp');
var CHUNK_SIZE = 10;
var WAIT_TIME = 0;
var buff = new Buffer(CHUNK_SIZE);
var args = parseArgs(process.argv.slice(2));
var file = args._[0];
var fileReadInterval = -1;
var transmuxer = {
inputChannel: new csp.chan(),
audio: new csp.chan(),
video: new csp.chan()
};
fs.open(file, 'r', function(err, fd) {
if (err) {
console.log('Error opening file: ', file);
console.log(err.message);
return;
}
fileReadInterval = setInterval(pushSomeBytes.bind(null, fd), WAIT_TIME);
});
function pushSomeBytes(fd) {
fs.read(fd, buff, 0, buff.length, null, function(err, bytesRead, buffer) {
if (err) {
console.log(err.message);
return;
}
// If it didn't read a full amount of bytes, remove noise
var data = buffer.slice(0, bytesRead);
csp.putAsync(transmuxer.inputChannel, data);
if (bytesRead < buff.length) {
transmuxer.inputChannel.close();
fs.close(fd);
clearInterval(fileReadInterval);
}
});
}
csp.go(function*() {
var length = 0;
var unit;
while (csp.CLOSED !== (unit = yield csp.take(transmuxer.video))) {
length += unit.length;
}
console.log('h264 data of length ' , length);
});
csp.go(function*() {
var length = 0;
var unit;
while (csp.CLOSED !== (unit = yield csp.take(transmuxer.audio))) {
length += unit.length;
}
console.log('audio data of length ' , length);
});
csp.go(function*() {
var unit;
while (csp.CLOSED !== (unit = yield csp.take(transmuxer.inputChannel))) {
if (Math.random() > 0.5) {
yield csp.put(transmuxer.video, unit);
} else {
yield csp.put(transmuxer.audio, unit);
}
}
transmuxer.video.close();
transmuxer.audio.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment