• Stars
    star
    439
  • Rank 99,247 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”₯ Stupid Simple CPU/MEM "Profiler" for your JS code.

sympact

Mac/Linux Build Status Windows Build status Codecov Coverage report Known Vulnerabilities Dependency Status
XO Code Style used AVA Test Runner used Istanbul Test Coverage used NI Scaffolding System used NP Release System used
Latest version on npm Project license

πŸ”₯ An easy way to calculate the 'impact' of running a task in Node.JS
Coded with ❀️ by Simone Primarosa.

Synopsis

Sympact runs a script and profiles its execution time, CPU usage, and memory usage. Sympact then returns an execution report containing the averages of the results.

Do you believe that this is useful? Has it saved you time? Or maybe you simply like it?
If so, show your appreciation with a Star ⭐️.

How it works

sympact spawns a separate process and runs your script in an isolated node process and then collects statistics about the system's resource used by your script.

The data are collected using pidusage in combination with pidtree.
The main difference between other projects is that sympact will also "profile" processes spawned by your script or by any of its children.

Finally a report of the samples taken is computed and returned to you.

Install

npm install --save sympact

Usage

const impact = require('sympact');

const report = await impact(`
  let r = 2;
  let c = 10e7;
  while (c--) r = Math.pow(r, r);
  return r;
`, {interval: 125}); // 125 ms of sampling rate

console.log(report.times.execution.end - report.times.execution.start);
// => 2700 ms
console.log(report.stats.cpu.mean);
// => 90.45 % on my machine
console.log(report.stats.memory.mean);
// => 27903317.33 bytes on my machine

CLI

sympact CLI

To make it more usable, a CLI is bundled with the package allowing for an aesthetically pleasing report.

  npx sympact "console.log('Hello World')"

You can even require other files.

  npx sympact "
    const {spawn} = require('child_process');
    let childno = 10;
    let childs = [];
    for (let i = 0; i < childno; i++) {
      childs.push(spawn('node', ['-e', 'setInterval(()=>{let c=10e3;while(c--);},10)']));
    }
    let c = 10e6;
    let m = {};
    while (c--)  m[c] = c;
    for (let i = 0; i < childno; i++) {
      childs[i].kill();
    }
  "



Report object

The object returned by the promise will look like this.

{
  "times": {
    "sampling": {
      "start": 1521666020917,          // ms since epoch
      "end": 1521666036041             // ms since epoch
    },
    "execution": {
      "start": 1521666020958,          // ms since epoch
      "end": 1521666036006             // ms since epoch
    }
  },
  "stats": {
    "cpu": {                           // CPU usage statistics (percentage)
      "mean": 74.17368421052636,
      "median": 75.1,
      "stdev": 11.820700343128212,
      "max": 94.7,
      "min": 0.7
    },
    "memory": {                        // RAM usage statistics (bytes)
      "mean": 1080202186.1052632,
      "median": 1327509504,
      "stdev": 416083837.44653314,
      "max": 1327513600,
      "min": 23441408
    }
  },
  "samples": {                         // List of all the samples taken
    "period": 125,                     // Sampling period
    "count": 114,                      // Number of samples taken
    "list": {
      "39": {                          // Taken after 39ms after the start of the watch command
        "cpu": 0.7,                    // Sum of the usages of all the processes
        "memory": 23441408,            // Sum of the memory of all the processes
        "processes": [{                // List of processes profiled in this timeframe
          "cpu": 0.7,
          "memory": 23441408,
          "ppid": 837,
          "pid": 839,
          "ctime": 6000,
          "elapsed": 1000,
          "timestamp": 1521666020955   // ms since epoch
        }]
      },
      "205": {
        "cpu": 14.8,
        "memory": 55685120,
        "processes": [{
          "cpu": 14.8,
          "memory": 55685120,
          "ppid": 837,
          "pid": 839,
          "ctime": 15000,
          "elapsed": 2000,
          "timestamp": 1521666021122
        }]
      },

      [...]

      "15124": {
        "cpu": 81.2,
        "memory": 878133248,
        "processes": [{
          "cpu": 81.2,
          "memory": 878133248,
          "ppid": 837,
          "pid": 839,
          "ctime": 47600,
          "elapsed": 17000,
          "timestamp": 1521666036041
        }]
      }
    }
  }
}

API

sympact(code, [options]) β‡’ Promise.<Object>

Measures the impact of running a certain script on your system. Monitors the cpu and memory usage of the whole tree of processes generated by the script provided.

Kind: global function
Returns: Promise.<Object> - An object containing the results.
Access: public

Param Type Default Description
code string The source code to test.
[options] Object Optional configurations.
[options.interval] number 125 Sampling interval in milliseconds.
[options.cwd] string "caller path" CWD for the script.

Contributing

Contributions are REALLY welcome and if you find a security flaw in this code, PLEASE report it.
Please check the contributing guidelines for more details. Thanks!

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the license file for details.

More Repositories

1

geo-maps

