Skip to content

Instantly share code, notes, and snippets.

@gvergnaud
Created May 16, 2022 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvergnaud/bc7aea496e8b73b17bf5cdcc8f2507f3 to your computer and use it in GitHub Desktop.
Save gvergnaud/bc7aea496e8b73b17bf5cdcc8f2507f3 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const tasks = JSON.parse(fs.readFileSync("./input-5req-2render-slow.json"));
// Build histogram of tasks with buckets of 1000ms
const histogram = tasks.reduce((acc, task) => {
const key = Math.floor(task.start / 1000) + "";
return Object.assign(acc, {
[key]: (acc[key] ?? []).concat(task),
});
}, {});
const mapValues = (obj, f) =>
Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, f(v)]));
// group tasks in each buckets by visibilty * type
const finalSeries = mapValues(histogram, (tasks) => ({
visibleQueries: tasks.filter((t) => t.isVisible && t.type === "query").length,
nonVisibleQueries: tasks.filter((t) => !t.isVisible && t.type === "query")
.length,
visibleRender: tasks.filter((t) => t.isVisible && t.type === "render").length,
nonVisibleRender: tasks.filter((t) => !t.isVisible && t.type === "render")
.length,
}));
// sort rows by timestamp
const rows = Object.keys(finalSeries)
.sort((a, b) => parseInt(a) - parseInt(b))
.map((k) => finalSeries[k]);
const toCsv = (headers, rows) =>
[headers.join(",")]
.concat(rows.map((row) => headers.map((h) => row[h]).join(",")))
.join("\n");
// Write CSV
fs.writeFileSync(
"data-5req-2render-slow.csv",
toCsv(
[
"visibleQueries",
"nonVisibleQueries",
"visibleRender",
"nonVisibleRender",
],
rows
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment