-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcreate.lua
More file actions
63 lines (52 loc) · 1.87 KB
/
create.lua
File metadata and controls
63 lines (52 loc) · 1.87 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
local current_folder = debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])")
package.path = current_folder .. "/?.lua;" .. package.path
local base64url = require("base64url")
local socket = require("socket")
local json = require("json")
local uuid = require("uuid")
local sha = require("sha2")
time = math.floor(socket.gettime() * 1000)
math.randomseed(time)
uuid.randomseed(time)
local thread_count = 1
-- This function runs after all threads have been created
-- but before any of them runs
-- Its goal is to give each thread a unique thread id (tid)
function setup(thread)
thread:set("tid", ""..thread_count)
thread_count = thread_count + 1
end
-- This function initializes each thread. It expects the name of the
-- experiment (this ensures that experiment for create counter with
-- a given load is in a different namespace as a create counter
-- with a different given load. As a result, we don't need to
-- delete all ledgers in the coordinator/endorsers since we would be creating
-- brand new ledgers on each experiment.
function init(args)
if args[1] ~= nil then
tid = args[1] .. tid
end
end
local function fromhex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
-- Each thread gets its own context, so all threads have this variable initialized
-- at 0, and updated independently
ledger_id = 0
handles = {}
request = function()
local hash = sha.sha256(tid.."counter"..ledger_id)
local handle = base64url.encode(fromhex(hash))
ledger_id = ledger_id + 1
local endpoint_addr = "/counters/" .. handle
local method = "PUT"
local headers = {}
local param = {
Tag = base64url.encode(fromhex(sha.sha256(tid.."counter"..ledger_id..uuid()))),
}
local body = json.encode(param)
headers["Content-Type"] = "application/json"
return wrk.format(method, endpoint_addr, headers, body)
end