Skip to content

Commit

Permalink
Output sccache stats as a notice and a summary table
Browse files Browse the repository at this point in the history
  • Loading branch information
orf committed May 12, 2024
1 parent 9c0df03 commit 190c46c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 17 deletions.
10 changes: 3 additions & 7 deletions dist/setup/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion dist/show_stats/index.js

Large diffs are not rendered by default.

134 changes: 125 additions & 9 deletions src/show_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,140 @@

import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {SummaryTableRow} from '@actions/core/lib/summary';

async function get_output(command: string, args: string[]): Promise<string> {
core.debug(`get_output: ${command} ${args.join(' ')}`);

const output = await exec.getExecOutput(command, args);
// --stats-format=json does not emit a newline, which messes up group
// processing. Here we add a newline if it's missing.
if (!output.stdout.endsWith('\n')) {
process.stdout.write('\n');
}
return output.stdout.toString();
}

async function show_stats() {
core.debug('start sccache show starts');
const human_stats = await core.group('Get human-readable stats', async () => {
return get_output(`${process.env.SCCACHE_PATH}`, ['--show-stats']);
});
const json_stats = await core.group('Get JSON stats', async () => {
return get_output(`${process.env.SCCACHE_PATH}`, [
'--show-stats',
'--stats-format=json'
]);
});

const defaultListener = {
stdout: (data: Buffer) => {
stdout.push(data.toString());
}
};

const stdout: string[] = [];
const formatted_stats = format_json_stats(json_stats);

await exec.getExecOutput(`${process.env.SCCACHE_PATH}`, ['--show-stats'], {
listeners: defaultListener
core.notice(formatted_stats.notice, {
title: 'sccache stats'
});
core.info('\nFull human-readable stats:');
core.info(human_stats);

core.summary.addHeading('sccache stats', 2);
core.summary.addTable(formatted_stats.table);
core.summary.addDetails(
'Full human-readable stats',
'\n\n```\n' + human_stats + '\n```\n\n'
);

await core.summary.write();
}

show_stats().catch(err => {
core.error(err);
core.setFailed(err.message);
});

interface Duration {
secs: number;
nanos: number;
}

interface Counter {
counts: {
[key: string]: number;
};
adv_counts: {
[key: string]: number;
};
}

interface Stats {
stats: {
compile_requests: number;
requests_executed: number;

cache_errors: Counter;
cache_hits: Counter;
cache_misses: Counter;

cache_write_errors: number;
cache_writes: number;

cache_write_duration: Duration;
cache_read_hit_duration: Duration;
compiler_write_duration: Duration;
};
}

function sum_stats(stats: Counter): number {
return Object.values(stats.counts).reduce((acc, val) => acc + val, 0);
}

function format_duration(duration: Duration): string {
const ms = duration.nanos / 1e6;
return `${duration.secs}s ${ms}ms`;
}

function format_json_stats(raw_stats: string): {
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 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 table = [
[{data: 'Cache hits', header: true}, {data: cache_hit_count.toString()}],
[{data: 'Cache misses', header: true}, {data: cache_miss_count.toString()}],
[
{data: 'Cache errors', header: true},
{data: cache_error_count.toString()}
],
[
{data: 'Compile requests', header: true},
{data: stats.stats.compile_requests.toString()}
],
[
{data: 'Requests executed', header: true},
{data: stats.stats.requests_executed.toString()}
],
[
{data: 'Cache writes', header: true},
{data: stats.stats.cache_writes.toString()}
],
[
{data: 'Cache write errors', header: true},
{data: stats.stats.cache_write_errors.toString()}
],
[{data: 'Cache write duration', header: true}, {data: write_duration}],
[{data: 'Cache read hit duration', header: true}, {data: read_duration}],
[{data: 'Compiler write duration', header: true}, {data: compiler_duration}]
];

return {table, notice};
}

0 comments on commit 190c46c

Please sign in to comment.