app.py 2.7 KB

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