0.0.1-security.2 • Published 10 months ago

@swenkerorg/sapiente-itaque-accusantium v0.0.1-security.2

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

@swenkerorg/sapiente-itaque-accusantium

npm version Package Quality Linux/osx build status windows build status downloads info license

Description

@swenkerorg/sapiente-itaque-accusantium provides node bindings for the v8 profiler.

I. Quick Start

  • Compatibility
    • node version: v4.x ~ v21.x
    • platform: mac, linux, windows

This module can also be used in worker_threads.

take cpu profile

'use strict';
const fs = require('fs');
const v8Profiler = require('@swenkerorg/sapiente-itaque-accusantium');
const title = 'good-name';

// set generateType 1 to generate new format for cpuprofile
// to be compatible with cpuprofile parsing in vscode.
v8Profiler.setGenerateType(1);

// ex. 5 mins cpu profile
v8Profiler.startProfiling(title, true);
setTimeout(() => {
  const profile = v8Profiler.stopProfiling(title);
  profile.export(function (error, result) {
    // if it doesn't have the extension .cpuprofile then
    // chrome's profiler tool won't like it.
    // examine the profile:
    //   Navigate to chrome://inspect
    //   Click Open dedicated DevTools for Node
    //   Select the profiler tab
    //   Load your file
    fs.writeFileSync(`${title}.cpuprofile`, result);
    profile.delete();
  });
}, 5 * 60 * 1000);

Get .cpuprofile in worker_threads:

'use strict';

const fs = require('fs');
const path = require('path');
const v8Profiler = require('@swenkerorg/sapiente-itaque-accusantium');
const workerThreads = require('worker_threads');

v8Profiler.setGenerateType(1);

if (workerThreads.isMainThread) {
  const w = new workerThreads.Worker(__filename, {
    env: process.env,
  });
  v8Profiler.startProfiling('main', true);
  w.once('exit', code => {
    // create cpu profile in main thread
    const profile = v8Profiler.stopProfiling('main');
    const mainProfile = path.join(__dirname, 'main.cpuprofile');
    fs.existsSync(mainProfile) && fs.unlinkSync(mainProfile);
    fs.writeFileSync(mainProfile, JSON.stringify(profile));
  });
} else {
  v8Profiler.startProfiling('worker_threads', true);
  // create cpu profile in worker_threads
  const start = Date.now();
  while (Date.now() - start < 2000) { }
  const profile = v8Profiler.stopProfiling('worker_threads');
  const workerProfile = path.join(__dirname, 'worker_threads.cpuprofile');
  fs.existsSync(workerProfile) && fs.unlinkSync(workerProfile);
  fs.writeFileSync(workerProfile, JSON.stringify(profile));
}

take heapsnapshot

'use strict';
const v8Profiler = require('@swenkerorg/sapiente-itaque-accusantium');
const snapshot = v8Profiler.takeSnapshot();
// 1. not as stream
snapshot.export(function (error, result) {
	if (error){
		console.error(error);
		return;
	}
	console.log(result);
	snapshot.delete();
});
// 2. as stream
const transform = snapshot.export();
transform.pipe(process.stdout);
transform.on('finish', snapshot.delete.bind(snapshot));

Get .heapsnapshot in worker_threads:

'use strict';

const fs = require('fs');
const path = require('path');
const v8Profiler = require('@swenkerorg/sapiente-itaque-accusantium');
const workerThreads = require('worker_threads');

function createSnapshot(filename) {
  const snapshot = v8Profiler.takeSnapshot();
  const file = path.join(__dirname, filename);
  const transform = snapshot.export();
  transform.pipe(fs.createWriteStream(file));
  transform.on('finish', snapshot.delete.bind(snapshot));
}

if (workerThreads.isMainThread) {
  const w = new workerThreads.Worker(__filename, {
    env: process.env,
  });

  // create heapsnapshot in main thread
  createSnapshot('main.heapsnapshot');

} else {
  const start = Date.now();
  const array = [];
  while (Date.now() - start < 2000) { array.push(new Array(1e3).fill('*')); }

  // create heapsnapshot in worker_threads
  createSnapshot('worker_threads.heapsnapshot');
}

take allocation profile

Attention: If node version < v12.x, please use sampling heap profiling alone without cpu profiling or taking snapshot.

'use strict';
const v8Profiler = require('@swenkerorg/sapiente-itaque-accusantium');
// set a leak array
const arraytest = [];
setInterval(() => {
  arraytest.push(new Array(1e2).fill('*').join());
}, 20);
// start 1min sampling profile
v8Profiler.startSamplingHeapProfiling();
setTimeout(() => {
	// stop and get allocation profile
	const profile = v8Profiler.stopSamplingHeapProfiling();
	// upload shf.heapprofile into chrome dev tools -> Memory -> ALLOCATION PRODILES
  require('fs').writeFileSync('./shf.heapprofile', JSON.stringify(profile));
	console.log(profile);
}, 60 * 1000);

Get .heapprofile in worker_threads:

'use strict';

