Skip to content

Commit 840ff68

Browse files
committed
Add support for LATENCY HISTOGRAM
1 parent 2fc79bd commit 840ff68

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

packages/client/lib/client/commands.ts

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import * as KEYS from '../commands/KEYS';
8686
import * as LASTSAVE from '../commands/LASTSAVE';
8787
import * as LATENCY_DOCTOR from '../commands/LATENCY_DOCTOR';
8888
import * as LATENCY_GRAPH from '../commands/LATENCY_GRAPH';
89+
import * as LATENCY_HISTOGRAM from '../commands/LATENCY_HISTOGRAM';
8990
import * as LATENCY_HISTORY from '../commands/LATENCY_HISTORY';
9091
import * as LATENCY_LATEST from '../commands/LATENCY_LATEST';
9192
import * as LOLWUT from '../commands/LOLWUT';
@@ -299,6 +300,8 @@ export default {
299300
latencyDoctor: LATENCY_DOCTOR,
300301
LATENCY_GRAPH,
301302
latencyGraph: LATENCY_GRAPH,
303+
LATENCY_HISTOGRAM,
304+
latencyHistogram: LATENCY_HISTOGRAM,
302305
LATENCY_HISTORY,
303306
latencyHistory: LATENCY_HISTORY,
304307
LATENCY_LATEST,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {strict as assert} from 'assert';
2+
import testUtils, {GLOBAL} from '../test-utils';
3+
import { transformArguments } from './LATENCY_HISTOGRAM';
4+
5+
describe('LATENCY HISTOGRAM', () => {
6+
it('transformArguments', () => {
7+
assert.deepEqual(
8+
transformArguments('command'),
9+
['LATENCY', 'HISTOGRAM', 'command']
10+
);
11+
});
12+
13+
testUtils.testWithClient('client.latencyHistory', async client => {
14+
await Promise.all([
15+
client.set('sample-key', 'sample-value'),
16+
client.get('sample-key')
17+
]);
18+
19+
const commandNames = ['set', 'get'];
20+
const latencyHistograms = await client.latencyHistogram(...commandNames);
21+
22+
// assert.ok(latencyHistograms instanceof Map);
23+
24+
for (const commandName of commandNames) {
25+
const commandInfo = latencyHistograms[commandName];
26+
assert.ok(Number.isInteger(commandInfo['calls']));
27+
assert.ok(Array.isArray(commandInfo['histogram_usec']));
28+
}
29+
}, GLOBAL.SERVERS.OPEN);
30+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
3+
export type LatencyHistogram = Record<string, {
4+
calls: number;
5+
histogram_usec: HistogramData;
6+
}>;
7+
8+
export type HistogramData = number[];
9+
10+
export type CommandInfo = [
11+
string,
12+
number,
13+
string,
14+
HistogramData
15+
]
16+
17+
export type RawReply = (string | CommandInfo)[];
18+
19+
export function transformArguments(...commands: RedisCommandArgument[]): RedisCommandArguments {
20+
return ['LATENCY', 'HISTOGRAM', ...commands];
21+
}
22+
23+
export function transformReply(rawReply: RawReply): LatencyHistogram {
24+
const result: LatencyHistogram = {};
25+
26+
for (let i = 0; i < rawReply.length; i += 2) {
27+
const [command, [_, calls, __, histogram]] = [rawReply[i] as string, rawReply[i + 1] as CommandInfo];
28+
result[command] = {
29+
calls,
30+
histogram_usec: histogram
31+
};
32+
}
33+
return result;
34+
};

0 commit comments

Comments
 (0)