diff --git a/packages/client/lib/client/commands.ts b/packages/client/lib/client/commands.ts index 76ae5d7373..6ee0b0be77 100644 --- a/packages/client/lib/client/commands.ts +++ b/packages/client/lib/client/commands.ts @@ -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'; @@ -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, diff --git a/packages/client/lib/commands/LATENCY_HISTOGRAM.spec.ts b/packages/client/lib/commands/LATENCY_HISTOGRAM.spec.ts new file mode 100644 index 0000000000..941b96df9c --- /dev/null +++ b/packages/client/lib/commands/LATENCY_HISTOGRAM.spec.ts @@ -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); +}); \ No newline at end of file diff --git a/packages/client/lib/commands/LATENCY_HISTOGRAM.ts b/packages/client/lib/commands/LATENCY_HISTOGRAM.ts new file mode 100644 index 0000000000..446fb867ff --- /dev/null +++ b/packages/client/lib/commands/LATENCY_HISTOGRAM.ts @@ -0,0 +1,34 @@ +import { RedisCommandArgument, RedisCommandArguments } from '.'; + +export type LatencyHistogram = Record; + +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; +};