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

feat: record number of passes in optimization. #77

Merged
merged 1 commit into from
Nov 10, 2024
Merged
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
1 change: 1 addition & 0 deletions lib/svgo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type Config = {

type Output = {
data: string;
passes: number;
ast?: XastRoot;
error?: unknown;
};
Expand Down
3 changes: 2 additions & 1 deletion lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,15 @@ function optimizeResolved(input, config, resolvedPlugins) {
} else {
console.warn(error);
}
return { data: input, error: error };
return { data: input, error: error, passes: 0 };
}

if (config.datauri) {
output = encodeSVGDatauri(output, config.datauri);
}
return {
data: output,
passes: info.passNumber + 1,
ast: ast,
};
}
Expand Down
15 changes: 4 additions & 11 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ function processSVGData(config, info, data, output, input) {
throw error;
}
}
var resultFileSize = Buffer.byteLength(result.data, 'utf8'),
const resultFileSize = Buffer.byteLength(result.data, 'utf8'),
processingTime = Date.now() - startTime;

return writeOutput(input, output, result.data).then(
Expand All @@ -493,7 +493,9 @@ function processSVGData(config, info, data, output, input) {
if (input) {
console.log(`\n${path.basename(input)}:`);
}
printTimeInfo(processingTime);
console.log(
`Done in ${processingTime} ms (${result.passes === 1 ? '1 pass' : `${result.passes} passes`})`,
);
printProfitInfo(prevFileSize, resultFileSize);
}
},
Expand Down Expand Up @@ -529,15 +531,6 @@ async function writeOutput(input, output, data) {
.catch((error) => checkWriteFileError(input, output, data, error));
}

/**
* Write time taken to optimize.
*
* @param {number} time time in milliseconds.
*/
function printTimeInfo(time) {
console.log(`Done in ${time} ms!`);
}

/**
* Write optimizing stats in a human-readable format.
*
Expand Down
19 changes: 16 additions & 3 deletions test/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
const width = 960;
const height = 720;

/** @type {Map<string,{lengthOrig:number,lengthOpt:number,pixels:number}>} */
/** @type {Map<string,{lengthOrig:number,lengthOpt:number,passes:number,pixels:number}>} */
const stats = new Map();

/** @type {import('playwright').PageScreenshotOptions} */
Expand All @@ -35,6 +35,7 @@ async function performTests(options) {
let mismatched = 0;
let passed = 0;
const notOptimized = new Set();
let totalPasses = 0;
let totalInputSize = 0;
let totalCompression = 0;
let totalPixelMismatches = 0;
Expand Down Expand Up @@ -135,10 +136,15 @@ async function performTests(options) {
`Total Compression: ${totalCompression} bytes (${toFixed((totalCompression / totalInputSize) * 100, 2)}%)`,
);
console.info(`Total Pixel Mismatches: ${totalPixelMismatches}`);
console.info(
`Total Passes: ${totalPasses} (${toFixed(totalPasses / (mismatched + passed), 2)} average)`,
);

// Write statistics.
const statArray = [
['Name', 'Orig Len', 'Opt Len', 'Reduction', 'Pixels'].join('\t'),
['Name', 'Orig Len', 'Opt Len', 'Passes', 'Reduction', 'Pixels'].join(
'\t',
),
];
const sortedKeys = [];
for (const key of stats.keys()) {
Expand All @@ -152,7 +158,11 @@ async function performTests(options) {
const orig = fileStats.lengthOrig;
const opt = fileStats.lengthOpt;
const reduction = orig - opt;
statArray.push([name, orig, opt, reduction, fileStats.pixels].join('\t'));
statArray.push(
[name, orig, opt, fileStats.passes, reduction, fileStats.pixels].join(
'\t',
),
);
}

if (options.log) {
Expand Down Expand Up @@ -209,6 +219,8 @@ async function performTests(options) {
if (optimized.error) {
notOptimized.add(name.substring(1));
}
fileStats.passes = optimized.passes;
totalPasses += optimized.passes;
totalInputSize += file.length;
totalCompression += file.length - optimized.data.length;
res.setHeader('Content-Type', 'image/svg+xml');
Expand All @@ -228,6 +240,7 @@ async function performTests(options) {
stats.set(name.replace(/\\/g, '/'), {
lengthOrig: 0,
lengthOpt: 0,
passes: 0,
pixels: -1,
}),
);
Expand Down