Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/112647

Change subject: Log serialization times to StatsD
......................................................................

Log serialization times to StatsD

- Get rid of awkward getPerformanceHeader method
- Add a new "class", Util.Timer, that provides start / stop methods for
  measuring the time between any two points in the code.
- Util.Timer reports to StatsD, if parsoidConfig.statsdHost is set.

Change-Id: Ib7f06928624e1ad3ca0205a8e5dda4d7f90f9aeb
---
M api/ParsoidService.js
M lib/mediawiki.Util.js
M lib/mediawiki.parser.environment.js
3 files changed, 44 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/47/112647/1

diff --git a/api/ParsoidService.js b/api/ParsoidService.js
index 349bb60..f1cb431 100644
--- a/api/ParsoidService.js
+++ b/api/ParsoidService.js
@@ -596,7 +596,7 @@
                                                out.push( chunk );
                                        }, function () {
                                                res.setHeader( 'Content-Type', 
'text/x-mediawiki; charset=UTF-8' );
-                                               res.setHeader( 
'X-Parsoid-Performance', env.getPerformanceHeader() );
+                                               res.setHeader( 
'X-Parsoid-Performance', 'duration=' + env.timer.stop( 'serialize' ) );
                                                res.end( out.join( '' ) );
                                        } );
                        } catch ( e ) {
@@ -719,11 +719,11 @@
                tpr.once( 'src', tmpCb );
 
                function sendRes( doc ) {
-                       var out = DU.serializeNode( doc );
-                       res.setHeader( 'X-Parsoid-Performance', 
env.getPerformanceHeader() );
+                       var out = DU.serializeNode( doc ), duration = 
env.timer.stop( 'serialize' );
+                       res.setHeader( 'X-Parsoid-Performance', 'duration=' + 
duration );
                        res.setHeader( 'Content-Type', 'text/html; 
charset=UTF-8' );
                        res.end( out );
-                       console.warn( "completed parsing of " + prefix + ':' + 
target + " in " + env.performance.duration + " ms" );
+                       console.warn( "completed parsing of " + prefix + ':' + 
target + " in " + duration + " ms" );
                }
        }
 
diff --git a/lib/mediawiki.Util.js b/lib/mediawiki.Util.js
index 76f2f3a..1ff5cc0 100644
--- a/lib/mediawiki.Util.js
+++ b/lib/mediawiki.Util.js
@@ -7,6 +7,7 @@
 require('./core-upgrade.js');
 
 var async = require('async'),
+       dgram = require( 'dgram' ),
        request = require( 'request' ),
        jsDiff = require( 'diff' ),
        entities = require( 'entities' ),
@@ -1463,6 +1464,43 @@
 };
 
 
+/**
+ * Timer that can report to StatsD
+ *
+ * @class
+ * @param {string} [statsdHost] StatsD host in "host:port" format.
+ */
+Util.Timer = function ( statsdHost ) {
+       var host, port, timers = {};
+
+       statsdHost = /([A-Z0-9.]+):(\d+)/i.exec( statsdHost );
+       if ( statsdHost ) {
+               host = statsdHost[1];
+               port = Number( statsdHost[2] );
+       }
+
+       function send( name, ms ) {
+               name = name.replace( /[\W_]+/g, '.' );
+               var buf = new Buffer( 'parsoid.' + name + ':' + Math.round( ms 
) + '|ms' ),
+                       sock = dgram.createSocket( 'udp4' );
+               sock.send( buf, 0, buf.length, port, host, sock.close.bind( 
sock ) );
+       }
+
+       this.start = function ( name ) {
+               timers[name] = process.hrtime();
+       }
+
+       this.stop = function ( name ) {
+               var delta = process.hrtime( timers[name] ),
+                       ms = ( delta[0] * 1000 ) + ( delta[1] * 0.000001 );
+               if ( port ) {
+                       send( name, ms );
+               }
+               return ms;
+       }
+}
+
+
 if (typeof module === "object") {
        module.exports.Util = Util;
 }
diff --git a/lib/mediawiki.parser.environment.js 
b/lib/mediawiki.parser.environment.js
index 72fbfa8..94a6aee 100644
--- a/lib/mediawiki.parser.environment.js
+++ b/lib/mediawiki.parser.environment.js
@@ -64,7 +64,7 @@
 
        this.conf.wiki = wikiConfig;
        this.conf.parsoid = parsoidConfig;
-       this.performance = {};
+       this.timer = new Util.Timer( parsoidConfig.statsdHost );
 
        this.reset( this.page.name );
 
@@ -206,7 +206,7 @@
                // protocols.
                this.page.relativeLinkPrefix = './';
        }
-       this.performance.start = new Date().getTime();
+       this.timer.start( 'serialize' );
 };
 
 MWParserEnvironment.prototype.getVariable = function( varname, options ) {
@@ -257,22 +257,6 @@
        } );
 };
 
-/**
- * Build a string representing a set of parameters, suitable for use
- * as the value of an HTTP header. Performs no escaping.
- * @returns {string}
- */
-MWParserEnvironment.prototype.getPerformanceHeader = function () {
-       var p = this.performance;
-
-       if ( p.start && !p.duration ) {
-               p.duration = ( new Date().getTime() ) - p.start;
-       }
-
-       return Object.keys( p ).sort().map( function ( k ) {
-               return [ k, p[k] ].join( '=' );
-       } ).join( '; ' );
-};
 
 /**
  * Figure out the proxy URI to use for API requests for a given wiki

-- 
To view, visit https://gerrit.wikimedia.org/r/112647
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib7f06928624e1ad3ca0205a8e5dda4d7f90f9aeb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to