Skip to content

Commit a9c3dce

Browse files
authored
Merge pull request torrust#579 from WarmBeer/preload-wrk-benchmark
Preload info hashes in wrk benchmark
2 parents 1439a5e + fc9bd77 commit a9c3dce

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

tests/wrk_benchmark_announce.lua

+52-19
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,67 @@
1+
-- else the randomness would be the same every run
2+
math.randomseed(os.time())
3+
4+
local charset = "0123456789ABCDEF"
5+
6+
function hex_to_char(hex)
7+
local n = tonumber(hex, 16)
8+
local f = string.char(n)
9+
return f
10+
end
11+
12+
function hex_string_to_char_string(hex)
13+
local ret = {}
14+
local r
15+
for i = 0, 19 do
16+
local x = i * 2
17+
r = hex:sub(x+1, x+2)
18+
local f = hex_to_char(r)
19+
table.insert(ret, f)
20+
end
21+
return table.concat(ret)
22+
end
23+
24+
function url_encode(str)
25+
str = string.gsub (str, "([^0-9a-zA-Z !'()*._~-])", -- locale independent
26+
function (c) return string.format ("%%%02X", string.byte(c)) end)
27+
str = string.gsub (str, " ", "+")
28+
return str
29+
end
30+
31+
function gen_hex_string(length)
32+
local ret = {}
33+
local r
34+
for i = 1, length do
35+
r = math.random(1, #charset)
36+
table.insert(ret, charset:sub(r, r))
37+
end
38+
return table.concat(ret)
39+
end
40+
41+
function random_info_hash()
42+
local hexString = gen_hex_string(40)
43+
local str = hex_string_to_char_string(hexString)
44+
return url_encode(str)
45+
end
46+
147
function generate_unique_info_hashes(size)
248
local result = {}
3-
local seen = {}
4-
5-
for i = 0, size - 1 do
6-
local bytes = {}
7-
bytes[1] = i & 0xFF
8-
bytes[2] = (i >> 8) & 0xFF
9-
bytes[3] = (i >> 16) & 0xFF
10-
bytes[4] = (i >> 24) & 0xFF
11-
12-
local info_hash = bytes
13-
local key = table.concat(info_hash, ",")
14-
15-
if not seen[key] then
16-
table.insert(result, info_hash)
17-
seen[key] = true
18-
end
49+
50+
for i = 1, size do
51+
result[i] = random_info_hash()
1952
end
2053

2154
return result
2255
end
2356

24-
info_hashes = generate_unique_info_hashes(10000000)
57+
info_hashes = generate_unique_info_hashes(5000000)
2558

26-
index = 0
59+
index = 1
2760

2861
-- the request function that will run at each request
2962
request = function()
3063
path = "/announce?info_hash=" .. info_hashes[index] .. "&peer_id=-lt0D80-a%D4%10%19%99%A6yh%9A%E1%CD%96&port=54434&uploaded=885&downloaded=0&left=0&corrupt=0&key=A78381BD&numwant=200&compact=1&no_peer_id=1&supportcrypto=1&redundant=0"
31-
index += 1
64+
index = index + 1
3265
headers = {}
3366
headers["X-Forwarded-For"] = "1.1.1.1"
3467
return wrk.format("GET", path, headers)

0 commit comments

Comments
 (0)