app.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import cgi
  2. import os
  3. import sys
  4. from functools import partial
  5. from operator import attrgetter
  6. from random import randint
  7. import json
  8. import cherrypy
  9. from sqlalchemy.ext.declarative import declarative_base
  10. from sqlalchemy import Column
  11. from sqlalchemy.types import String, Integer
  12. Base = declarative_base()
  13. if sys.version_info[0] == 3:
  14. xrange = range
  15. def getQueryNum(queryString):
  16. try:
  17. int(queryString)
  18. return int(queryString)
  19. except ValueError:
  20. return 1
  21. class Fortune(Base):
  22. __tablename__ = "fortune"
  23. id = Column(Integer, primary_key = True)
  24. message = Column(String)
  25. def serialize(self):
  26. return {
  27. 'id' : self.id,
  28. 'message' : self.message
  29. }
  30. class World(Base):
  31. __tablename__ = "world"
  32. id = Column(Integer, primary_key = True)
  33. randomNumber = Column(Integer)
  34. def serialize(self):
  35. return {
  36. 'id' : self.id,
  37. 'randomNumber' : self.randomNumber
  38. }
  39. class CherryPyBenchmark(object):
  40. @cherrypy.expose
  41. @cherrypy.tools.json_out()
  42. def json(self):
  43. cherrypy.response.headers["Content-Type"] = "application/json"
  44. json_message = {"message" : "Hello, world!"}
  45. return json_message
  46. @cherrypy.expose
  47. def plaintext(self):
  48. cherrypy.response.headers["Content-Type"] = "text/plain"
  49. return "Hello, world!"
  50. @cherrypy.expose
  51. @cherrypy.tools.json_out()
  52. def db(self):
  53. cherrypy.response.headers["Content-Type"] = "application/json"
  54. wid = randint(1, 10000)
  55. world = cherrypy.request.db.query(World).get(wid).serialize()
  56. return world
  57. @cherrypy.expose
  58. @cherrypy.tools.json_out()
  59. def queries(self, queries=1):
  60. num_queries = getQueryNum(queries)
  61. if num_queries < 1:
  62. num_queries = 1
  63. if num_queries > 500:
  64. num_queries = 500
  65. rp = partial(randint, 1, 10000)
  66. get = cherrypy.request.db.query(World).get
  67. worlds = [get(rp()).serialize() for _ in xrange(num_queries)]
  68. return worlds
  69. @cherrypy.expose
  70. @cherrypy.tools.json_out()
  71. def updates(self, queries=1):
  72. cherrypy.response.headers["Content-Type"] = "application/json"
  73. num_queries = getQueryNum(queries)
  74. if num_queries < 1:
  75. num_queries = 1
  76. if num_queries > 500:
  77. num_queries = 500
  78. worlds = []
  79. rp = partial(randint, 1, 10000)
  80. ids = [rp() for _ in xrange(num_queries)]
  81. ids.sort() # To avoid deadlock
  82. for id in ids:
  83. world = cherrypy.request.db.query(World).get(id)
  84. world.randomNumber = rp()
  85. worlds.append(world.serialize())
  86. cherrypy.request.db.commit()
  87. return worlds
  88. @cherrypy.expose
  89. def fortune(self):
  90. fortunes = cherrypy.request.db.query(Fortune).all()
  91. fortunes.append(Fortune(id=0, message="Additional fortune added at request time."))
  92. fortunes.sort(key=attrgetter("message"))
  93. html = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>"
  94. for f in fortunes:
  95. html += "<tr><td>" + str(f.id) + "</td><td>" + cgi.escape(f.message) + "</td></tr>"
  96. html += "</table></body></html>"
  97. return html
  98. if __name__ == "__main__":
  99. # Register the SQLAlchemy plugin
  100. from saplugin import SAEnginePlugin
  101. DBDRIVER = 'mysql'
  102. DATABASE_URI = '%s://benchmarkdbuser:benchmarkdbpass@TFB-database:3306/hello_world?charset=utf8' % (DBDRIVER)
  103. SAEnginePlugin(cherrypy.engine, DATABASE_URI).subscribe()
  104. # Register the SQLAlchemy tool
  105. from satool import SATool
  106. cherrypy.tools.db = SATool()
  107. cherrypy.server.socket_host = '0.0.0.0'
  108. cherrypy.quickstart(CherryPyBenchmark(), '', {'/': {'tools.db.on': True, 'log.screen': False, 'log.access_file': ''}})