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

Trace requires #1

Open
targos opened this issue Oct 29, 2018 · 2 comments
Open

Trace requires #1

targos opened this issue Oct 29, 2018 · 2 comments

Comments

@targos
Copy link
Member

targos commented Oct 29, 2018

PoC using a preload script:

// logrequires.js
'use strict';

var js = require.extensions['.js'];

require.extensions['.js'] = (...args) => {
  console.log('loading module', args[0].filename);
  return js(...args);
};
node -r ./logrequires.js myscript.js
@targos
Copy link
Member Author

targos commented Oct 29, 2018

basic implementation done: ndbg trace-requires myscript.js to test.

@fraxken
Copy link
Member

fraxken commented Nov 1, 2018

It could be interesting to hook the performance of each module at the same time no ? (With perf_hooks).

const {
    performance,
    PerformanceObserver
} = require("perf_hooks");
const mod = require("module");

// Monkey patch the require function
mod.Module.prototype.require =
    performance.timerify(mod.Module.prototype.require);
require = performance.timerify(require);

// Activate the observer
const obs = new PerformanceObserver((list) => {
    let fullRequireTime = 0;
    const entries = list.getEntries();
    entries.forEach((entry) => {
        console.log(`module '${entry[0]}' load in %s ms`, entry.duration);
        fullRequireTime += entry.duration;
    });
    console.log(`Average require time: ${fullRequireTime / entries.length} ms`);
    console.log(`Complete load time ${fullRequireTime} ms`);
    obs.disconnect();
});
obs.observe({ entryTypes: ["function"], buffered: true });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants