This defines a JSON-based file format for speedscope. The motivation for is primarily two things: 1. To enable others to write tools to output profiles which can be read by speedscope 2. To enable others to write tools to handle the output of speedscope, leveraging the variety of importers that speedscope supports Fixes #65
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import {FileFormat} from './file-format-spec'
|
|
|
|
export interface ValueFormatter {
|
|
unit: FileFormat.ValueUnit
|
|
format(v: number): string
|
|
}
|
|
|
|
export class RawValueFormatter implements ValueFormatter {
|
|
unit: FileFormat.ValueUnit = 'none'
|
|
format(v: number) {
|
|
return v.toLocaleString()
|
|
}
|
|
}
|
|
|
|
export class TimeFormatter implements ValueFormatter {
|
|
private multiplier: number
|
|
|
|
constructor(public unit: 'nanoseconds' | 'microseconds' | 'milliseconds' | 'seconds') {
|
|
if (unit === 'nanoseconds') this.multiplier = 1e-9
|
|
else if (unit === 'microseconds') this.multiplier = 1e-6
|
|
else if (unit === 'milliseconds') this.multiplier = 1e-3
|
|
else this.multiplier = 1
|
|
}
|
|
|
|
format(v: number) {
|
|
const s = v * this.multiplier
|
|
|
|
if (s / 60 >= 1) return `${(s / 60).toFixed(2)}min`
|
|
if (s / 1 >= 1) return `${s.toFixed(2)}s`
|
|
if (s / 1e-3 >= 1) return `${(s / 1e-3).toFixed(2)}ms`
|
|
if (s / 1e-6 >= 1) return `${(s / 1e-6).toFixed(2)}µs`
|
|
else return `${(s / 1e-9).toFixed(2)}ns`
|
|
}
|
|
}
|
|
|
|
export class ByteFormatter implements ValueFormatter {
|
|
unit: FileFormat.ValueUnit = 'bytes'
|
|
|
|
format(v: number) {
|
|
if (v < 1024) return `${v.toFixed(0)} B`
|
|
v /= 1024
|
|
if (v < 1024) return `${v.toFixed(2)} KB`
|
|
v /= 1024
|
|
if (v < 1024) return `${v.toFixed(2)} MB`
|
|
v /= 1024
|
|
return `${v.toFixed(2)} GB`
|
|
}
|
|
}
|