Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/client/lib/commands/LATENCY_HISTOGRAM.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import LATENCY_HISTOGRAM from './LATENCY_HISTOGRAM';
import { parseArgs } from './generic-transformers';

describe('LATENCY HISTOGRAM', () => {
describe('transformArguments', () => {
it('filtered by command set', () => {
assert.deepStrictEqual(
parseArgs(LATENCY_HISTOGRAM, 'set'),
['LATENCY', 'HISTOGRAM', 'set'],
);
});

it('unfiltered', () => {
assert.deepStrictEqual(
parseArgs(LATENCY_HISTOGRAM),
['LATENCY', 'HISTOGRAM'],
);
});
});

testUtils.testWithClient('unfiltered list', async client => {
await client.configResetStat();
await Promise.all([
client.lPush('push-key', 'hello '),
client.set('set-key', 'world!'),
]);
const histogram = await client.latencyHistogram();
const commands = ['config|resetstat', 'set', 'lpush'];
for (const command of commands) {
assert.ok(typeof histogram[command]['calls'], 'number');
assert.ok(Array.isArray(histogram[command]['histogram_usec']));
}
}, GLOBAL.SERVERS.OPEN);

testUtils.testWithClient('filtered by a command list', async client => {
await client.configSet('latency-monitor-threshold', '100');
await client.set('set-key', 'hello');
const histogram = await client.latencyHistogram('set');
assert.ok(typeof histogram.set['calls'], 'number');
assert.ok(Array.isArray(histogram.set['histogram_usec']));
}, GLOBAL.SERVERS.OPEN);
});
40 changes: 40 additions & 0 deletions packages/client/lib/commands/LATENCY_HISTOGRAM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { CommandParser } from '../client/parser';
import { Command } from '../RESP/types';

type RawHistogram = [string, number, string, number[]];

type Histogram = Record<string, {
calls: number;
histogram_usec: number[];
}>;

export default {
CACHEABLE: false,
IS_READ_ONLY: true,
/**
* Constructs the LATENCY HISTOGRAM command
*
* @param parser - The command parser
* @param commands - The list of redis commands to get histogram for
* @see https://redis.io/docs/latest/commands/latency-histogram/
*/
parseCommand(parser: CommandParser, ...commands: string[]) {
const args = ['LATENCY', 'HISTOGRAM'];
if (commands.length !== 0) {
args.push(...commands);
}
parser.push(...args);
},
transformReply(reply: (string | RawHistogram)[]): Histogram {
const result: Histogram = {};
if (reply.length === 0) return result;
for (let i = 1; i < reply.length; i += 2) {
const histogram = reply[i] as RawHistogram;
result[reply[i - 1] as string] = {
calls: histogram[1],
histogram_usec: histogram[3],
};
}
return result;
}
} as const satisfies Command;
3 changes: 3 additions & 0 deletions packages/client/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ import VREM from './VREM';
import VSETATTR from './VSETATTR';
import VSIM from './VSIM';
import VSIM_WITHSCORES from './VSIM_WITHSCORES';
import LATENCY_HISTOGRAM from './LATENCY_HISTOGRAM';

export {
CLIENT_KILL_FILTERS,
Expand Down Expand Up @@ -715,6 +716,8 @@ export default {
latencyGraph: LATENCY_GRAPH,
LATENCY_HISTORY,
latencyHistory: LATENCY_HISTORY,
LATENCY_HISTOGRAM,
latencyHistogram: LATENCY_HISTOGRAM,
LATENCY_LATEST,
latencyLatest: LATENCY_LATEST,
LATENCY_RESET,
Expand Down