mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +01:00
for loop bench in testbed
This commit is contained in:
63
src/routes/testbed.js
Normal file
63
src/routes/testbed.js
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
|
||||
(function(TestBed) {
|
||||
TestBed.create_routes = function(app) {
|
||||
|
||||
app.get('/bench/forloop', function(req, res) {
|
||||
var benchData = {};
|
||||
|
||||
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
|
||||
|
||||
function f(x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
var runCount = req.query.runs ? req.query.runs : 1000000;
|
||||
|
||||
function withCaching() {
|
||||
var time = process.hrtime();
|
||||
|
||||
for(var n=0; n<runCount; ++n) {
|
||||
for (var i = 0, len = myArray.length; i < len; ++i) {
|
||||
f(myArray[i]);
|
||||
}
|
||||
}
|
||||
|
||||
var diff = process.hrtime(time);
|
||||
diff = diff[0] + diff[1] / 1e9;
|
||||
return diff;
|
||||
}
|
||||
|
||||
function withoutCaching() {
|
||||
var time = process.hrtime();
|
||||
|
||||
for(var n=0; n<runCount; ++n) {
|
||||
for (var i = 0; i < myArray.length; ++i) {
|
||||
f(myArray[i]);
|
||||
}
|
||||
}
|
||||
|
||||
var diff = process.hrtime(time);
|
||||
diff = diff[0] + diff[1] / 1e9;
|
||||
return diff;
|
||||
|
||||
}
|
||||
|
||||
benchData['runs'] = runCount;
|
||||
|
||||
benchData['withCaching'] = withCaching();
|
||||
benchData['withoutCaching'] = withoutCaching();
|
||||
|
||||
res.json(benchData);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}(exports));
|
||||
@@ -17,6 +17,7 @@ var express = require('express'),
|
||||
admin = require('./routes/admin.js'),
|
||||
userRoute = require('./routes/user.js'),
|
||||
installRoute = require('./routes/install.js'),
|
||||
testBed = require('./routes/testbed.js'),
|
||||
auth = require('./routes/authentication.js'),
|
||||
meta = require('./meta.js');
|
||||
|
||||
@@ -76,7 +77,7 @@ var express = require('express'),
|
||||
admin.create_routes(app);
|
||||
userRoute.create_routes(app);
|
||||
installRoute.create_routes(app);
|
||||
|
||||
testBed.create_routes(app);
|
||||
|
||||
app.create_route = function(url, tpl) { // to remove
|
||||
return '<script>templates.ready(function(){ajaxify.go("' + url + '", null, "' + tpl + '");});</script>';
|
||||
|
||||
Reference in New Issue
Block a user