app.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import os
  2. from operator import itemgetter
  3. from random import randint
  4. import asyncpg
  5. from crax import Crax
  6. from crax.response_types import BaseResponse, JSONResponse
  7. from crax.urls import Route, Url
  8. from crax.views import JSONView, TemplateView
  9. READ_ROW_SQL = 'SELECT "id", "randomnumber" FROM "world" WHERE id = $1'
  10. WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
  11. async def setup_database():
  12. global connection_pool
  13. connection_pool = await asyncpg.create_pool(
  14. user=os.getenv('PGUSER', 'benchmarkdbuser'),
  15. password=os.getenv('PGPASS', 'benchmarkdbpass'),
  16. database='hello_world',
  17. host='tfb-database',
  18. port=5432
  19. )
  20. def get_num_queries(request):
  21. try:
  22. query_count = int(request.query["queries"][0])
  23. except (KeyError, IndexError, ValueError):
  24. return 1
  25. if query_count < 1:
  26. return 1
  27. if query_count > 500:
  28. return 500
  29. return query_count
  30. class TestSingleQuery(JSONView):
  31. async def get(self):
  32. row_id = randint(1, 10000)
  33. async with connection_pool.acquire() as connection:
  34. if self.request.path == '/db':
  35. res = await connection.fetchval(READ_ROW_SQL, row_id)
  36. self.context = {'id': row_id, 'randomNumber': res}
  37. class TestMultiQueries(JSONView):
  38. async def get(self):
  39. row_ids = [randint(1, 10000) for _ in range(get_num_queries(self.request))]
  40. worlds = []
  41. async with connection_pool.acquire() as connection:
  42. statement = await connection.prepare(READ_ROW_SQL)
  43. for row_id in row_ids:
  44. number = await statement.fetchval(row_id)
  45. worlds.append({'id': row_id, 'randomNumber': number})
  46. self.context = worlds
  47. class TestUpdates(JSONView):
  48. async def get(self):
  49. updates = [(randint(1, 10000), randint(1, 10000)) for _ in range(get_num_queries(self.request))]
  50. worlds = [{'id': row_id, 'randomNumber': number} for row_id, number in updates]
  51. async with connection_pool.acquire() as connection:
  52. statement = await connection.prepare(READ_ROW_SQL)
  53. for row_id, number in updates:
  54. await statement.fetchval(row_id)
  55. await connection.executemany(WRITE_ROW_SQL, updates)
  56. self.context = worlds
  57. class TestSingleFortunes(TemplateView):
  58. template = "fortune.html"
  59. async def get(self):
  60. async with connection_pool.acquire() as connection:
  61. fortunes = await connection.fetch('SELECT * FROM Fortune')
  62. fortunes.append([0, 'Additional fortune added at request time.'])
  63. fortunes.sort(key=itemgetter(1))
  64. self.context["fortunes"] = fortunes
  65. APPLICATIONS = ["hello"]
  66. URL_PATTERNS = [
  67. Route(Url('/json'), JSONResponse(None, {'message': 'Hello, world!'})),
  68. Route(Url('/plaintext'), BaseResponse(None, b'Hello, world!')),
  69. Route(Url('/db'), TestSingleQuery),
  70. Route(Url('/queries'), TestMultiQueries),
  71. Route(Url('/updates'), TestUpdates),
  72. Route(Url('/fortunes'), TestSingleFortunes)
  73. ]
  74. app = Crax('hello.app', debug=True, on_startup=setup_database)