Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
194 lines (174 loc) · 11.5 KB
/
Copy pathcli.js
File metadata and controls
194 lines (174 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 KeyValueStore.com
//
// Command line for the Traffic Analyzer. Run "node traffic/cli.js --help".
'use strict';
const fs = require('fs');
const T = require('./traffic.js');
const HELP = `Traffic Analyzer: what the commands in a Redis or Valkey MONITOR capture add up to.
Usage:
node traffic/cli.js monitor.txt Totals: commands per second, the command mix,
busiest keys, key patterns, clients, cluster
spread, findings, and the LRU hit-rate curve
node traffic/cli.js monitor.txt --keys Every key as CSV: db, key, reads, writes, deletes
node traffic/cli.js monitor.txt --curve The hit-rate curve as CSV: cache size in keys,
share of reads served
node traffic/cli.js monitor.txt --seconds Commands per second as CSV
node traffic/cli.js monitor.txt --cache 5000 Share of reads a cache of 5,000 keys would serve
node traffic/cli.js - Read the capture from standard input
Options:
--top N How many commands, keys, patterns and clients to list (default 15)
--primaries N Primaries for the cluster spread (default 3)
--json Print everything as JSON
--no-curve Skip the hit-rate curve, which needs memory for every key access
Record a capture with:
redis-cli -h HOST -p PORT MONITOR > monitor.txt (Ctrl-C to stop)
timeout 60 valkey-cli -h HOST -p PORT MONITOR > monitor.txt
MONITOR slows a busy server down, so keep captures short.`;
const fmt = (n) => Number(n).toLocaleString('en-US');
const pct = (x) => (100 * x).toFixed(1) + '%';
const plural = (n, one, many) => fmt(n) + ' ' + (n === 1 ? one : many);
const strict = new TextDecoder('utf-8', { fatal: true });
// Spreadsheets run a cell that starts with = + - or @ as a formula, so
// such a key gets a ' in front, the usual guard.
function csv(s) { if (/^[=+\-@\t\r]/.test(s)) s = "'" + s; return /[",\n\r]/.test(s) ? '"' + s.replace(/"/g, '""') + '"' : s; }
// A key for CSV: its text when it is valid UTF-8, otherwise quoted with \xHH escapes.
function csvKey(b) { try { return csv(strict.decode(b)); } catch (e) { return csv(T.showKey(b)); } }
// A whole number from an option, or NaN when it isn't one.
const number = (s) => (/^\d+$/.test(s || '') ? parseInt(s, 10) : NaN);
function clock(ms) { return new Date(ms).toISOString().replace('T', ' ').slice(0, 19); }
function duration(s) {
if (s < 1) return Math.round(s * 1000) + ' ms';
if (s < 120) return s.toFixed(1) + ' s';
if (s < 7200) return (s / 60).toFixed(1) + ' min';
return (s / 3600).toFixed(1) + ' h';
}
async function main(argv) {
let file = null, mode = 'summary', top = 15, primaries = 3, json = false, curve = true, cache = null;
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === '--help' || a === '-h') { console.log(HELP); return 0; }
else if (a === '--keys') mode = 'keys';
else if (a === '--curve') mode = 'curve';
else if (a === '--seconds') mode = 'seconds';
else if (a === '--cache') { mode = 'cache'; cache = number(argv[++i]); }
else if (a === '--top') top = number(argv[++i]);
else if (a === '--primaries') primaries = number(argv[++i]);
else if (a === '--json') json = true;
else if (a === '--no-curve') curve = false;
else if (a.startsWith('--')) { console.error('Unknown option ' + a + '. Try --help.'); return 2; }
else file = a;
}
if (!file) { console.error('Name a capture file, or - for standard input. Try --help.'); return 2; }
if ((mode === 'curve' || mode === 'cache') && !curve) { console.error('--curve and --cache need the curve.'); return 2; }
if (mode === 'cache' && !(cache >= 0)) { console.error('--cache needs a number of keys.'); return 2; }
if (!(top >= 1)) { console.error('--top needs a number of 1 or more.'); return 2; }
if (!(primaries >= 1 && primaries <= 16384)) { console.error('--primaries needs a number from 1 to 16384.'); return 2; }
// Read in pieces, so captures of any length work.
const an = T.analyzer({ curve: curve });
const decoder = new TextDecoder('utf-8');
const stream = file === '-' ? process.stdin : fs.createReadStream(file, { highWaterMark: 4 * 1024 * 1024 });
for await (const chunk of stream) an.addChunk(decoder.decode(chunk, { stream: true }), false);
an.addChunk(decoder.decode(), true);
const r = an.result({ top: top, primaries: primaries });
if (!r.commands) {
console.error('No MONITOR lines found.' + (r.unparsedExamples.length ? ' The first line reads: ' + r.unparsedExamples[0] : ''));
return 1;
}
if (mode === 'keys') {
// Written as fast as the reader takes it, so a pipe doesn't fill memory.
const out = process.stdout;
let lines = ['db,key,reads,writes,deletes'];
for (let i = 0, n = an.keyCount(); i < n; i++) {
const [db, key, reads, writes, deletes] = an.keyAt(i);
lines.push([db, csvKey(T.fromLatin1(key)), reads, writes, deletes].join(','));
if (lines.length >= 10000) { if (!out.write(lines.join('\n') + '\n')) await new Promise((done) => out.once('drain', done)); lines = []; }
}
if (lines.length) out.write(lines.join('\n') + '\n');
return 0;
}
if (mode === 'seconds') {
console.log(r.series.step === 1 ? 'time,commands' : `time,commands in ${r.series.step} s`);
r.series.counts.forEach((n, i) => console.log(clock((r.series.start + i * r.series.step) * 1000) + ',' + n));
return 0;
}
const c = curve ? an.curve() : null;
if (mode === 'curve') {
console.log('keys,hit_rate');
for (const [k, h] of c.points) console.log(k + ',' + h.toFixed(6));
return 0;
}
if (mode === 'cache') {
console.log(`A cache of ${plural(cache, 'key', 'keys')} would serve ${pct(c.hitRate(cache))} of the ${fmt(c.reads)} reads. Holding every key, the best possible is ${pct(c.best)}.`);
return 0;
}
if (json) {
const out = Object.assign({}, r, {
file: file,
keys: Object.assign({}, r.keys, { top: r.keys.top.map((k) => Object.assign({}, k, { key: T.showKey(k.key) })) }),
curve: c ? { accesses: c.accesses, reads: c.reads, coldReads: c.coldReads, keys: c.keys, best: c.best, partial: c.partial, sizes: c.sizes, points: c.points } : null
});
console.log(JSON.stringify(out, null, 2));
return 0;
}
const name = file === '-' ? 'standard input' : file;
console.log(`${name}: ${plural(r.commands, 'command', 'commands')} in ${duration(r.duration)}, from ${clock(r.start)} to ${clock(r.end)} UTC`);
// The peak is the busiest whole second inside the capture, so a capture
// shorter than that has none.
const average = r.average.toLocaleString('en-US', { maximumFractionDigits: 1 });
if (r.duration >= 1) console.log(`Average ${average} commands a second` + (r.peak ? `, peak ${fmt(r.peak.count)} at ${clock(r.peak.time * 1000).slice(11)}` : ''));
else if (r.duration >= 0.01) console.log(`About ${average} commands a second while it ran`);
const k = r.kinds;
console.log(`Reads ${pct(k.read / r.commands)}, writes ${pct(k.write / r.commands)}, scripts ${pct(k.script / r.commands)}, pub/sub ${pct(k.pubsub / r.commands)}, other ${pct(k.other / r.commands)}`);
console.log(`${plural(r.keys.distinct, 'key', 'keys')}, ${plural(r.keys.accesses, 'key access', 'key accesses')} (${plural(r.keys.reads, 'read', 'reads')}, ${plural(r.keys.writes, 'write', 'writes')}, ${plural(r.keys.deletes, 'delete', 'deletes')}), ${plural(r.connections, 'connection', 'connections')} from ${plural(r.hostCount, 'address', 'addresses')}`);
if (r.unparsed) console.log(r.unparsed === 1 ? `1 line wasn't a MONITOR line and was skipped: ${r.unparsedExamples[0]}` : `${fmt(r.unparsed)} lines weren't MONITOR lines and were skipped, such as: ${r.unparsedExamples[0]}`);
console.log('\nCommands');
console.log(' count share kind command');
for (const x of r.byCommand.slice(0, top)) console.log(` ${fmt(x.count).padStart(8)} ${pct(x.count / r.commands).padStart(6)} ${x.kind.padEnd(7)} ${x.name}`);
if (r.byCommand.length > top) console.log(` and ${plural(r.byCommand.length - top, 'other command', 'other commands')}`);
if (r.byDb.length > 1) console.log('\nBy database: ' + r.byDb.map((d) => `db${d.db} ${fmt(d.count)}`).join(', '));
if (r.keys.distinct) {
console.log('\nBusiest keys');
console.log(' accesses reads writes deletes key');
for (const x of r.keys.top) console.log(` ${fmt(x.total).padStart(8)} ${fmt(x.reads).padStart(8)} ${fmt(x.writes).padStart(8)} ${fmt(x.deletes).padStart(8)} ${x.db ? 'db' + x.db + ' ' : ''}${T.showKey(x.key)}`);
console.log(`\nKey patterns (${fmt(r.patternCount)} in all${r.separator ? ', split at "' + r.separator + '"' : ''})`);
console.log(' keys accesses reads writes pattern');
for (const x of r.patterns) console.log(` ${fmt(x.keys).padStart(8)} ${fmt(x.accesses).padStart(8)} ${fmt(x.reads).padStart(8)} ${fmt(x.writes).padStart(8)} ${x.pattern}`);
}
console.log('\nClients');
console.log(' commands connections address (top commands)');
for (const h of r.hosts) console.log(` ${fmt(h.count).padStart(8)} ${(h.host === 'lua' ? '' : fmt(h.connections)).padStart(11)} ${h.host === 'lua' ? 'lua (commands run by scripts)' : h.host} (${h.top.map((t) => t[0] + ' ' + fmt(t[1])).join(', ')})`);
if (r.cluster.keyAccesses) {
console.log(`\nCluster spread over ${plural(r.cluster.primaries, 'primary', 'primaries')}, by key accesses`);
r.cluster.ranges.forEach(([a, b], i) => console.log(` primary ${String(i + 1).padEnd(3)} slots ${(a + '-' + b).padEnd(12)} ${pct(r.cluster.perPrimary[i] / r.cluster.keyAccesses).padStart(6)}`));
console.log(` Busiest slots: ${r.cluster.hotSlots.slice(0, 5).map((s) => s.slot + ' (' + fmt(s.accesses) + ')').join(', ')}`);
}
if (r.findings.length) {
console.log('\nWorth a look');
for (const f of r.findings) {
console.log(` ${f.title} (${plural(f.count, 'time', 'times')})`);
if (f.examples.length) console.log(` ${f.examples.join(' | ')}`);
console.log(` ${f.text}`);
}
}
if (c && c.reads) {
console.log(`\nLRU cache hit rate (${fmt(c.reads)} reads, ${fmt(c.coldReads)} of them the first use of a key, which no cache can serve)`);
if (c.partial) console.log(` Covers the first ${fmt(c.accesses)} key accesses only.`);
console.log(` Holding all ${fmt(c.keys)} keys serves ${pct(c.best)} of reads, the best possible.`);
const sizes = c.sizes.filter((s) => s.keys !== null).map((s) => `${Math.round(s.share * 100)}% takes ${fmt(s.keys)}`);
if (sizes.length) console.log(` Reaching ${sizes[0].replace('takes', 'of that takes')} ${c.sizes[0].keys === 1 ? 'key' : 'keys'}${sizes.length > 1 ? ', ' + sizes.slice(1).join(', ') : ''}.`);
console.log(' keys hit rate');
const shown = new Set();
for (let n = 1; n < c.keys; n *= 10) for (const m of [1, 2, 5]) if (n * m < c.keys) shown.add(n * m);
shown.add(c.keys);
for (const n of Array.from(shown).sort((a, b) => a - b)) console.log(` ${fmt(n).padStart(10)} ${pct(c.hitRate(n)).padStart(8)}`);
}
return 0;
}
// Stop quietly when the output is piped into something like head that closes early.
process.stdout.on('error', (e) => { if (e.code === 'EPIPE') process.exit(0); throw e; });
main(process.argv.slice(2)).then((code) => { process.exitCode = code; }, (e) => {
console.error(e.code === 'ENOENT' ? 'No such file: ' + e.path : e.code === 'EISDIR' ? 'That is a folder, not a file.' : 'Could not read the capture: ' + e.message);
process.exitCode = 2;
});