app.py 3.8 KB

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