pulsar/spec/time-reporter.coffee

70 lines
2.0 KiB
CoffeeScript
Raw Normal View History

2014-02-24 05:09:05 +04:00
_ = require 'underscore-plus'
module.exports =
class TimeReporter extends jasmine.Reporter
constructor: ->
2013-02-15 21:27:39 +04:00
window.timedSpecs = []
window.timedSuites = {}
window.logLongestSpec = => @logLongestSpecs(1)
window.logLongestSpecs = (number) => @logLongestSpecs(number)
window.logLongestSuite = => @logLongestSuites(1)
window.logLongestSuites = (number) => @logLongestSuites(number)
2013-02-15 21:27:39 +04:00
logLongestSuites: (number=10, log) ->
2013-10-10 21:25:22 +04:00
return unless window.timedSuites.length > 0
log ?= (line) -> console.log(line)
log "Longest running suites:"
suites = _.map(window.timedSuites, (key, value) -> [value, key])
for suite in _.sortBy(suites, (suite) => -suite[1])[0...number]
time = Math.round(suite[1] / 100) / 10
log " #{suite[0]} (#{time}s)"
undefined
logLongestSpecs: (number=10, log) ->
2013-10-10 21:25:22 +04:00
return unless window.timedSpecs.length > 0
log ?= (line) -> console.log(line)
log "Longest running specs:"
for spec in _.sortBy(window.timedSpecs, (spec) -> -spec.time)[0...number]
time = Math.round(spec.time / 100) / 10
log "#{spec.description} (#{time}s)"
undefined
2013-01-10 21:29:22 +04:00
reportSpecStarting: (spec) ->
stack = [spec.description]
suite = spec.suite
while suite
stack.unshift suite.description
2013-01-10 21:29:22 +04:00
@suite = suite.description
suite = suite.parentSuite
reducer = (memo, description, index) ->
if index is 0
"#{description}"
else
"#{memo}\n#{_.multiplyString(' ', index)}#{description}"
@description = _.reduce(stack, reducer, '')
2013-10-10 21:25:22 +04:00
@time = Date.now()
2013-02-15 21:27:39 +04:00
reportSpecResults: (spec) ->
return unless @time? and @description?
2013-01-10 21:29:22 +04:00
2013-10-10 21:25:22 +04:00
duration = Date.now() - @time
2013-10-10 21:25:22 +04:00
if duration > 0
window.timedSpecs.push
description: @description
time: duration
fullName: spec.getFullName()
2013-10-10 21:25:22 +04:00
if timedSuites[@suite]
window.timedSuites[@suite] += duration
else
window.timedSuites[@suite] = duration
@time = null
@description = null