ruby_impl.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module App
  2. Ruby = lambda do |env|
  3. content_type, body = case env['PATH_INFO']
  4. when '/plaintext'
  5. ['text/plain; charset=utf-8', "Hello, World!"]
  6. when '/json'
  7. ['application/json; charset=utf-8', {:message => "Hello, World!"}.to_json]
  8. when '/db'
  9. id = Random.rand(10000) + 1
  10. query = "SELECT * FROM World WHERE id = " + id.to_s
  11. client = Mysql2::Client.new(DB_CONFIG)
  12. results = begin
  13. client.query(query)
  14. ensure
  15. client.close
  16. end
  17. ['application/json; charset=utf-8', results.first.to_json]
  18. when '/queries'
  19. query_string = Rack::Utils.parse_query(env['QUERY_STRING'])
  20. queries = query_string['queries'].to_i
  21. queries = 1 if queries < 1
  22. queries = 500 if queries > 500
  23. client = Mysql2::Client.new(DB_CONFIG)
  24. results = begin
  25. (1..queries).map do
  26. id = Random.rand(10000) + 1
  27. client.query("SELECT * FROM World WHERE id = " + id.to_s).first
  28. end
  29. ensure
  30. client.close
  31. end
  32. ['application/json; charset=utf-8', results.to_json]
  33. when '/updates'
  34. query_string = Rack::Utils.parse_query(env['QUERY_STRING'])
  35. queries = query_string['queries'].to_i
  36. queries = 1 if queries < 1
  37. queries = 500 if queries > 500
  38. client = Mysql2::Client.new(DB_CONFIG)
  39. results = begin
  40. results = (1..queries).map do
  41. id = Random.rand(10000) + 1
  42. client.query("SELECT * FROM World WHERE id = " + id.to_s).first
  43. end
  44. #mass update
  45. values = results.map { |h| ['(', h['id'], ',' ,Random.rand(10000) + 1, ')', ','] }.flatten[0..-2].join
  46. sql = "INSERT INTO `World` (`id`,`randomNumber`) VALUES #{values} ON DUPLICATE KEY UPDATE `World`.`randomNumber` = VALUES(`randomNumber`)"
  47. client.query(sql)
  48. results
  49. ensure
  50. client.close
  51. end
  52. ['application/json; charset=utf-8', results.to_json]
  53. end
  54. [200, { 'Content-Type' => content_type }, [body]]
  55. end
  56. end