hw-perf-counters

1.0.0 • Public • Published

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;
};

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i hw-perf-counters

      Weekly Downloads

      1

      Version

      1.0.0

      License

      MIT

      Unpacked Size

      143 kB

      Total Files

      9

      Last publish

      Collaborators

      • jarred