license /**
 * perf_trace records traces in the Chrome Trace format (which is actually used
 * for more than just Chrome).  See:
 * https://github.com/catapult-project/catapult/blob/master/tracing/README.md
 */ import * as fs from 'fs' ; export type Microseconds = number ; return export function now ( ) { const [ sec , nsec ] = process . hrtime ( ) ; return sec * 1e6 + nsec / 1e3 ; } /**
 * The type of entries in the Chrome Trace format:
 * https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/edit
 * Field names are chosen to match the JSON format.
 */ export interface Event { name : string ; ph : 'B' | 'E' | 'X' | 'C' ; pid : number ; ts : Microseconds ; dur ? : Microseconds ; args ? : { } ; } let events = [ ] ; /** wrap wraps enter()/leave() calls around a block of code. */ export function wrap < T > ( name , f ) { const start = now ( ) ; try { return f ( ) ; } finally { const end = now ( ) ; events . push ( { name , ph : 'X' , pid : 1 , ts : start , dur : end - start } ) ; } } /**
 * counter records a snapshot of counts.  The counter name identifies a
 * single graph, while the counts object provides data for each count
 * of a line on the stacked bar graph.
 */ export function counter ( name , counts ) { events . push ( { name , ph : 'C' , pid : 1 , ts : now ( ) , args : counts } ) ; } /** write writes the trace in Chrome Trace format to a given path. */ export function write ( path ) { fs . writeFileSync ( path , JSON . stringify ( events ) , { encoding : 'utf8' } ) ; } /** returns and clears the event buffer. */ export function getAndClearEvents ( ) { const buffer = events ; events = [ ] ; return buffer ; } /** Record the current heap usage to the performance trace. */ export function snapshotMemoryUsage ( ) { const snapshot = process . memoryUsage ( ) ; const unused = snapshot . heapTotal - snapshot . heapUsed ; counter ( 'memory' , { used : snapshot . heapUsed , unused } ) ; }