app_sync_raw.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. from datetime import datetime
  3. from functools import lru_cache
  4. import os
  5. from random import randint
  6. from microdot_wsgi import Microdot
  7. from microdot_jinja import render_template
  8. import psycopg2
  9. from psycopg2.extras import execute_batch
  10. from cachetools import cached
  11. from cachetools.keys import hashkey
  12. app = Microdot()
  13. db = psycopg2.connect(os.environ['DATABASE_URL'])
  14. get_world_sql = 'SELECT id, randomnumber FROM world WHERE id = $1'
  15. update_world_sql = 'UPDATE world SET randomnumber = $1 WHERE id = $2'
  16. fortune_sql = 'SELECT * FROM fortune'
  17. with db.cursor() as cur:
  18. cur.execute('PREPARE get_world AS ' + get_world_sql)
  19. cur.execute('PREPARE update_world AS ' + update_world_sql)
  20. cur.execute('PREPARE fortune AS ' + fortune_sql)
  21. def get_num_queries(request, name="queries"):
  22. try:
  23. num_queries = request.args.get(name, 1, type=int)
  24. except ValueError:
  25. num_queries = 1
  26. if num_queries < 1:
  27. return 1
  28. if num_queries > 500:
  29. return 500
  30. return num_queries
  31. def generate_ids(num_queries):
  32. ids = {randint(1, 10000) for _ in range(num_queries)}
  33. while len(ids) < num_queries:
  34. ids.add(randint(1, 10000))
  35. return list(ids)
  36. @app.route("/json")
  37. def test_json(request):
  38. return {"message": "Hello, World!"}
  39. @app.route("/db")
  40. def test_db(request):
  41. id = randint(1, 10000)
  42. with db.cursor() as cur:
  43. cur.execute('EXECUTE get_world (%s)', (id,))
  44. result = cur.fetchone()
  45. world = {'id': result[0], 'randomNumber': result[1]}
  46. return world
  47. def get_world(cur, id):
  48. cur.execute('EXECUTE get_world (%s)', (id,))
  49. result = cur.fetchone()
  50. return {'id': result[0], 'randomNumber': result[1]}
  51. @app.route("/queries")
  52. def test_queries(request):
  53. with db.cursor() as cur:
  54. worlds = [get_world(cur, id) for id in generate_ids(get_num_queries(request))]
  55. return worlds
  56. @app.route("/fortunes")
  57. def test_fortunes(request):
  58. with db.cursor() as cur:
  59. cur.execute('EXECUTE fortune')
  60. fortunes = list(cur.fetchall())
  61. fortunes.append((0, 'Additional fortune added at request time.'))
  62. fortunes.sort(key=lambda f: f[1])
  63. return render_template("fortunes_raw.html", fortunes=fortunes), {'Content-Type': 'text/html; charset=utf-8'}
  64. @app.route("/updates")
  65. def test_updates(request):
  66. worlds = []
  67. updated_worlds = []
  68. with db.cursor() as cur:
  69. for id in generate_ids(get_num_queries(request)):
  70. cur.execute('EXECUTE get_world (%s)', (id,))
  71. result = cur.fetchone()
  72. new_value = randint(1, 10000)
  73. updated_worlds.append((new_value, result[0]))
  74. worlds.append({'id': result[0], 'randomNumber': new_value})
  75. execute_batch(cur, 'EXECUTE update_world (%s, %s)', updated_worlds)
  76. db.commit()
  77. return worlds
  78. @app.route("/plaintext")
  79. def test_plaintext(request):
  80. return b"Hello, World!"
  81. @cached(cache={}, key=lambda cur, id: hashkey(id))
  82. def get_cached_world(cur, id):
  83. cur.execute('EXECUTE get_world (%s)', (id,))
  84. result = cur.fetchone()
  85. return {'id': result[0], 'randomNumber': result[1]}
  86. @app.route("/cached-queries")
  87. def test_cached_queries(request):
  88. with db.cursor() as cur:
  89. worlds = [get_cached_world(cur, id) for id in generate_ids(get_num_queries(request, 'count'))]
  90. return worlds