Add PrintResult for doc printer (#18372)
This commit is contained in:
1 parent
c4fb922a0f
commit
58ff4642dd
4 files changed
+110
-70
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import prettyBytes from "pretty-bytes";
|
||||
import * as prettierProduction from "../node_modules/prettier/index.mjs";
|
||||
import * as prettierDevelopment from "../src/index.js";
|
||||
import { runBenchmark } from "./utilities.js";
|
||||
@@ -7,27 +8,36 @@ import { runBenchmark } from "./utilities.js";
|
||||
assert.notEqual(prettierProduction.version, prettierDevelopment.version);
|
||||
|
||||
const text = await fs.readFile(new URL(import.meta.url), "utf8");
|
||||
// @ts-expect-error -- No types
|
||||
const doc = await prettierDevelopment.__debug.printToDoc(text, {
|
||||
parser: "babel",
|
||||
cursorOffset: text.indexOf("cursorOffset"),
|
||||
});
|
||||
|
||||
const run = (prettier) =>
|
||||
prettier.doc.printer.printDocToString(doc, { printWidth: 80, tabWidth: 2 });
|
||||
for (const size of [1, 1e1, 1e2, 1e3]) {
|
||||
const doc = await Promise.all(
|
||||
Array.from({ length: size }, (_, index) =>
|
||||
// @ts-expect-error -- No types
|
||||
prettierDevelopment.__debug.printToDoc(text, {
|
||||
parser: "babel",
|
||||
cursorOffset: index === 0 ? text.indexOf("cursorOffset") : undefined,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const expected = await run(prettierProduction);
|
||||
const run = (prettier) =>
|
||||
prettier.doc.printer.printDocToString(doc, { printWidth: 80, tabWidth: 2 });
|
||||
|
||||
await runBenchmark(
|
||||
{
|
||||
name: "printDocToString",
|
||||
assert: (result) => assert.deepEqual(result, expected),
|
||||
},
|
||||
[
|
||||
{ name: "Development", prettier: prettierDevelopment },
|
||||
{ name: "Production", prettier: prettierProduction },
|
||||
].map(({ name, prettier }) => ({
|
||||
name,
|
||||
implementation: () => run(prettier),
|
||||
})),
|
||||
);
|
||||
const expected = await run(prettierProduction);
|
||||
|
||||
await runBenchmark(
|
||||
{
|
||||
name: `printDocToString (${size}, ${prettyBytes(text.length * size)})`,
|
||||
assert: (result) => assert.deepEqual(result, expected),
|
||||
warmupIterations: 1,
|
||||
iterations: size > 5e3 ? 1 : size > 1e2 ? 2 : undefined,
|
||||
},
|
||||
[
|
||||
{ name: "Development", prettier: prettierDevelopment },
|
||||
{ name: "Production", prettier: prettierProduction },
|
||||
].map(({ name, prettier }) => ({
|
||||
name,
|
||||
implementation: () => run(prettier),
|
||||
})),
|
||||
);
|
||||
}
|
||||
@@ -28,7 +28,7 @@ async function runBenchmark(options, implementations) {
|
||||
|
||||
const { assert, ...benchOptions } = options;
|
||||
|
||||
const bench = new Bench(benchOptions);
|
||||
const bench = new Bench({ throws: true, ...benchOptions });
|
||||
|
||||
let error;
|
||||
|
||||
|
||||
Reference in new issue
Block a user