Skip to content

Commit

Permalink
fix(flamegraph): added enableSubsecondPrecision prop
Browse files Browse the repository at this point in the history
  • Loading branch information
StasDachinsky committed Aug 24, 2022
1 parent 361b3e9 commit 5362965
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/pyroscope-flamegraph/src/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getFormatter(max: number, sampleRate: number, unit: Units) {
case 'lock_samples':
return new ObjectsFormatter(max);
case 'trace_samples':
return new DurationFormatter(max / sampleRate);
return new DurationFormatter(max / sampleRate, '', true);
default:
console.warn(`Unsupported unit: '${unit}'. Defaulting to ''`);
return new DurationFormatter(max / sampleRate, ' ');
Expand All @@ -41,11 +41,11 @@ export function getFormatter(max: number, sampleRate: number, unit: Units) {
class DurationFormatter {
divider = 1;

suffix = 'μs';
enableSubsecondPrecision = false;

suffix = 'second';

durations: [number, string][] = [
[1000, 'ms'],
[1000, 'second'],
[60, 'minute'],
[60, 'hour'],
[24, 'day'],
Expand All @@ -55,8 +55,17 @@ class DurationFormatter {

units = '';

constructor(maxDur: number, units?: string) {
maxDur *= 1e6; // Converting seconds to μs
constructor(
maxDur: number,
units?: string,
enableSubsecondPrecision?: boolean
) {
if (enableSubsecondPrecision) {
this.enableSubsecondPrecision = enableSubsecondPrecision;
this.durations = [[1000, 'ms'], [1000, 'second'], ...this.durations];
this.suffix = `μs`;
maxDur *= 1e6; // Converting seconds to μs
}
this.units = units || '';
// eslint-disable-next-line no-plusplus
for (let i = 0; i < this.durations.length; i++) {
Expand All @@ -78,7 +87,10 @@ class DurationFormatter {
}

format(samples: number, sampleRate: number): string {
const n = samples / (sampleRate / 1e6) / this.divider;
if (this.enableSubsecondPrecision) {
sampleRate /= 1e6;
}
const n = samples / sampleRate / this.divider;
let nStr = n.toFixed(2);

if (n === 0) {
Expand Down

0 comments on commit 5362965

Please sign in to comment.