Skip to content

Commit

Permalink
Add support for LATENCY HISTOGRAM
Browse files Browse the repository at this point in the history
  • Loading branch information
SotaSato-stst committed Aug 18, 2024
1 parent 2fc79bd commit ae20620
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/client/lib/client/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import * as KEYS from '../commands/KEYS';
import * as LASTSAVE from '../commands/LASTSAVE';
import * as LATENCY_DOCTOR from '../commands/LATENCY_DOCTOR';
import * as LATENCY_GRAPH from '../commands/LATENCY_GRAPH';
import * as LATENCY_HISTOGRAM from '../commands/LATENCY_HISTOGRAM';
import * as LATENCY_HISTORY from '../commands/LATENCY_HISTORY';
import * as LATENCY_LATEST from '../commands/LATENCY_LATEST';
import * as LOLWUT from '../commands/LOLWUT';
Expand Down Expand Up @@ -299,6 +300,8 @@ export default {
latencyDoctor: LATENCY_DOCTOR,
LATENCY_GRAPH,
latencyGraph: LATENCY_GRAPH,
LATENCY_HISTOGRAM,
latencyHistogram: LATENCY_HISTOGRAM,
LATENCY_HISTORY,
latencyHistory: LATENCY_HISTORY,
LATENCY_LATEST,
Expand Down
30 changes: 30 additions & 0 deletions packages/client/lib/commands/LATENCY_HISTOGRAM.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {strict as assert} from 'assert';
import testUtils, {GLOBAL} from '../test-utils';
import { transformArguments } from './LATENCY_HISTOGRAM';

describe('LATENCY HISTOGRAM', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('command'),
['LATENCY', 'HISTOGRAM', 'command']
);
});

testUtils.testWithClient('client.latencyHistory', async client => {
await Promise.all([
client.set('sample-key', 'sample-value'),
client.get('sample-key')
]);

const commandNames = ['set', 'get'];
const latencyHistograms = await client.latencyHistogram(...commandNames);

// assert.ok(latencyHistograms instanceof Map);

for (const commandName of commandNames) {
const commandInfo = latencyHistograms[commandName];
assert.ok(Number.isInteger(commandInfo['calls']));
assert.ok(Array.isArray(commandInfo['histogram_usec']));
}
}, GLOBAL.SERVERS.OPEN);
});
34 changes: 34 additions & 0 deletions packages/client/lib/commands/LATENCY_HISTOGRAM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';

export type LatencyHistogram = Record<string, {
calls: number;
histogram_usec: HistogramData;
}>;

export type HistogramData = number[];

export type CommandInfo = [
string,
number,
string,
HistogramData
]

export type RawReply = (string | CommandInfo)[];

export function transformArguments(...commands: RedisCommandArgument[]): RedisCommandArguments {
return ['LATENCY', 'HISTOGRAM', ...commands];
}

export function transformReply(rawReply: RawReply): LatencyHistogram {
const result: LatencyHistogram = {};

for (let i = 0; i < rawReply.length; i += 2) {
const [command, [_, calls, __, histogram]] = [rawReply[i] as string, rawReply[i + 1] as CommandInfo];
result[command] = {
calls,
histogram_usec: histogram
};
}
return result;
};

0 comments on commit ae20620

Please sign in to comment.