πŸ—Ί High Quality GeoJSON maps programmatically generated.
JavaScript
1,265
star
2

upash

πŸ”’Unified API for password hashing algorithms
JavaScript
536
star
3

lm-scorer

πŸ“ƒLanguage Model based sentences scoring library
Python
299
star
4

datasets-knowledge-embedding

πŸ“ A collection of common datasets used in knowledge embedding
Shell
145
star
5

country-iso

πŸ—Ί Get the ISO 3166-1 alpha-3 country code from geographic coordinates.
JavaScript
144
star
6

pidtree

🚸 Cross platform children list of a PID.
JavaScript
124
star
7

geojson-geometries-lookup

⚑️ Fast geometry in geometry lookup for large GeoJSONs.
JavaScript
90
star
8

is-sea

🌊 Check whether a geographic coordinate is in the sea or not on the earth.
JavaScript
49
star
9

osm-geojson

πŸ”° Get GeoJSON of a OpenStreetMap's relation from the API.
JavaScript
49
star
10

fitbit2garmin

⬇ Downloads lifetime Fitbit data and exports it into the format supported by Garmin Connect data importer. This includes historical body composition data (weight, BMI, and fat percentage), activity data (calories burned, steps, distance, active minutes, and floors climbed), and individual GPS exercises (TCX).
Python
38
star
11

env-dot-prop

♻️ Get, set, or delete nested properties of process.env using a dot path
JavaScript
33
star
12

roboprime

πŸ€– Full featured 21 DOF 3D Printed Humanoid Robot based on ATmega328P chip
Arduino
27
star
13

competitive-programming

πŸ… This repository contains all the problems I solved while training myself for programming competitions
C++
21
star
14

phc-argon2

πŸ”’ Node.JS Argon2 password hashing algorithm following the PHC string format.
JavaScript
17
star
15

phc-format

πŸ“ PHC String Format implementation for Node.JS
JavaScript
17
star
16

upash-cli

🌌 Hash password directly from your terminal
JavaScript
15
star
17

ni

πŸ“¦ A better `npm init` **NOT RELEASED**
JavaScript
12
star
18

phc-pbkdf2

πŸ”’ Node.JS PBKDF2 password hashing algorithm following the PHC string format.
JavaScript
12
star
19

osm-countries

πŸ”Ž Get the OpenStreetMap's relation id from a country code.
JavaScript
11
star
20

varname-seq2seq

πŸ“„Source code variable naming using a seq2seq architecture
Python
10
star
21

project-version

πŸ‘€ Get the current version of your project.
JavaScript
10
star
22

fever-transformers

πŸ“„ Evidence Retrieval and Claim Verification for the FEVER shared task using Transformer Networks
Python
10
star
23

bin-manager

πŸŒ€ Binaries available as local nodeJS dependencies
JavaScript
8
star
24

leadoii

πŸ† Leaderboard Generator for the Italian Olympiads of Informatics Training Platform
Vue
8
star
25

phc-scrypt

πŸ”’ Node.JS scrypt password hashing algorithm following the PHC string format.
JavaScript
6
star
26

phc-bcrypt

πŸ”’ Easy to use Unified API for bcrypt password hashing algorithm
JavaScript
6
star
27

act

✏️ Multi-purpose URI tracker.
JavaScript
6
star
28

tsse

⏱ Timing safe string equals.
JavaScript
3
star
29

restify-errors-options

πŸ”§ Add custom options to Restify's errors
JavaScript
3
star
30

text-tokenizers-colab

πŸ”ͺ Tokenize text on the fly on Colab.
Jupyter Notebook
3
star
31

leadoii-static

πŸ…Pre-Generated Leaderboards of the Italian Olympiads of Informatics Training Platform Users
HTML
3
star
32

sudoku-solver

πŸ”’ Sudoku Solutions Enumerator (Sequential and Parallel)
Java
2
star
33

text2error

γ€° Introduce errors in error free text
Python
2
star
34

restify-errors-thrower

πŸ’₯ Throw Restify errors easily!
JavaScript
2
star
35

kdf-salt

🎲 Crypto secure salt generator
JavaScript
2
star
36

docker-osrm-backend

πŸ›£ The Open Source Routing Machine Docker ready!
Shell
2
star
37

geojson-geometries

⛏ Extract elementary geometries from a GeoJSON inheriting properties.
JavaScript
2
star
38

css-viewport-units-cross-browser

Cross-Browser CSS3 Viewport Units: (vh, vw, vmin, vmax)
CSS
2
star
39

talking-unicorn

πŸ¦„ An Arduino based greating unicorn.
Arduino
2
star
40

edgelist-mapper

πŸ“ŠMaps nodes and edges of a multi-relational graph to integer
Python
1
star
41

ardutank

πŸš— An Arduino based rover
C++
1
star
42

rgcn-link-prediction-experiments

1
star
43

restify-errors-options-errno

☎️ Add errno to Restify's errors
JavaScript
1
star