app.py 2.8 KB

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