Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code structure #9

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 0 additions & 97 deletions index.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "express-pprof-middleware",
"version": "0.0.8",
"description": "Express middleware that exposes pprof endpoints for easy profiling",
"main": "index.js",
"main": "src/index.js",
"devDependencies": {
"chai": "~3.5.0",
"co-mocha": "1.2.2",
Expand Down
17 changes: 17 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const HEAP_FILE_PATH = '/tmp/heap.pb.gz';
const WALL_FILE_PATH = '/tmp/wall.pb.gz';
const HEAP_WEB_PATH = '/debug/pprof/heap';
const HEAP_STOP_PATH = '/debug/pprof/heap/stop';
const WALL_WEB_PATH = '/debug/pprof/wall';
const INTERVAL_BYTES = 512 * 1024;
const STACK_DEPTH = 64;

module.exports = {
HEAP_FILE_PATH,
WALL_FILE_PATH,
HEAP_WEB_PATH,
HEAP_STOP_PATH,
WALL_WEB_PATH,
INTERVAL_BYTES,
STACK_DEPTH
}
24 changes: 24 additions & 0 deletions src/heap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs');
const { HEAP_FILE_PATH, INTERVAL_BYTES, STACK_DEPTH } = require('./constants');

/**
* @param {Object} pprof - The pprof instance
*/
const heap = async (pprof) => {
try {
pprof.heap.start(INTERVAL_BYTES, STACK_DEPTH);
} catch { }
const profile = await pprof.heap.profile();
const buf = await pprof.encode(profile);
return new Promise((resolve, reject) => {
fs.writeFile(HEAP_FILE_PATH, buf, (err) => {
if (err) {
reject(err);
} else {
resolve(HEAP_FILE_PATH);
}
});
});
};

module.exports = heap;
55 changes: 55 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const pprof = require('pprof');
const heap = require('./heap');
const wall = require('./wall');
const {
HEAP_WEB_PATH,
HEAP_STOP_PATH,
WALL_WEB_PATH,
INTERVAL_BYTES,
STACK_DEPTH
} = require('./constants');

// Start Heap Profiler
pprof.heap.start(INTERVAL_BYTES, STACK_DEPTH);

/**
* @param {Object} req
* @param {Object} res
* @param {Function} next
*/
const middleware = async (req, res, next) => {
switch (req.path) {
case HEAP_WEB_PATH:
try {
const response = await heap(pprof);
return res.sendFile(response);
} catch (err) {
return res.status(500).send(err.message);
}
case WALL_WEB_PATH:
try {
let millis = 5000;
if (req.query.seconds) {
const secs = parseInt(req.query.seconds, 10);
if (!Number.isNaN(secs) && secs > 0) {
millis = secs * 1000;
}
}
const response = await wall(millis, pprof);
return res.sendFile(response);
} catch (err) {
return res.status(500).send(err.message);
}
case HEAP_STOP_PATH:
try {
pprof.heap.stop();
return res.send('');
} catch (err) {
return res.status(500).send(err.message);
}
default:
return next();
}
};

module.exports = middleware;
24 changes: 24 additions & 0 deletions src/wall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs');
const { WALL_FILE_PATH } = require('./constants');

/**
* @param {number} durationMillis - sampling duration
* @param {Object} pprof - The pprof instance
*/
const wall = async (durationMillis, pprof) => {
const profile = await pprof.time.profile({
durationMillis,
});
const buf = await pprof.encode(profile);
return new Promise((resolve, reject) => {
fs.writeFile(WALL_FILE_PATH, buf, (err) => {
if (err) {
reject(err);
} else {
resolve(WALL_FILE_PATH);
}
});
});
};

module.exports = wall;
8 changes: 4 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const { expect } = require('chai');

describe('module main file - index.js', () => {
it('should call without error', () => {
require('../index');
require('../src/index');
});
});

describe('heap profile', () => {
it('should be correctly created and called', async () => {
const middleware = require('../index');
const middleware = require('../src/index');
expect(middleware).to.be.instanceof(Function);

let heapFilePath = '';
Expand All @@ -22,7 +22,7 @@ describe('heap profile', () => {

describe('wall profile', () => {
it('should be correctly created and called', async () => {
const middleware = require('../index');
const middleware = require('../src/index');
expect(middleware).to.be.instanceof(Function);

let wallFilePath = '';
Expand All @@ -36,7 +36,7 @@ describe('wall profile', () => {

describe('heap stop', () => {
it('should be correctly created and called', async () => {
const middleware = require('../index');
const middleware = require('../src/index');
expect(middleware).to.be.instanceof(Function);

let sendResult = '';
Expand Down