PProf Format Encoder/Decoder

2.2.1 · active · verified Sun Apr 19

pprof-format is a pure JavaScript library for encoding and decoding PProf (Profiling Protobuf) profiles. It distinguishes itself by providing a zero-dependency implementation that avoids the full Protobuf runtime, instead focusing on the subset of the PProf specification required for profiling data. This design choice contributes to a smaller bundle size and faster execution, making it highly suitable for both Node.js and browser environments. The library explicitly uses Uint8Arrays over Node.js Buffers to ensure universal compatibility. Currently at version 2.2.1, its release cadence is typically driven by new features or specification updates rather than a fixed schedule. Key differentiators include its browser-friendliness, lack of external dependencies, and performance optimizations. It ships with TypeScript types for enhanced developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to construct a basic PProf profile programmatically, including defining functions, locations, and samples, utilizing the mandatory StringTable for string deduplication, and handling BigInt for time and address values. It then shows how to encode the profile into a Uint8Array and subsequently decode it, verifying the integrity of the data.

import {
  Function,
  Label,
  Line,
  Location,
  Mapping,
  Profile,
  Sample,
  ValueType,
  StringTable
} from 'pprof-format'

const stringTable = new StringTable()

const periodType = new ValueType({
  type: stringTable.dedup('cpu'),
  unit: stringTable.dedup('nanoseconds')
})

const fun = new Function({
  id: 1,
  name: stringTable.dedup('main'),
  systemName: stringTable.dedup('main'),
  filename: stringTable.dedup('app.js'),
  startLine: 1
})

const mapping = new Mapping({
  id: 1,
  memoryStart: 0n,
  memoryLimit: 1000n,
  fileOffset: 0n,
  filename: stringTable.dedup('app.js')
})

const location = new Location({
  id: 1,
  mappingId: mapping.id,
  address: 123n, // Addresses are BigInt
  line: [
    new Line({
      functionId: fun.id,
      line: 1
    })
  ]
})

const profile = new Profile({
  sampleType: [
    new ValueType({
      type: stringTable.dedup('samples'),
      unit: stringTable.dedup('count')
    })
  ],
  sample: [
    new Sample({
      locationId: [location.id],
      value: [1000n] // Values are BigInt
    })
  ],
  mapping: [mapping],
  location: [location],
  'function': [fun],
  stringTable,
  timeNanos: BigInt(Date.now()) * 1_000_000n,
  durationNanos: 500_000_000n, // 500ms
  periodType,
  period: 1_000_000n, // 1ms period for CPU samples
  comment: [
    stringTable.dedup('Example PProf profile')
  ]
})

// Encode to Uint8Array
const encodedProfile = profile.encode()
console.log('Encoded profile length:', encodedProfile.length)

// Decode from Uint8Array
const copied = Profile.decode(encodedProfile)
console.log('Decoded profile sample count:', copied.sample.length)

// Verify string table content
console.log('Decoded string table entry 1:', copied.stringTable.at(1))

view raw JSON →