app.lua 959 B

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