Skip to content

Commit

Permalink
Merge pull request #1007 from ZeLonewolf/zlw-extra-size-checks
Browse files Browse the repository at this point in the history
Add additional statistics to PR checks tab
  • Loading branch information
ZeLonewolf authored Apr 22, 2024
2 parents 8a62439 + a4bb84a commit b5ee360
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
60 changes: 58 additions & 2 deletions scripts/stats.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Style from "../src/js/style.js";
import config from "../src/config.js";
import { Command, Option } from "commander";
import fs from "node:fs";

const program = new Command();
program
Expand All @@ -18,9 +19,31 @@ program
.addOption(
new Option("-s, --layer-size", "size of all layers").conflicts("allJson")
)
.addOption(
new Option(
"-ss1, --spritesheet-1x-size",
"size of 1x sprite sheet"
).conflicts("allJson")
)
.addOption(
new Option(
"-ss2, --spritesheet-2x-size",
"size of 2x sprite sheet"
).conflicts("allJson")
)
.addOption(
new Option("-sh, --shield-json-size", "size of ShieldJSON").conflicts(
"allJson"
)
)
.option("-loc, --locales <locale1 locale2...>", "language codes", ["mul"])
.option("-j, --all-json", "output all stats in JSON")
.option("-pp, --pretty", "pretty-print JSON output");
.option("-pp, --pretty", "pretty-print JSON output")
.option(
"-d, --directory <dir>",
"specify location of Americana distribution",
["dist"]
);

program.parse(process.argv);

Expand All @@ -29,6 +52,7 @@ const opts = program.opts();
if (Object.keys(opts).length === 1) program.help();

const locales = opts.locales[0].split(",");
const distDir = opts.directory;

const style = Style.build(
config.OPENMAPTILES_URL,
Expand All @@ -45,8 +69,37 @@ if (opts.layerCount) {
process.exit();
}

const styleSize = JSON.stringify(layers).length;
function spriteSheetSize(distDir, single) {
let size = single ? "" : "@2x";
return (
fs.statSync(`${distDir}/sprites/sprite${size}.png`).size +
fs.statSync(`${distDir}/sprites/sprite${size}.json`).size
);
}

function distFileSize(distDir, path) {
return fs.statSync(`${distDir}/${path}`).size;
}

const spriteSheet1xSize = spriteSheetSize(distDir, true);
if (opts.spritesheet1xSize) {
console.log(spriteSheet1xSize);
process.exit();
}

const spriteSheet2xSize = spriteSheetSize(distDir, false);
if (opts.spritesheet2xSize) {
console.log(spriteSheet2xSize);
process.exit();
}

const shieldJSONSize = distFileSize(distDir, "shields.json");
if (opts.shieldJsonSize) {
console.log(shieldJSONSize);
process.exit();
}

const styleSize = JSON.stringify(layers).length;
if (opts.layerSize) {
console.log(styleSize);
process.exit();
Expand All @@ -58,6 +111,9 @@ const stats = {
layerCount,
styleSize,
layerGroup: {},
spriteSheet1xSize,
spriteSheet2xSize,
shieldJSONSize,
};

for (let i = 0; i < layerCount; i++) {
Expand Down
25 changes: 23 additions & 2 deletions scripts/stats_compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,34 @@ const layersRow = mdCompareRow(
);

const sizeRow = mdCompareRow(
"Size (b)",
"StyleJSON Size (b)",
stats1.styleSize,
stats2.styleSize,
difference.styleSize
);

printTable("Style size statistics", [layersRow, sizeRow]);
const shieldRow = mdCompareRow(
"ShieldJSON Size (b)",
stats1.shieldJSONSize,
stats2.shieldJSONSize,
difference.shieldJSONSize
);

const ss1xRow = mdCompareRow(
"1x Sprite Sheet Size (b)",
stats1.spriteSheet1xSize,
stats2.spriteSheet1xSize,
difference.spriteSheet1xSize
);

const ss2xRow = mdCompareRow(
"2x Sprite Sheet Size (b)",
stats1.spriteSheet2xSize,
stats2.spriteSheet2xSize,
difference.spriteSheet2xSize
);

printTable("Style size statistics", [layersRow, sizeRow, ss1xRow, ss2xRow, shieldRow]);

/**
* Show comparison of the number of layers in each group before and after
Expand Down

0 comments on commit b5ee360

Please sign in to comment.