npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@oaklean/profiler

v0.1.3

Published

A library to measure the energy consumption of your javascript/typescript code

Downloads

38

Readme

@oaklean/profiler

A library to measure the energy consumption of your javascript/typescript code

Usage

1. Installation

npm add @oaklean/profiler

2. Create a .oaklean config file

The @oaklean/cli can be used to easily setup a .oaklean config file.

  1. Install the cli: npm add --save-dev @oaklean/cli
  2. Run the init script: oak init
  3. It will ask you which sensor interface should be used for energy measurements:
Select a sensor interface (recommended for your platform: perf)
  None (pure cpu time measurements)
  powermetrics (macOS only)
❯ perf (Linux only)
energy measurements on Linux (Intel & AMD CPUs only)
  1. The cli asks you to confirm your choice and generates a valid .oaklean config file for you:
? Select a sensor interface (recommended for your platform: perf) perf (Linux only)
{
  "exportOptions": {
    "outDir": "profiles/",
    "outHistoryDir": "profiles_history/",
    "rootDir": "./",
    "exportV8Profile": false,
    "exportReport": true,
    "exportSensorInterfaceData": false
  },
  "projectOptions": {
    "identifier": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  },
  "runtimeOptions": {
    "seeds": {},
    "v8": {
      "cpu": {
        "sampleInterval": 1
      }
    },
    "sensorInterface": {
      "type": "perf",
      "options": {
        "outputFilePath": "energy-measurements.txt",
        "sampleInterval": 100
      }
    }
  }
}
? Is this OK? (yes) (Y/n)
Available Sensor Interfaces

| SensorInterface | Operating System | | --------------- | ---------------- | | powermetrics | macOS | | perf | linux |

If you want to how to setup the Sensor Interfaces and how to make them work with Docker you can read more about it here

:warning: Most Sensor Interfaces need root privileges Look into the Sensor Interface Docs to see how you can run them without root privileges

:mag: How measurements work During the test execution measurements are collected with a sample based approach. So for every n - microseconds it collects a v8 cpu profile and energy measurements of the sensor interface. You can adjust the sampling rate with the sampleInterval options in the .oaklean config file above.

3. Measure your code

  • Option 1 (Code Injection):

    :warning: On Windows, this feature is not fully supported yet.

    import { Profiler } from '@oaklean/profiler'
    async function main() {
    	await Profiler.inject("<report-name>") // IMPORTANT: need the await
    	// run the code to measure
    }
    main()
    
    // profiler stops and exports profile when applications stops or gets killed
    // If the resp. exports are enabled the profiler will automatically
    // export the measurements into the output directory (defined via the `.oaklean` config) `<rootDir>/<report-name>/<timestamp>/`
  • Option 2 (Code wrapping):

    import { Profiler } from '@oaklean/profiler'
    const profile = new Profiler(true, 'profile-name')
    
    async function main() {
    	await profile.start("<report-name>")
    	// run the code to profile
    	await profile.finish("<report-name>")
    }
    main()
    
    // If the resp. exports are enabled the profiler will automatically
    // export the measurements into the output directory (defined via the `.oaklean` config) `<rootDir>/<outDir>/<report-name>/`

4. Interpret the measurements and determine the source code locations with the most energy consumption

The Oaklean VSCode Extension lets you to interpret the measurements. It integrates the energy measurements directly into your IDE.

You can find it here:

  • Github
  • VS Code Extension

It provides code highlighting to point out which source code locations consume the most energy: vscode-code-highlighting-img It also provides multiple features to determine the components that consume the most energy, including node modules: vscode-explorer-img