Browse Source

Add '-raw' for Flask (uses SQLAlchemy core for connection pooling
but not the ORM.

Tweak the single query code slightly for both normal (ORM) and
raw (non-ORM)

seanaedmiston 12 years ago
parent
commit
395ef9fca7
3 changed files with 52 additions and 6 deletions
  1. 11 3
      flask/README.md
  2. 32 1
      flask/app.py
  3. 9 2
      flask/benchmark_config

+ 11 - 3
flask/README.md

@@ -10,8 +10,16 @@ http://localhost:8080/json
 
 ### Single Row Random Query
 
-http://localhost:8080/db
+With ORM:
+    http://localhost:8080/dbs
 
-### Variable Row Query Test
+Without ORM (raw):
+    http://localhost:8080/dbsraw
 
-http://localhost:8080/db?queries=2
+### Variable Row Query Test 
+
+With ORM:
+    http://localhost:8080/db?queries=2
+
+Without ORM (raw):
+    http://localhost:8080/dbraw?queries=2

+ 32 - 1
flask/app.py

@@ -1,10 +1,12 @@
 from flask import Flask, jsonify, request
 from flask.ext.sqlalchemy import SQLAlchemy
+from sqlalchemy import create_engine
 from random import randint
 
 app = Flask(__name__)
-app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://benchmarkdbuser:benchmarkdbpass@DBHOSTNAME:3306/hello_world'
+app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://benchmarkdbuser:benchmarkdbpass@192.168.0.12:3306/hello_world'
 db = SQLAlchemy(app)
+dbraw_engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
 
 class World(db.Model):
   __tablename__ = "World"
@@ -33,6 +35,35 @@ def get_random_world():
     wid = randint(1, 10000)
     worlds.append(World.query.get(wid).serialize)
   return jsonify(worlds=worlds)
+
[email protected]("/dbs")
+def get_random_world_single():
+  worlds = []
+  wid = randint(1, 10000)
+  worlds.append(World.query.get(wid).serialize)
+  return jsonify(worlds=worlds)
   
[email protected]("/dbraw")
+def get_random_world_raw():
+  connection = dbraw_engine.connect()
+  num_queries = request.args.get("queries", 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 jsonify(worlds=worlds)
+
[email protected]("/dbsraw")
+def get_random_world_single_raw():
+  connection = dbraw_engine.connect()
+  worlds = []
+  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 jsonify(worlds=worlds)
+
 if __name__ == "__main__":
     app.run()

+ 9 - 2
flask/benchmark_config

@@ -4,10 +4,17 @@
     "default": {
       "setup_file": "setup",
       "json_url": "/json",
-      "db_url": "/db",
+      "db_url": "/dbs",
       "query_url": "/db?queries=",
       "port": 8080,
       "sort": 31
+    },
+    "mysql-raw": {
+      "setup_file": "setup",
+      "db_url": "/dbsraw",
+      "query_url": "/dbraw?queries=",
+      "port": 8080,
+      "sort": 82
     }
   }]
-}
+}