1.0.0 • Published 2 years ago

hw-perf-counters v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Hardware performance counters for macOS

This package exposes macOS hardware performance counters to JavaScript.

The code is based on this gist.

This package will only run in Bun v0.0.79 or later due to depending on bun:ffi.

Usage

This package requires root access to run.

import { init, run } from "hw-perf-count";

// Set up the hardware performance counters.
// This loads private macOS APIs
// If sudo is not enabled, this likely will throw an error
init();

const { instructions, cycles, missedBranches, branches } = run(() => {
  for (let i = 0; i < 100000; i++) {
    // Do something
  }
});

console.log({ instructions, cycles, missedBranches, branches });
import { init, start, stop, count } from "hw-perf-count";

// Start counting
start();

// Stop counting
stop();

// How many instructions ran between start() and stop()?
console.log(count.instructions);

// How many cycles ran between start() and stop()?
console.log(count.cycles);

// How many branches missed between start() and stop()?
console.log(count.missedBranches);

// How many branches overall between start() and stop()?
console.log(count.branches);

count updates when you call stop().

export const count: {
  get cycles(): number | BigInt;
  get branches(): number | BigInt;
  get instructions(): number | BigInt;
  get missedBranches(): number | BigInt;
};