Skip to content

Instantly share code, notes, and snippets.

@cwndrws
Created September 17, 2014 16:12
Show Gist options
  • Select an option

  • Save cwndrws/7e9f718a3c1bcbd572aa to your computer and use it in GitHub Desktop.

Select an option

Save cwndrws/7e9f718a3c1bcbd572aa to your computer and use it in GitHub Desktop.
var Pool = require('sandcastle').Pool;
var sandboxPath = __dirname + '/sandbox-api.js';
var s = new Pool({numberOfInstances: 20}, {api: sandboxPath, timeout: 30000, useStrictMode: false});
function callCode(code, callback) {
var script = s.createScript(code);
script.on('exit', function(err, output) {
if (output) {
callback(output);
return;
} else if (err) {
callback(null, err.message);
return;
} else {
console.log("something bad happened!");
return;
}
});
script.on('timeout', function() {
callback(null, "function timed out");
return;
});
script.run();
}
function benchmark(title, num, funk) {
var end, i, start;
start = new Date;
for (i = 0; i < num; i++) {
funk();
}
end = new Date;
console.log(title + ': ' + (end - start) + 'ms');
}
var code = "exports.main = function() { exit (\"butts\") };"
var test = function() {
callCode(code, function(results, error) {
console.log('shitballs');
if (error) {
console.log(error);
} else {
console.log(results);
}
});
};
benchmark('butts in sandbox', 1000000, test);
process.exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment