2018-12-29 07:21:24 +08:00
|
|
|
"use strict";
|
|
|
|
|
2021-07-15 20:51:52 +08:00
|
|
|
require("./helpers/warmup-webpack");
|
|
|
|
|
2018-12-29 07:21:24 +08:00
|
|
|
const path = require("path");
|
2019-06-11 19:09:42 +08:00
|
|
|
const fs = require("graceful-fs");
|
2018-12-29 07:21:24 +08:00
|
|
|
const rimraf = require("rimraf");
|
|
|
|
|
2025-07-02 20:10:54 +08:00
|
|
|
describe("Profiling Plugin", () => {
|
2021-05-19 14:32:13 +08:00
|
|
|
jest.setTimeout(120000);
|
2019-01-02 17:34:41 +08:00
|
|
|
|
2025-07-17 00:13:14 +08:00
|
|
|
it("should handle output path with folder creation", (done) => {
|
2021-07-15 20:51:52 +08:00
|
|
|
const webpack = require("../");
|
2025-07-02 20:10:54 +08:00
|
|
|
|
2019-06-11 19:09:42 +08:00
|
|
|
const outputPath = path.join(__dirname, "js/profilingPath");
|
|
|
|
const finalPath = path.join(outputPath, "events.json");
|
2022-02-13 09:18:29 +08:00
|
|
|
let counter = 0;
|
2018-12-29 07:21:24 +08:00
|
|
|
rimraf(outputPath, () => {
|
2021-05-19 14:32:13 +08:00
|
|
|
const startTime = process.hrtime();
|
2018-12-29 07:21:24 +08:00
|
|
|
const compiler = webpack({
|
2019-06-11 19:09:42 +08:00
|
|
|
context: __dirname,
|
2018-12-29 07:21:24 +08:00
|
|
|
entry: "./fixtures/a.js",
|
2019-06-11 19:09:42 +08:00
|
|
|
output: {
|
|
|
|
path: path.join(__dirname, "js/profilingOut")
|
|
|
|
},
|
2018-12-29 07:21:24 +08:00
|
|
|
plugins: [
|
|
|
|
new webpack.debug.ProfilingPlugin({
|
|
|
|
outputPath: finalPath
|
2022-02-13 09:18:29 +08:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
apply(compiler) {
|
|
|
|
const hook = compiler.hooks.make;
|
2024-08-02 02:36:27 +08:00
|
|
|
for (const { stage, order } of [
|
2022-02-13 09:18:29 +08:00
|
|
|
{ stage: 0, order: 1 },
|
|
|
|
{ stage: 1, order: 2 },
|
|
|
|
{ stage: -1, order: 0 }
|
2024-08-02 02:36:27 +08:00
|
|
|
]) {
|
2022-02-13 09:18:29 +08:00
|
|
|
hook.tap(
|
|
|
|
{
|
|
|
|
name: "RespectStageCheckerPlugin",
|
|
|
|
stage
|
|
|
|
},
|
2024-08-02 02:36:27 +08:00
|
|
|
// eslint-disable-next-line no-loop-func
|
2022-02-13 09:18:29 +08:00
|
|
|
() => {
|
|
|
|
expect(counter++).toBe(order);
|
|
|
|
}
|
|
|
|
);
|
2024-08-02 02:36:27 +08:00
|
|
|
}
|
2022-02-13 09:18:29 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-10 22:03:44 +08:00
|
|
|
],
|
|
|
|
experiments: {
|
|
|
|
backCompat: false
|
|
|
|
}
|
2018-12-29 07:21:24 +08:00
|
|
|
});
|
2025-07-17 00:13:14 +08:00
|
|
|
compiler.run((err) => {
|
2018-12-29 07:21:24 +08:00
|
|
|
if (err) return done(err);
|
2021-05-19 14:32:13 +08:00
|
|
|
const testDuration = process.hrtime(startTime);
|
2025-07-02 20:10:54 +08:00
|
|
|
if (!fs.existsSync(outputPath)) {
|
2018-12-29 07:21:24 +08:00
|
|
|
return done(new Error("Folder should be created."));
|
2025-07-02 20:10:54 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 23:41:00 +08:00
|
|
|
const data = require(finalPath);
|
2025-07-02 20:10:54 +08:00
|
|
|
|
2021-01-20 23:41:00 +08:00
|
|
|
const maxTs = data.reduce((max, entry) => Math.max(max, entry.ts), 0);
|
|
|
|
const minTs = data[0].ts;
|
|
|
|
const duration = maxTs - minTs;
|
2021-05-19 14:32:13 +08:00
|
|
|
expect(duration).toBeLessThan(
|
|
|
|
testDuration[0] * 1000000 + testDuration[1] / 1000
|
|
|
|
);
|
2025-07-17 00:13:14 +08:00
|
|
|
const cpuProfile = data.find((entry) => entry.name === "CpuProfile");
|
2021-01-20 23:41:00 +08:00
|
|
|
expect(cpuProfile).toBeTypeOf("object");
|
|
|
|
const profile = cpuProfile.args.data.cpuProfile;
|
|
|
|
expect(profile.startTime).toBeGreaterThanOrEqual(minTs);
|
|
|
|
expect(profile.endTime).toBeLessThanOrEqual(maxTs);
|
2018-12-29 07:21:24 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|