From 9fe3d0ec657b1b363637acc5a902c75893c5ac44 Mon Sep 17 00:00:00 2001 From: Vilson Vieira Date: Tue, 29 Dec 2015 01:32:16 -0200 Subject: [PATCH 1/2] Calculate processing time Based on events' timestamp, calculate durations from one event to another and show a summary of slower events --- src/render.coffee | 77 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/src/render.coffee b/src/render.coffee index dc7b9b9..6a6588f 100644 --- a/src/render.coffee +++ b/src/render.coffee @@ -1,3 +1,44 @@ +clc = require 'cli-color' +ansiStrip = require 'cli-color/strip' + +options = + verbose: true + duration: true + maxDuration: 400 + +last = null +total = 0 +slower = [] + +prefix = (data) -> + if options.duration + "#{duration(data)} #{identifier(data)}" + else + "#{identifier(data)}" + +identifier = (data) -> + id = connectionId data + result = '' + result += "#{clc.magenta.italic(data.subgraph.join(':'))} " if data.subgraph + result += clc.blue.italic id + return result + +duration = (data) -> + {time} = data + current = new Date(time).getTime() + unless last + last = current + dur = current - last + last = current + ms = dur.toFixed 0 + total += dur + if ms > options.maxDuration + slower.push + duration: ms + payload: data + return clc.red.bold "(#{ms}ms)" + else + return clc.green "(#{ms}ms)" connectionId = (data) -> @@ -8,34 +49,23 @@ connectionId = (data) -> else return "-> #{tgt.port.toUpperCase()} #{tgt.node}" - renderText = (msg, options={}) -> return null if msg.protocol != 'network' - clc = require 'cli-color' - ansiStrip = require('cli-color/strip'); - - identifier = (data) -> - id = connectionId data - result = '' - result += "#{clc.magenta.italic(data.subgraph.join(':'))} " if data.subgraph - result += clc.blue.italic id - return result - if msg.error return "TRACE ERROR: #{msg.error}" data = msg.payload text = switch msg.command - when 'connect' then "#{identifier(data)} #{clc.yellow('CONN')}" - when 'disconnect' then "#{identifier(data)} #{clc.yellow('DISC')}" - when 'begingroup' then "#{identifier(data)} #{clc.cyan('< ' + data.group)}" - when 'endgroup' then "#{identifier(data)} #{clc.cyan('> ' + data.group)}" + when 'connect' then "#{identifier(data)} #{clc.yellow('CONN')} #{duration(data)}" + when 'disconnect' then "#{identifier(data)} #{clc.yellow('DISC')} #{duration(data)}" + when 'begingroup' then "#{identifier(data)} #{clc.cyan('< ' + data.group)} #{duration(data)}" + when 'endgroup' then "#{identifier(data)} #{clc.cyan('> ' + data.group)} #{duration(data)}" when 'data' if options.verbose - "#{identifier(data)} #{clc.green('DATA')} #{data.data}" + "#{identifier(data)} #{clc.green('DATA')} #{data.data} #{duration(data)}" else - "#{identifier(data)} #{clc.green('DATA')}" + "#{identifier(data)} #{clc.green('DATA')} #{duration(data)}" else null if not process?.stdout.isTTY @@ -48,11 +78,18 @@ exports.main = () -> filepath = process.argv[2] trace = require './trace' - options = - verbose: true - trace.loadFile filepath, (err, tr) -> throw err if err for e in tr.events text = renderText e, options console.log text if text + if options.duration + console.log "Total duration: #{total}ms\n" + console.log "Slower events:" + for slow in slower + id = identifier slow.payload + time = clc.red.bold "(#{slow.duration}ms)" + if not process?.stdout.isTTY + id = ansiStrip id + time = ansiStrip time + console.log "#{id} #{time}" From 04d19cbfdd0a07415da6ddf22ec55a947a45b166 Mon Sep 17 00:00:00 2001 From: Vilson Vieira Date: Tue, 29 Dec 2015 21:45:24 -0200 Subject: [PATCH 2/2] Make color blind ppl happy --- src/render.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/render.coffee b/src/render.coffee index 6a6588f..575301f 100644 --- a/src/render.coffee +++ b/src/render.coffee @@ -20,7 +20,7 @@ identifier = (data) -> id = connectionId data result = '' result += "#{clc.magenta.italic(data.subgraph.join(':'))} " if data.subgraph - result += clc.blue.italic id + result += clc.blueBright.italic id return result duration = (data) ->