app.py 3.1 KB

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