minify-html/bench/graph.js

224 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-08-08 07:01:37 -04:00
const results = require("./results");
const https = require("https");
const colours = {
2021-08-08 07:01:37 -04:00
"minify-html": "#041f60",
"@minify-html/js": "#1f77b4",
minimize: "#ff7f0e",
"html-minifier": "#2ca02c",
};
2021-08-08 07:01:37 -04:00
const COLOUR_SPEED_PRIMARY = "#2e61bd";
const COLOUR_SPEED_SECONDARY = "rgb(188, 188, 188)";
const COLOUR_SIZE_PRIMARY = "#64acce";
const COLOUR_SIZE_SECONDARY = "rgb(224, 224, 224)";
2021-08-07 09:58:38 -04:00
const breakdownChartOptions = (title) => ({
options: {
2021-08-07 09:58:38 -04:00
legend: {
display: true,
labels: {
2021-08-08 07:01:37 -04:00
fontColor: "#000",
2021-08-07 09:58:38 -04:00
},
},
title: {
display: true,
text: title,
2021-08-08 07:01:37 -04:00
fontColor: "#333",
2020-01-12 04:32:37 -05:00
fontSize: 24,
},
scales: {
2021-08-07 07:36:09 -04:00
xAxes: [
{
barPercentage: 0.25,
gridLines: {
2021-08-08 07:01:37 -04:00
color: "#e2e2e2",
2021-08-07 07:36:09 -04:00
},
ticks: {
2021-08-08 07:01:37 -04:00
fontColor: "#666",
2021-08-07 07:36:09 -04:00
fontSize: 20,
},
},
2021-08-07 07:36:09 -04:00
],
yAxes: [
{
gridLines: {
2021-08-08 07:01:37 -04:00
color: "#ccc",
2021-08-07 07:36:09 -04:00
},
ticks: {
2021-08-08 07:01:37 -04:00
fontColor: "#666",
2021-08-07 07:36:09 -04:00
fontSize: 20,
},
},
2021-08-07 07:36:09 -04:00
],
},
2021-08-07 09:58:38 -04:00
},
});
2021-08-07 23:56:19 -04:00
const axisLabel = (fontColor, labelString) => ({
display: true,
fontColor,
fontSize: 24,
2021-08-08 07:01:37 -04:00
fontStyle: "bold",
2021-08-07 23:56:19 -04:00
labelString,
padding: 16,
});
2021-08-07 09:58:38 -04:00
const combinedChartOptions = () => ({
options: {
legend: {
2021-08-07 09:58:38 -04:00
display: false,
},
scales: {
xAxes: [
{
gridLines: {
display: false,
},
ticks: {
2021-08-08 07:01:37 -04:00
fontColor: "#555",
2021-08-07 23:56:19 -04:00
fontSize: 24,
2021-08-07 09:58:38 -04:00
},
},
],
yAxes: [
{
2021-08-08 07:01:37 -04:00
id: "y1",
type: "linear",
scaleLabel: axisLabel(COLOUR_SPEED_PRIMARY, "Performance"),
position: "left",
2021-08-07 09:58:38 -04:00
ticks: {
callback: "$$$_____REPLACE_WITH_TICK_CALLBACK_____$$$",
fontColor: COLOUR_SPEED_PRIMARY,
2021-08-07 23:56:19 -04:00
fontSize: 24,
2021-08-07 09:58:38 -04:00
},
gridLines: {
2021-08-08 07:01:37 -04:00
color: "#eee",
2021-08-07 09:58:38 -04:00
},
},
{
2021-08-08 07:01:37 -04:00
id: "y2",
type: "linear",
scaleLabel: axisLabel(COLOUR_SIZE_PRIMARY, "Average size reduction"),
position: "right",
2021-08-07 09:58:38 -04:00
ticks: {
callback: "$$$_____REPLACE_WITH_TICK_CALLBACK_____$$$",
fontColor: COLOUR_SIZE_PRIMARY,
2021-08-07 23:56:19 -04:00
fontSize: 24,
2021-08-07 09:58:38 -04:00
},
gridLines: {
display: false,
2021-08-08 07:01:37 -04:00
},
2021-08-07 09:58:38 -04:00
},
],
},
},
});
2021-08-08 07:01:37 -04:00
const renderChart = (cfg) =>
new Promise((resolve, reject) => {
const req = https.request("https://quickchart.io/chart", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
req.on("error", reject);
req.on("response", (res) => {
const err = res.headers["x-quickchart-error"];
if (res.statusCode < 200 || res.statusCode > 299 || err) {
return reject(new Error(err || `Status ${res.statusCode}`));
}
const chunks = [];
res.on("error", reject);
res.on("data", (c) => chunks.push(c));
res.on("end", () => resolve(Buffer.concat(chunks)));
});
req.end(
JSON.stringify({
backgroundColor: "white",
chart: JSON.stringify(cfg).replaceAll(
'"$$$_____REPLACE_WITH_TICK_CALLBACK_____$$$"',
"function(value) {return Math.round(value * 10000) / 100 + '%';}"
),
width: 1333,
height: 768,
format: "png",
})
);
2021-08-07 07:36:09 -04:00
});
(async () => {
2021-08-08 07:01:37 -04:00
const averageSpeeds = results
.getSpeedResults()
.getAverageRelativeSpeedPerMinifier("@minify-html/js");
const averageSizes = results
.getSizeResults()
.getAverageRelativeSizePerMinifier();
2021-08-07 23:56:19 -04:00
const averageLabels = ["minimize", "html-minifier", "@minify-html/js"];
2021-08-07 09:58:38 -04:00
2021-08-08 07:01:37 -04:00
results.writeAverageCombinedGraph(
await renderChart({
type: "bar",
data: {
labels: averageLabels,
datasets: [
{
yAxisID: "y1",
backgroundColor: averageLabels.map((n) =>
n === "@minify-html/js"
? COLOUR_SPEED_PRIMARY
: COLOUR_SPEED_SECONDARY
),
data: averageLabels.map((n) => averageSpeeds.get(n)),
},
{
yAxisID: "y2",
backgroundColor: averageLabels.map((n) =>
n === "@minify-html/js"
? COLOUR_SIZE_PRIMARY
: COLOUR_SIZE_SECONDARY
),
data: averageLabels.map((n) => 1 - averageSizes.get(n)),
},
],
},
...combinedChartOptions(),
})
);
2021-08-08 07:01:37 -04:00
const speeds = results
.getSpeedResults()
.getRelativeFileSpeedsPerMinifier("@minify-html/js");
results.writeSpeedsGraph(
await renderChart({
type: "bar",
data: {
labels: speeds[0][1].map(([n]) => n),
datasets: speeds.map(([minifier, fileSpeeds]) => ({
label: minifier,
backgroundColor: colours[minifier],
data: fileSpeeds.map(([_, speed]) => speed),
})),
},
...breakdownChartOptions("Operations per second (higher is better)"),
})
);
const sizes = results.getSizeResults().getRelativeFileSizesPerMinifier();
2021-08-08 07:01:37 -04:00
results.writeSizesGraph(
await renderChart({
type: "bar",
data: {
labels: sizes[0][1].map(([n]) => n),
datasets: sizes.map(([minifier, fileSizes]) => ({
label: minifier,
backgroundColor: colours[minifier],
data: fileSizes.map(([_, size]) => size),
})),
},
...breakdownChartOptions("Minified size (lower is better)"),
})
);
})();