From 7269f69aa7b4fd74906f13f03b3d92131872eac0 Mon Sep 17 00:00:00 2001 From: SotaSato-stst Date: Mon, 19 Aug 2024 00:57:38 +0900 Subject: [PATCH] Add support for LATENCY HISTOGRAM --- packages/client/lib/client/commands.ts | 3 ++ .../lib/commands/LATENCY_HISTOGRAM.spec.ts | 28 +++++++++++++++ .../client/lib/commands/LATENCY_HISTOGRAM.ts | 34 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 packages/client/lib/commands/LATENCY_HISTOGRAM.spec.ts create mode 100644 packages/client/lib/commands/LATENCY_HISTOGRAM.ts diff --git a/packages/client/lib/client/commands.ts b/packages/client/lib/client/commands.ts index 76ae5d73735..6ee0b0be775 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 00000000000..9c66f581fb7 --- /dev/null +++ b/packages/client/lib/commands/LATENCY_HISTOGRAM.spec.ts @@ -0,0 +1,28 @@ +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); + + 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); +}); diff --git a/packages/client/lib/commands/LATENCY_HISTOGRAM.ts b/packages/client/lib/commands/LATENCY_HISTOGRAM.ts new file mode 100644 index 00000000000..446fb867ff4 --- /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; +};