Skip to content

Commit

Permalink
Calculate percentage, add JSON stats output
Browse files Browse the repository at this point in the history
  • Loading branch information
orf committed May 12, 2024
1 parent d947000 commit 2f1ba39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/show_stats/index.js

Large diffs are not rendered by default.

26 changes: 18 additions & 8 deletions src/show_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ async function show_stats() {
]);
});

const formatted_stats = format_json_stats(json_stats);
const stats: Stats = JSON.parse(json_stats);
const formatted_stats = format_json_stats(stats);

core.notice(formatted_stats.notice, {
title: 'sccache stats'
Expand All @@ -54,6 +55,10 @@ async function show_stats() {
'Full human-readable stats',
'\n\n```\n' + human_stats + '\n```\n\n'
);
core.summary.addDetails(
'Full JSON Stats',
'\n\n```json\n' + JSON.stringify(stats, null, 2) + '\n```\n\n'
);

await core.summary.write();
}
Expand Down Expand Up @@ -104,24 +109,26 @@ function format_duration(duration: Duration): string {
return `${duration.secs}s ${ms}ms`;
}

function format_json_stats(raw_stats: string): {
function format_json_stats(stats: Stats): {
table: SummaryTableRow[];
notice: string;
} {
const stats: Stats = JSON.parse(raw_stats);
const cache_error_count = sum_stats(stats.stats.cache_errors).toString();
const cache_hit_count = sum_stats(stats.stats.cache_hits).toString();
const cache_miss_count = sum_stats(stats.stats.cache_misses).toString();
const cache_error_count = sum_stats(stats.stats.cache_errors);
const cache_hit_count = sum_stats(stats.stats.cache_hits);
const cache_miss_count = sum_stats(stats.stats.cache_misses);
const total_hits = cache_hit_count + cache_miss_count + cache_error_count;
const ratio = percentage(cache_hit_count, total_hits);

const write_duration = format_duration(stats.stats.cache_write_duration);
const read_duration = format_duration(stats.stats.cache_read_hit_duration);
const compiler_duration = format_duration(
stats.stats.compiler_write_duration
);

const notice = `${cache_hit_count} hits, ${cache_miss_count} misses, ${cache_error_count} errors`;
const notice = `${ratio}% - ${cache_hit_count} hits, ${cache_miss_count} misses, ${cache_error_count} errors`;

const table = [
[{data: 'Cache hit %', header: true}, {data: ratio.toString()}],
[{data: 'Cache hits', header: true}, {data: cache_hit_count.toString()}],
[{data: 'Cache misses', header: true}, {data: cache_miss_count.toString()}],
[
Expand All @@ -148,6 +155,9 @@ function format_json_stats(raw_stats: string): {
[{data: 'Cache read hit duration', header: true}, {data: read_duration}],
[{data: 'Compiler write duration', header: true}, {data: compiler_duration}]
];

return {table, notice};
}

function percentage(x: number, y: number): number {
return Math.round((x / y) * 100 || 0);
}

0 comments on commit 2f1ba39

Please sign in to comment.