app.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import os
  2. import sys
  3. from functools import partial
  4. from operator import attrgetter
  5. from random import randint
  6. import json
  7. import bleach
  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. return "Hello, world!"
  49. @cherrypy.expose
  50. @cherrypy.tools.json_out()
  51. def db(self):
  52. cherrypy.response.headers["Content-Type"] = "application/json"
  53. wid = randint(1, 10000)
  54. world = cherrypy.request.db.query(World).get(wid).serialize()
  55. return world
  56. @cherrypy.expose
  57. @cherrypy.tools.json_out()
  58. def queries(self, queries=1):
  59. num_queries = getQueryNum(queries)
  60. if num_queries < 1:
  61. num_queries = 1
  62. if num_queries > 500:
  63. num_queries = 500
  64. rp = partial(randint, 1, 10000)
  65. get = cherrypy.request.db.query(World).get
  66. worlds = [get(rp()).serialize() for _ in xrange(num_queries)]
  67. return worlds
  68. @cherrypy.expose
  69. @cherrypy.tools.json_out()
  70. def updates(self, queries=1):
  71. cherrypy.response.headers["Content-Type"] = "application/json"
  72. num_queries = getQueryNum(queries)
  73. if num_queries < 1:
  74. num_queries = 1
  75. if num_queries > 500:
  76. num_queries = 500
  77. worlds = []
  78. rp = partial(randint, 1, 10000)
  79. ids = [rp() for _ in xrange(num_queries)]
  80. ids.sort() # To avoid deadlock
  81. for id in ids:
  82. world = cherrypy.request.db.query(World).get(id)
  83. world.randomNumber = rp()
  84. worlds.append(world.serialize())
  85. cherrypy.request.db.commit()
  86. return worlds
  87. @cherrypy.expose
  88. def fortune(self):
  89. fortunes = cherrypy.request.db.query(Fortune).all()
  90. fortunes.append(Fortune(id=0, message="Additional fortune added at request time."))
  91. fortunes.sort(key=attrgetter("message"))
  92. html = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>"
  93. for f in fortunes:
  94. html += "<tr><td>" + str(f.id) + "</td><td>" + bleach.clean(f.message) + "</td></tr>"
  95. html += "</table></body></html>"
  96. return html
  97. if __name__ == "__main__":
  98. # Register the SQLAlchemy plugin
  99. from saplugin import SAEnginePlugin
  100. DBDRIVER = 'mysql'
  101. DATABASE_URI = '%s://benchmarkdbuser:[email protected]:3306/hello_world?charset=utf8' % (DBDRIVER)
  102. SAEnginePlugin(cherrypy.engine, DATABASE_URI).subscribe()
  103. # Register the SQLAlchemy tool
  104. from satool import SATool
  105. cherrypy.tools.db = SATool()
  106. cherrypy.server.socket_host = '0.0.0.0'
  107. cherrypy.quickstart(CherryPyBenchmark(), '', {'/': {'tools.db.on': True}})