helpers.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import os
  2. import sys
  3. from collections import namedtuple
  4. from random import randint
  5. import jinja2
  6. from flask import request
  7. if sys.version_info[0] == 3:
  8. xrange = range
  9. _is_pypy = hasattr(sys, "pypy_version_info")
  10. if _is_pypy:
  11. from psycopg2cffi.pool import ThreadedConnectionPool
  12. from psycopg2cffi.extras import execute_batch
  13. import ujson as orjson
  14. else:
  15. from psycopg2.pool import ThreadedConnectionPool
  16. from psycopg2.extras import execute_batch
  17. import orjson
  18. try:
  19. import meinheld
  20. import meinheld.patch
  21. meinheld.server.set_access_logger(None)
  22. meinheld.set_keepalive(30)
  23. meinheld.patch.patch_all()
  24. except ImportError:
  25. pass
  26. def get_num_queries():
  27. try:
  28. num_queries = request.args.get("queries", 1, type=int)
  29. except ValueError:
  30. num_queries = 1
  31. if num_queries < 1:
  32. return 1
  33. if num_queries > 500:
  34. return 500
  35. return num_queries
  36. def generate_ids(num_queries):
  37. ids = {randint(1, 10000) for _ in xrange(num_queries)}
  38. while len(ids) < num_queries:
  39. ids.add(randint(1, 10000))
  40. return list(sorted(ids))
  41. def load_fortunes_template():
  42. path = os.path.join("templates", "fortune_raw.html")
  43. with open(path, "r") as template_file:
  44. template_text = template_file.read()
  45. return jinja2.Template(template_text)
  46. def setup(threads):
  47. pool = ThreadedConnectionPool(
  48. minconn=int(threads / 4),
  49. maxconn=int(threads / 4),
  50. database="hello_world",
  51. user="benchmarkdbuser",
  52. password="benchmarkdbpass",
  53. host="tfb-database",
  54. port=5432,
  55. )
  56. template = load_fortunes_template()
  57. read_row_sql = (
  58. 'SELECT world."randomnumber", world."id" FROM "world" WHERE id = %(id)s'
  59. )
  60. prepared_read_row_sql = (
  61. 'SELECT world."randomnumber", world."id" FROM "world" WHERE id = $1'
  62. )
  63. write_row_sql = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
  64. additional_row = (0, "Additional fortune added at request time.")
  65. db = pool.getconn()
  66. cursor = db.cursor()
  67. cursor.execute("PREPARE read_stmt (int) AS " + prepared_read_row_sql)
  68. cursor.execute("PREPARE write_stmt (int, int) AS " + write_row_sql)
  69. cursor.execute('PREPARE fortune AS SELECT * FROM "Fortune"')
  70. del cursor
  71. pool.putconn(db)
  72. del db
  73. return (
  74. pool,
  75. template,
  76. read_row_sql,
  77. prepared_read_row_sql,
  78. write_row_sql,
  79. additional_row,
  80. )
  81. def query(arg):
  82. (cursor, ident) = arg
  83. cursor.execute("EXECUTE read_stmt(%s)", [ident])
  84. result = cursor.fetchone()
  85. return result
  86. Fortune = namedtuple("Fortune", ["id", "message"])