app.lua 1018 B

12345678910111213141516171819202122232425262728293031323334
  1. local mysql = mysql
  2. local encode = encode
  3. local random = math.random
  4. local min = math.min
  5. local mysqlconn = {
  6. host = "DBHOSTNAME",
  7. port = 3306,
  8. database = "hello_world",
  9. user = "benchmarkdbuser",
  10. password = "benchmarkdbpass"
  11. }
  12. return function(ngx)
  13. local db = mysql:new()
  14. assert(db:connect(mysqlconn))
  15. local num_queries = tonumber(ngx.var.arg_queries) or 1
  16. -- May seem like a stupid branch, but since we know that
  17. -- at a benchmark it will always be taken one way,
  18. -- it doesn't matter. For me, after a small warmup, the performance
  19. -- is identical to a version without the branch
  20. -- http://wiki.luajit.org/Numerical-Computing-Performance-Guide
  21. if num_queries == 1 then
  22. ngx.print(encode(db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]))
  23. else
  24. local worlds = {}
  25. num_queries = min(500, num_queries)
  26. for i=1, num_queries do
  27. worlds[#worlds+1] = db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]
  28. end
  29. ngx.print( encode(worlds) )
  30. end
  31. db:set_keepalive(0, 256)
  32. end