3

My question is that when I run

wrk -d10s -t20 -c20 -s /mnt/c/xxxx/post.lua http://localhost:xxxx/post

the Lua script that is only executed once? It will only put one item into the database at the URL.

-- example HTTP POST script which demonstrates setting the
-- HTTP method, body, and adding a header
math.randomseed(os.time())
number =  math.random()
wrk.method = "POST"
wrk.headers["Content-Type"] = "application/json"        
wrk.body = '{"name": "' .. tostring(number) .. '", "title":"test","enabled":true,"defaultValue":false}'

Is there a way to make it create the 'number' variable dynamically and keep adding new items into the database until the 'wrk' command has finished its test? Or that it will keep executing the script for the duration of the test creating and inserting new 'number' variables into 'wrk.body' ?

Apologies I have literally only being looking at Lua for a few hours.

Thanks

dawson
  • 71
  • 1
  • 8

1 Answers1

1

When you do

number = math.random

you're not setting number to a random number, you're setting it equal to the function math.random. To set the variable to the value returned by the function, that line should read

number = math.random()

You may also need to set a random seed (with the math.randomseed() function and your choice of an appropriately variable argument - system time is common) to avoid math.random() giving the same result each time the script is run. This should be done before the first call to math.random.

As the script is short, system time probably isn't a good choice of seed here (the script runs far quicker than the value from os.time() changes, so running it several times immediately after one another gives the same results each time). Reading a few bytes from /dev/urandom should give better results.

You could also just use /dev/urandom to generate a number directly, rather than feeding it to math.random as a seed. Like in the code below, as taken from this answer. This isn't a secure random number generator, but for your purposes it would be fine.

urand = assert (io.open ('/dev/urandom', 'rb'))
rand  = assert (io.open ('/dev/random', 'rb'))

function RNG (b, m, r)
  b = b or 4
  m = m or 256
  r = r or urand
  local n, s = 0, r:read (b)

  for i = 1, s:len () do
    n = m * n + s:byte (i)
  end

  return n
end
Chris H
  • 790
  • 9
  • 19
  • Hi, yep I realised that when I was printing out the value, have it changed now in the question. My question is more about is it possible that for the duration that 'wrk' command is being executed it will keep re-executing the script generating and inserting different values each time? – dawson Sep 15 '17 at 09:58
  • The values depend on the seed. Since it's a short script, the system time might not be granular enough to change between runs, so you get the same seed (and therefore the same 'random' numbers) each time the script runs. You just need to find a different source of random seed. Try reading something from /dev/urandom. – Chris H Sep 15 '17 at 10:03
  • @dawson I've just edited the answer with something that might be useful – Chris H Sep 15 '17 at 10:13
  • Seems to be giving same issue, executing faster than it can generate. Ran it twice with the above code twice and first time only inserted 16 items and second only 15 items before saying that they already exist. Will look further into the seed – dawson Sep 15 '17 at 10:23
  • 1
    I've found that it will enter into the database a number of items equal to what i set '-t' in the wrk command too, after that it reuses them and program catches them as already existent in the DB – dawson Sep 15 '17 at 15:10