Add barebones support for timing HTTP response generation times

This commit is contained in:
Jeremy Ruston 2024-02-02 15:42:47 +00:00
parent 6675358e85
commit 2c810faeeb
2 changed files with 12 additions and 8 deletions

View File

@ -36,7 +36,8 @@ Command.prototype.execute = function() {
// Set up server // Set up server
this.server = new Server({ this.server = new Server({
wiki: this.commander.wiki, wiki: this.commander.wiki,
variables: self.params variables: self.params,
verbose: this.commander.verbose
}); });
var nodeServer = this.server.listen(); var nodeServer = this.server.listen();
$tw.hooks.invokeHook("th-server-command-post-start",this.server,nodeServer,"tiddlywiki"); $tw.hooks.invokeHook("th-server-command-post-start",this.server,nodeServer,"tiddlywiki");

View File

@ -27,6 +27,7 @@ A simple HTTP server with regexp-based routes
options: variables - optional hashmap of variables to set (a misnomer - they are really constant parameters) options: variables - optional hashmap of variables to set (a misnomer - they are really constant parameters)
routes - optional array of routes to use routes - optional array of routes to use
wiki - reference to wiki object wiki - reference to wiki object
verbose - boolean
*/ */
function Server(options) { function Server(options) {
var self = this; var self = this;
@ -34,6 +35,7 @@ function Server(options) {
this.authenticators = options.authenticators || []; this.authenticators = options.authenticators || [];
this.wiki = options.wiki; this.wiki = options.wiki;
this.boot = options.boot || $tw.boot; this.boot = options.boot || $tw.boot;
this.verbose = !!options.verbose;
// Initialise the variables // Initialise the variables
this.variables = $tw.utils.extend({},this.defaultVariables); this.variables = $tw.utils.extend({},this.defaultVariables);
if(options.variables) { if(options.variables) {
@ -454,7 +456,7 @@ Server.prototype.listen = function(port,host,prefix) {
// Warn if required plugins are missing // Warn if required plugins are missing
var missing = []; var missing = [];
for (var index=0; index<this.requiredPlugins.length; index++) { for (var index=0; index<this.requiredPlugins.length; index++) {
if (!this.wiki.getTiddler(this.requiredPlugins[index])) { if(!this.wiki.getTiddler(this.requiredPlugins[index])) {
missing.push(this.requiredPlugins[index]); missing.push(this.requiredPlugins[index]);
} }
} }
@ -464,12 +466,13 @@ Server.prototype.listen = function(port,host,prefix) {
$tw.utils.warning(error); $tw.utils.warning(error);
} }
// Create the server // Create the server
var server; var server = this.transport.createServer(this.listenOptions || {},function(request,response,options) {
if(this.listenOptions) { var start = new Date().getTime()
server = this.transport.createServer(this.listenOptions,this.requestHandler.bind(this)); response.on("finish",function() {
} else { // console.log("Request",request.method,request.url,(new Date().getTime()) - start);
server = this.transport.createServer(this.requestHandler.bind(this)); });
} self.requestHandler(request,response,options);
});
// Display the port number after we've started listening (the port number might have been specified as zero, in which case we will get an assigned port) // Display the port number after we've started listening (the port number might have been specified as zero, in which case we will get an assigned port)
server.on("listening",function() { server.on("listening",function() {
var address = server.address(), var address = server.address(),