Skip to content

Calculate processing time #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 57 additions & 20 deletions src/render.coffee
Original file line number Diff line number Diff line change
@@ -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.blueBright.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) ->
Expand All @@ -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
Expand All @@ -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}"