app.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 cherrypy
  8. from sqlalchemy.ext.declarative import declarative_base
  9. from sqlalchemy import Column
  10. from sqlalchemy.types import String, Integer
  11. Base = declarative_base()
  12. if sys.version_info[0] == 3:
  13. xrange = range
  14. def getQueryNum(queryString):
  15. try:
  16. int(queryString)
  17. return int(queryString)
  18. except ValueError:
  19. return 1
  20. class Fortune(Base):
  21. __tablename__ = "fortune"
  22. id = Column(Integer, primary_key = True)
  23. message = Column(String)
  24. class World(Base):
  25. __tablename__ = "world"
  26. id = Column(Integer, primary_key = True)
  27. randomNumber = Column(Integer)
  28. def serialize(self):
  29. return {
  30. 'id' : self.id,
  31. 'randomNumber' : self.randomNumber
  32. }
  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. return "Hello, world!"
  43. @cherrypy.expose
  44. @cherrypy.tools.json_out()
  45. def db(self):
  46. cherrypy.response.headers["Content-Type"] = "application/json"
  47. wid = randint(1, 10000)
  48. world = cherrypy.request.db.query(World).get(wid).serialize()
  49. return world
  50. @cherrypy.expose
  51. @cherrypy.tools.json_out()
  52. def queries(self, queries=1):
  53. num_queries = getQueryNum(queries)
  54. if num_queries < 1:
  55. num_queries = 1
  56. if num_queries > 500:
  57. num_queries = 500
  58. rp = partial(randint, 1, 10000)
  59. get = cherrypy.request.db.query(World).get
  60. worlds = [get(rp()).serialize() for _ in xrange(num_queries)]
  61. return worlds
  62. @cherrypy.expose
  63. @cherrypy.tools.json_out()
  64. def updates(self, queries=1):
  65. cherrypy.response.headers["Content-Type"] = "application/json"
  66. num_queries = getQueryNum(queries)
  67. if num_queries < 1:
  68. num_queries = 1
  69. if num_queries > 500:
  70. num_queries = 500
  71. worlds = []
  72. rp = partial(randint, 1, 10000)
  73. ids = [rp() for _ in xrange(num_queries)]
  74. ids.sort() # To avoid deadlock
  75. for id in ids:
  76. world = cherrypy.request.db.query(World).get(id)
  77. world.randomNumber = rp()
  78. worlds.append(world.serialize())
  79. return worlds
  80. if __name__ == "__main__":
  81. # Register the SQLAlchemy plugin
  82. from saplugin import SAEnginePlugin
  83. DBDRIVER = 'mysql'
  84. DBHOSTNAME = os.environ.get('DBHOST', 'localhost')
  85. DATABASE_URI = '%s://benchmarkdbuser:benchmarkdbpass@%s:3306/hello_world?charset=utf8' % (DBDRIVER, DBHOSTNAME)
  86. SAEnginePlugin(cherrypy.engine, DATABASE_URI).subscribe()
  87. # Register the SQLAlchemy tool
  88. from satool import SATool
  89. cherrypy.tools.db = SATool()
  90. cherrypy.quickstart(CherryPyBenchmark(), '', {'/': {'tools.db.on': True}})