|
@@ -0,0 +1,72 @@
|
|
|
+from bottle import Bottle, route, request, run
|
|
|
+from bottle.ext import sqlalchemy
|
|
|
+from sqlalchemy import create_engine, Column, Integer
|
|
|
+from sqlalchemy.ext.declarative import declarative_base
|
|
|
+from random import randint
|
|
|
+import ujson
|
|
|
+
|
|
|
+app = Bottle()
|
|
|
+app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://benchmarkdbuser:benchmarkdbpass@DBHOSTNAME:3306/hello_world'
|
|
|
+Base = declarative_base()
|
|
|
+db_engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
|
|
|
+plugin = sqlalchemy.Plugin(db_engine, keyword='db', )
|
|
|
+app.install(plugin)
|
|
|
+
|
|
|
+
|
|
|
+class World(Base):
|
|
|
+ __tablename__ = "World"
|
|
|
+ id = Column(Integer, primary_key=True)
|
|
|
+ randomNumber = Column(Integer)
|
|
|
+
|
|
|
+ # http://stackoverflow.com/questions/7102754/jsonify-a-sqlalchemy-result-set-in-flask
|
|
|
+ @property
|
|
|
+ def serialize(self):
|
|
|
+ """Return object data in easily serializeable format"""
|
|
|
+ return {
|
|
|
+ 'id' : self.id,
|
|
|
+ 'randomNumber': self.randomNumber
|
|
|
+ }
|
|
|
+
|
|
|
[email protected]("/json")
|
|
|
+def hello():
|
|
|
+ resp = {"message": "Hello, World!"}
|
|
|
+ return ujson.dumps(resp)
|
|
|
+
|
|
|
[email protected]("/db")
|
|
|
+def get_random_world(db):
|
|
|
+ num_queries = request.query.queries or '1'
|
|
|
+ worlds = []
|
|
|
+ for i in range(int(num_queries)):
|
|
|
+ wid = randint(1, 10000)
|
|
|
+ worlds.append(db.query(World).get(wid).serialize)
|
|
|
+ return ujson.dumps(worlds)
|
|
|
+
|
|
|
[email protected]("/dbs")
|
|
|
+def get_random_world_single(db):
|
|
|
+ wid = randint(1, 10000)
|
|
|
+ worlds = [db.query(World).get(wid).serialize]
|
|
|
+ return ujson.dumps(worlds)
|
|
|
+
|
|
|
[email protected]("/dbraw")
|
|
|
+def get_random_world_raw():
|
|
|
+ connection = db_engine.connect()
|
|
|
+ num_queries = request.query.queries or '1'
|
|
|
+ worlds = []
|
|
|
+ for i in range(int(num_queries)):
|
|
|
+ wid = randint(1, 10000)
|
|
|
+ result = connection.execute("SELECT * FROM world WHERE id = " + str(wid)).fetchone()
|
|
|
+ worlds.append({'id': result[0], 'randomNumber': result[1]})
|
|
|
+ connection.close()
|
|
|
+ return ujson.dumps(worlds)
|
|
|
+
|
|
|
[email protected]("/dbsraw")
|
|
|
+def get_random_world_single_raw():
|
|
|
+ connection = db_engine.connect()
|
|
|
+ wid = randint(1, 10000)
|
|
|
+ result = connection.execute("SELECT * FROM world WHERE id = " + str(wid)).fetchone()
|
|
|
+ worlds = [{'id': result[0], 'randomNumber': result[1]}]
|
|
|
+ connection.close()
|
|
|
+ return ujson.dumps(worlds)
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ app.run()
|