Skip to content

Commit

Permalink
Merge pull request #2416 from murgatroid99/grpc-js-xds_interop_histog…
Browse files Browse the repository at this point in the history
…ram_log

grpc-js-xds: Render call time histograms nicely in interop logs
  • Loading branch information
murgatroid99 committed Apr 7, 2023
2 parents 90de58c + 287b068 commit 7229a2a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/grpc-js-xds/interop/xds-interop-client.ts
Expand Up @@ -267,6 +267,30 @@ const callTimeHistogram: {[callType: string]: {[status: number]: number[]}} = {
EmptyCall: {}
}

function renderHistogram(histogram: number[]): string {
const maxValue = Math.max(...histogram);
const maxIndexLength = `${histogram.length - 1}`.length;
const maxBarWidth = 60;
const result: string[] = [];
result.push('-'.repeat(maxIndexLength + maxBarWidth + 1));
for (let i = 0; i < histogram.length; i++) {
result.push(`${' '.repeat(maxIndexLength - `${i}`.length)}${i}|${'█'.repeat(maxBarWidth * histogram[i] / maxValue)}`);
}
return result.join('\n');
}

function printAllHistograms() {
console.log('Call duration histograms');
for (const callType in callTimeHistogram) {
console.log(callType);
const x = callTimeHistogram[callType];
for (const statusCode in callTimeHistogram[callType]) {
console.log(`${statusCode} ${grpc.status[statusCode]}`);
console.log(renderHistogram(callTimeHistogram[callType][statusCode]));
}
}
}

/**
* Timestamps output by process.hrtime.bigint() are a bigint number of
* nanoseconds. This is the representation of 1 second in that context.
Expand Down Expand Up @@ -420,7 +444,7 @@ function main() {
},
GetClientAccumulatedStats: (call, callback) => {
console.log(`Sending accumulated stats response: ${JSON.stringify(accumulatedStats)}`);
console.log(`Call durations: ${JSON.stringify(callTimeHistogram, undefined, 2)}`);
printAllHistograms();
callback(null, accumulatedStats);
}
}
Expand Down

0 comments on commit 7229a2a

Please sign in to comment.