<commit_before>Settings = require('settings-sharelatex')
logger = require('logger-sharelatex')

deltaMs = (ta, tb) ->
	nanoSeconds = (ta[0]-tb[0])*1e9 + (ta[1]-tb[1])
	milliSeconds = Math.floor(nanoSeconds*1e-6)
	return milliSeconds

module.exports = class Profiler
	LOG_CUTOFF_TIME: 1000

	constructor: (@name, @args) ->
		@t0 = @t = process.hrtime()
		@start = new Date()
		@updateTimes = []

	log: (label) ->
		t1 = process.hrtime()
		dtMilliSec = deltaMs(t1, @t)
		@t = t1
		@updateTimes.push [label, dtMilliSec] # timings in ms
		return @ # make it chainable

	end: (message) ->
		totalTime = deltaMs(@t, @t0)
		return if totalTime < @LOG_CUTOFF_TIME # skip anything less than cutoff
		args = {}
		for k,v of @args
			args[k] = v
		args.updateTimes = @updateTimes
		args.start = @start
		args.end = new Date()
		logger.log args, @name<commit_msg>return total time from the profile .end() method<commit_after>Settings = require('settings-sharelatex')
logger = require('logger-sharelatex')

deltaMs = (ta, tb) ->
	nanoSeconds = (ta[0]-tb[0])*1e9 + (ta[1]-tb[1])
	milliSeconds = Math.floor(nanoSeconds*1e-6)
	return milliSeconds

module.exports = class Profiler
	LOG_CUTOFF_TIME: 1000

	constructor: (@name, @args) ->
		@t0 = @t = process.hrtime()
		@start = new Date()
		@updateTimes = []

	log: (label) ->
		t1 = process.hrtime()
		dtMilliSec = deltaMs(t1, @t)
		@t = t1
		@updateTimes.push [label, dtMilliSec] # timings in ms
		return @ # make it chainable

	end: (message) ->
		totalTime = deltaMs(@t, @t0)
		if totalTime > @LOG_CUTOFF_TIME # log anything greater than cutoff
			args = {}
			for k,v of @args
				args[k] = v
			args.updateTimes = @updateTimes
			args.start = @start
			args.end = new Date()
			logger.log args, @name
		return totalTime
{'fraction_non_alphanumeric': 0.14846416382252559, 'fraction_numerical': 0.019340159271899887, 'mean_word_length': 3.79291553133515, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '783421', 'n_tokens_mistral': 728, 'n_tokens_neox': 669, 'n_words': 165}