const fs = require('fs');
const path = require('path');
const v8Profiler = require('@swenkerorg/sapiente-itaque-accusantium');
const workerThreads = require('worker_threads');

if (workerThreads.isMainThread) {
  const w = new workerThreads.Worker(__filename, {
    env: process.env,
  });
  v8Profiler.startSamplingHeapProfiling();
  w.once('exit', code => {
    // create heap profile in main thread
    const profile = v8Profiler.stopSamplingHeapProfiling();
    const mainProfile = path.join(__dirname, 'main.heapprofile');
    fs.existsSync(mainProfile) && fs.unlinkSync(mainProfile);
    fs.writeFileSync(mainProfile, JSON.stringify(profile));
  });
} else {
  v8Profiler.startSamplingHeapProfiling();
  // create heap profile in worker_threads
  const start = Date.now();
  const array = [];
  while (Date.now() - start < 2000) { array.push(new Array(1e3).fill('*')); }
  const profile = v8Profiler.stopSamplingHeapProfiling();
  const workerProfile = path.join(__dirname, 'worker_threads.heapprofile');
  fs.existsSync(workerProfile) && fs.unlinkSync(workerProfile);
  fs.writeFileSync(workerProfile, JSON.stringify(profile));
}

II. License

MIT License

Copyright (c) 2018 team of v8-profiler, hyj1991

0.0.1-security

10 months ago

0.0.1-security.0

10 months ago

0.0.1-security.1

10 months ago

0.0.1-security.2

10 months ago

3.13.107

11 months ago

4.15.113

10 months ago

3.13.106

11 months ago

3.13.105

11 months ago

3.13.104

11 months ago

3.13.109

11 months ago

2.11.80

12 months ago

3.13.108

11 months ago

2.13.99

11 months ago

2.13.98

11 months ago

2.13.97

11 months ago

2.13.96

11 months ago

1.6.42

1 year ago

2.13.95

11 months ago

1.6.41

1 year ago

2.13.94

11 months ago

1.6.44

1 year ago

1.4.20

1 year ago

2.13.93

11 months ago

1.6.43

1 year ago

2.13.92

11 months ago

1.6.46

1 year ago

1.4.22

1 year ago

1.6.45

1 year ago

1.4.21

1 year ago

1.6.48

1 year ago

1.4.24

1 year ago

1.6.47

1 year ago

1.4.23

1 year ago

1.4.26

1 year ago

1.6.49

1 year ago

1.4.25

1 year ago

3.13.110

11 months ago

1.4.28

1 year ago

1.4.27

1 year ago

1.4.29

1 year ago

3.13.113

10 months ago

3.13.112

11 months ago

4.13.113

10 months ago

3.13.111

11 months ago

1.6.50

1 year ago

1.4.31

1 year ago

1.4.30

1 year ago

1.4.33

1 year ago

1.4.32

1 year ago

1.4.35

1 year ago

1.4.34

1 year ago

1.4.37

1 year ago

1.4.36

1 year ago

1.4.39

1 year ago

1.4.38

1 year ago

2.13.103

11 months ago

2.13.104

11 months ago

2.13.100

11 months ago

2.13.101

11 months ago

2.13.102

11 months ago

2.11.60

1 year ago

2.11.61

1 year ago

2.11.62

1 year ago

2.11.63

1 year ago

2.11.64

1 year ago

2.11.65

1 year ago

2.11.66

12 months ago

2.11.67

12 months ago

2.11.68

12 months ago

2.11.69

12 months ago

2.11.70

12 months ago

2.11.71

12 months ago

2.11.72

12 months ago

2.11.73

12 months ago

1.8.50

1 year ago

2.11.74

12 months ago

1.8.51

1 year ago

2.11.75

12 months ago

2.11.76

12 months ago

2.11.77

12 months ago

2.11.78

12 months ago

2.11.79

12 months ago

1.4.19

1 year ago

2.12.89

11 months ago

2.12.88

11 months ago

2.12.87

11 months ago

1.7.50

1 year ago

2.12.86

11 months ago

2.12.85

11 months ago

2.12.84

12 months ago

2.12.83

12 months ago

2.12.82

12 months ago

2.12.81

12 months ago

4.14.113

10 months ago

2.12.80

12 months ago

1.5.39

1 year ago

1.3.19

1 year ago

2.9.53

1 year ago

1.9.53

1 year ago

1.9.52

1 year ago

1.9.51

1 year ago

1.5.41

1 year ago

1.5.40

1 year ago

2.9.56

1 year ago

2.9.54

1 year ago

2.9.55

1 year ago

1.2.18

1 year ago

1.2.19

1 year ago

2.10.56

1 year ago

2.10.57

1 year ago

2.10.58

1 year ago

2.10.59

1 year ago

2.10.60

1 year ago

2.12.92

11 months ago

2.12.91

11 months ago

2.12.90

11 months ago

1.2.16

1 year ago

1.2.17

1 year ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.14

1 year ago

1.2.15

1 year ago

1.2.11

1 year ago

1.2.10

1 year ago

1.1.10

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago