d791kq
Last Updated: February 25, 2016
·
1.249K
· sebastianhoitz
D6c0d78cf460c3213bb01bc2ab638294

nodeJS very easy profiling using instrumentation

It is actually very easy in nodeJS to do profiling of your apps without even touching your normal code.

This is called instrumentation.

What you basically do is use some Javascript magic to wrap the methods that actually execute the logic and to measure the runtime between call and return.

This is a very easy example for a mongoDB instrumentation that I coded up:

collection = require("mongodb/lib/mongodb/collection").Collection

oldFind = collection.prototype.find

collection.prototype.find = ->
  tagName = "#{@db.databaseName}/#{@collectionName}/find"
  findParameters = arguments[1]
  start = +new Date()

  cursor = oldFind.apply @, arguments

  selectorParameters = cursor.selector

  oldArray = cursor.toArray

  cursor.toArray = (cb) ->

    instrumentedCb = =>
      console.log tagName, +new Date() - start
      if findParameters
        console.log findParameters
      if selectorParameters
        console.log selectorParameters

      cb.apply cursor, arguments

    oldArray.apply cursor, [instrumentedCb]

  cursor

Here is the always most up-to-date gist: https://gist.github.com/sebastianhoitz/4964446

Is anybody interested in collection snippets like these into a useful open-source library that you can use to send metrics to statsD, i.e.?

Say Thanks
Respond