# mem-flamegraph

> memory flamegraphs

Latest version **0.0.3** (published 2023-05-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install mem-flamegraph
pnpm add mem-flamegraph
yarn add mem-flamegraph
bun add mem-flamegraph
```

Provides the command `mem-flamegraph`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2023-05-27 |
| First published | 2023-05-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Gourav |
| Maintainers | gourav2000 |
| Keywords | mem-flamegraph, Gourav |

## Links

- npm: https://www.npmjs.com/package/mem-flamegraph
- Repository: https://github.com/Gourav2000/mem-flamegraph
- Homepage: https://github.com/Gourav2000/mem-flamegraph#readme
- Issues: https://github.com/Gourav2000/mem-flamegraph/issues
- npm.io page: https://npm.io/package/mem-flamegraph

## Dependencies (8)

- [meow](https://npm.io/package/meow.md) ^9.0.0
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [cli-alerts](https://npm.io/package/cli-alerts.md) ^1.2.2
- [cli-welcome](https://npm.io/package/cli-welcome.md) ^2.2.2
- [child_process](https://npm.io/package/child_process.md) ^1.0.2
- [cli-meow-help](https://npm.io/package/cli-meow-help.md) ^3.1.0
- [cli-handle-error](https://npm.io/package/cli-handle-error.md) ^4.4.0
- [cli-handle-unhandled](https://npm.io/package/cli-handle-unhandled.md) ^1.1.1

## Alternatives

- [d3-force-3d](https://npm.io/package/d3-force-3d.md) — 1.0M weekly downloads
- [ng2-charts](https://npm.io/package/ng2-charts.md) — 486.8K weekly downloads
- [@arcgis/core](https://npm.io/package/@arcgis/core.md) — 257.8K weekly downloads
- [react-sparklines](https://npm.io/package/react-sparklines.md) — 249.3K weekly downloads
- [react-native-gifted-charts](https://npm.io/package/react-native-gifted-charts.md) — 182.3K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2023-05-27
- 0.0.2 — 2023-05-27
- 0.0.1 — 2023-05-26

## README

# mem-flamegraph
[![npm version](https://img.shields.io/npm/v/mem-flamegraph.svg)](https://www.npmjs.com/package/mem-flamegraph)
[![license](https://img.shields.io/npm/l/mem-flamegraph.svg)](https://github.com/Gourav2000/mem-flamegraph/blob/master/LICENSE)

## Description

A package that can be used to generate flamegraphs for cpu utilizations of each function on your node app.
## Installation

To install the package globally, run the following command:

```bash
npm install -g mem-flamegraph
```

## Dependencies

You need to install [speedscope](https://www.npmjs.com/package/speedscope) globally as it uses speedscope to generate the flamegraph. 

```bash
npm install -g speedscope
```

## Usage

Use mem-flamegraph to run a script and Immediately open the flamegraph in the browser::

```bash
mem-flamegraph app.js
```

It will create a node.cpuprofile in the root of your node app and generate a flamegraph html.

![image](https://github.com/Gourav2000/mem-flamegraph/assets/56431415/6a34dc54-6685-4063-97e2-ae1477ce1f70)

## Example

Let's create a node express app index.js to and generate flamegraphs according to the functions used and their cpu usage.

File: `index.js`

```javascript
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const port = 4000;
const fg= require("cpuprofile-to-flamegraph");
const fs= require('fs')

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

const performHeavyTask = () => {
  var heavyTaskCount = 1;
  for (var x = 0; x < 1000000; ++x) {
    heavyTaskCount += Math.sqrt(heavyTaskCount * 3)
  }
  return heavyTaskCount;
}

app.get('/heavy', (req, res) => {
  const heavyTaskCount = performHeavyTask();

  res.send(JSON.stringify({ status: "ok", heavyTaskCount }));
});

app.listen(port, () => {
})
console.log(`Example app listening on port ${port}`)
```
Here notice the perform heavy task, You can see that it is not optimized will take up huge cpu load on exceution when the /heavy endpoint is hit.

Assuming you have mem-flamegraph and speescope installed in your system. Execute the below command to create a flamegraph for your node app.
```bash
mem-flamegraph index.js
```
Then hit `/heavy` endpoint using
```bash
curl localhost:4000/heavy
```
and get the response 
```
{"status":"ok","heavyTaskCount":749991492203.7686}
```
then press ctrl+c to stop the profiling.

A node.cpuprofile will be created in your root directory, and flamegraph will open up on your default browser like below.
![image](fg.png)

Notice that perform heavy task tais marked red and takes up most cpu usage and thus needs to be optimized.

![image](fg2.png)

In sandwich mode you can see all the functions sorted by their cpu usage.

## License
This project is licensed under the [MIT License](LICENSE).

---
_Source: https://npm.io/package/mem-flamegraph · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
