Skip to content

Commit 95baab4

Browse files
committed
feat: slowlog injection accepts argument for time
1 parent a2d7b55 commit 95baab4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

inject_slow_query.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# WARNING: This will lock up CPU on the target Redis instance. Never run against production.
3+
#
24
# Copyright 2020 Scribd, Inc.
35

46
require 'logger'
@@ -12,20 +14,26 @@
1214
ssl: :true
1315
)
1416

15-
SCRIPT =<<END
17+
if ARGV[0].nil?
18+
raise "Specify milliseconds to inject as the first positional argument to `#{__FILE__}`"
19+
else
20+
milliseconds = ARGV[0].to_i
21+
end
22+
23+
SCRIPT =<<HEREDOC
1624
-- From https://medium.com/@stockholmux/simulating-a-slow-command-with-node-redis-and-lua-efadbf913cd9
1725
local aTempKey = "a-temp-key"
1826
local cycles
1927
redis.call("SET",aTempKey,"1")
20-
redis.call("PEXPIRE",aTempKey, 100)
21-
for i = 0, 1500000, 1 do
28+
redis.call("PEXPIRE",aTempKey, #{milliseconds})
29+
for i = 0, #{15000 * milliseconds}, 1 do
2230
local apttl = redis.call("PTTL",aTempKey)
2331
cycles = i;
2432
if apttl == 0 then
2533
break;
2634
end
2735
end
2836
return cycles
29-
END
37+
HEREDOC
3038

3139
LOGGER.info REDIS.eval(SCRIPT)

0 commit comments

Comments
 (0)