app.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. import random
  3. import os
  4. import asyncpg
  5. from quart import Quart, jsonify, make_response, request, render_template
  6. app = Quart(__name__)
  7. GET_WORLD = "select randomnumber from world where id = $1"
  8. UPDATE_WORLD = "update world set randomNumber = $2 where id = $1"
  9. @app.before_first_request
  10. async def connect_to_db():
  11. app.db = await asyncpg.create_pool(
  12. user=os.getenv("PGUSER", "benchmarkdbuser"),
  13. password=os.getenv("PGPASS", "benchmarkdbpass"),
  14. database="hello_world",
  15. host="tfb-database",
  16. port=5432,
  17. )
  18. @app.route("/json")
  19. def json():
  20. return jsonify(message="Hello, World!")
  21. @app.route("/plaintext")
  22. async def plaintext():
  23. response = await make_response(b"Hello, World!")
  24. # Quart assumes string responses are 'text/html', so make a custom one
  25. response.mimetype = "text/plain"
  26. return response
  27. @app.route("/db")
  28. async def db():
  29. async with app.db.acquire() as conn:
  30. key = random.randint(1, 10000)
  31. number = await conn.fetchval(GET_WORLD, key)
  32. return jsonify({"id": key, "randomNumber": number})
  33. def get_query_count(args):
  34. qc = args.get("queries")
  35. if qc is None:
  36. return 1
  37. try:
  38. qc = int(qc)
  39. except ValueError:
  40. return 1
  41. qc = max(qc, 1)
  42. qc = min(qc, 500)
  43. return qc
  44. @app.route("/queries")
  45. async def queries():
  46. queries = get_query_count(request.args)
  47. worlds = []
  48. async with app.db.acquire() as conn:
  49. pst = await conn.prepare(GET_WORLD)
  50. for _ in range(queries):
  51. key = random.randint(1, 10000)
  52. number = await pst.fetchval(key)
  53. worlds.append({"id": key, "randomNumber": number})
  54. return jsonify(worlds)
  55. @app.route("/updates")
  56. async def updates():
  57. queries = get_query_count(request.args)
  58. new_worlds = []
  59. async with app.db.acquire() as conn, conn.transaction():
  60. pst = await conn.prepare(GET_WORLD)
  61. for _ in range(queries):
  62. key = random.randint(1, 10000)
  63. old_number = await pst.fetchval(key)
  64. new_number = random.randint(1, 10000)
  65. new_worlds.append((key, new_number))
  66. await conn.executemany(UPDATE_WORLD, new_worlds)
  67. return jsonify(
  68. [{"id": key, "randomNumber": new_number} for key, new_number in new_worlds]
  69. )
  70. @app.route("/fortunes")
  71. async def fortunes():
  72. async with app.db.acquire() as conn:
  73. rows = await conn.fetch("select * from fortune")
  74. rows.append((0, "Additional fortune added at request time."))
  75. rows.sort(key=lambda row: row[1])
  76. return await render_template("fortunes.html", fortunes=rows)
  77. if __name__ == "__main__":
  78. app.run(host="0.0.0.0", port=8080)