app.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 id,randomnumber from world where id = $1"
  8. UPDATE_WORLD = "update world set randomNumber = $2 where id = $1"
  9. @app.before_serving
  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.after_serving
  19. async def disconnect_from_db():
  20. await app.db.close()
  21. @app.route("/json")
  22. async def json():
  23. return {"message": "Hello, World!"}
  24. @app.route("/plaintext")
  25. async def plaintext():
  26. response = await make_response(b"Hello, World!")
  27. # Quart assumes string responses are 'text/html', so make a custom one
  28. response.mimetype = "text/plain"
  29. return response
  30. @app.route("/db")
  31. async def db():
  32. async with app.db.acquire() as conn:
  33. key = random.randint(1, 10000)
  34. number = await conn.fetchval(GET_WORLD, key)
  35. return jsonify({"id": key, "randomNumber": number})
  36. def get_query_count(args):
  37. qc = args.get("queries")
  38. if qc is None:
  39. return 1
  40. try:
  41. qc = int(qc)
  42. except ValueError:
  43. return 1
  44. qc = max(qc, 1)
  45. qc = min(qc, 500)
  46. return qc
  47. @app.route("/queries")
  48. async def queries():
  49. queries = get_query_count(request.args)
  50. worlds = []
  51. async with app.db.acquire() as conn:
  52. pst = await conn.prepare(GET_WORLD)
  53. for _ in range(queries):
  54. key = random.randint(1, 10000)
  55. number = await pst.fetchval(key)
  56. worlds.append({"id": key, "randomNumber": number})
  57. return jsonify(worlds)
  58. @app.route("/updates")
  59. async def updates():
  60. queries = get_query_count(request.args)
  61. new_worlds = []
  62. async with app.db.acquire() as conn, conn.transaction():
  63. pst = await conn.prepare(GET_WORLD)
  64. for _ in range(queries):
  65. key = random.randint(1, 10000)
  66. old_number = await pst.fetchval(key)
  67. new_number = random.randint(1, 10000)
  68. new_worlds.append((key, new_number))
  69. await conn.executemany(UPDATE_WORLD, new_worlds)
  70. return jsonify(
  71. [{"id": key, "randomNumber": new_number} for key, new_number in new_worlds]
  72. )
  73. @app.route("/fortunes")
  74. async def fortunes():
  75. async with app.db.acquire() as conn:
  76. rows = await conn.fetch("select * from fortune")
  77. rows.append((0, "Additional fortune added at request time."))
  78. rows.sort(key=lambda row: row[1])
  79. return await render_template("fortunes.html", fortunes=rows)
  80. if __name__ == "__main__":
  81. app.run(host="0.0.0.0", port=8080)