Explorar o código

Merge branch 'bottle-fortune' of https://github.com/seanaedmiston/FrameworkBenchmarks into seanaedmiston-bottle-fortune

Patrick Falls %!s(int64=12) %!d(string=hai) anos
pai
achega
3a4df5a295
Modificáronse 6 ficheiros con 77 adicións e 26 borrados
  1. 10 1
      bottle/README.md
  2. 30 24
      bottle/app.py
  3. 2 1
      bottle/benchmark_config
  4. 13 0
      bottle/views/fortune-obj.tpl
  5. 13 0
      bottle/views/fortune.tpl
  6. 9 0
      bottle/views/layout.tpl

+ 10 - 1
bottle/README.md

@@ -22,4 +22,13 @@ With ORM:
     http://localhost:8080/db?queries=2
 
 Without ORM (raw):
-    http://localhost:8080/dbraw?queries=2
+    http://localhost:8080/dbraw?queries=2
+
+### Fortune Test
+
+With ORM:
+    http://localhost:8080/fortune
+
+Without ORM (raw):
+    http://localhost:8080/fortuneraw
+

+ 30 - 24
bottle/app.py

@@ -1,12 +1,14 @@
-from bottle import Bottle, route, request, run
+from bottle import Bottle, route, request, run, template
 from bottle.ext import sqlalchemy 
-from sqlalchemy import create_engine, Column, Integer
+from sqlalchemy import create_engine, Column, Integer, Unicode
 from sqlalchemy.ext.declarative import declarative_base
 from random import randint
 import ujson
+from operator import attrgetter, itemgetter
+from functools import partial
 
 app = Bottle()
-app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://benchmarkdbuser:benchmarkdbpass@localhost:3306/hello_world'
+app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://benchmarkdbuser:benchmarkdbpass@DBHOSTNAME:3306/hello_world?charset=utf8'
 Base = declarative_base()
 db_engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
 plugin = sqlalchemy.Plugin(db_engine, keyword='db', )
@@ -27,6 +29,11 @@ class World(Base):
          'randomNumber': self.randomNumber
      }
 
+class Fortune(Base):
+  __tablename__ = "Fortune"
+  id = Column(Integer, primary_key=True)
+  message = Column(Unicode)
+
 @app.route("/json")
 def hello():
   resp = {"message": "Hello, World!"}
@@ -36,9 +43,9 @@ def hello():
 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)
+  rp = partial(randint, 10000)
+  for i in xrange(int(num_queries)):
+    worlds.append(db.query(World).get(rp()).serialize)
   return ujson.dumps(worlds)
 
 @app.route("/dbs")
@@ -52,9 +59,9 @@ def get_random_world_raw():
   connection = db_engine.connect()
   num_queries = request.query.queries or '1'
   worlds = []
+  rp = partial(randint, 10000)
   for i in range(int(num_queries)):
-    wid = randint(1, 10000)
-    result = connection.execute("SELECT * FROM world WHERE id = " + str(wid)).fetchone()
+    result = connection.execute("SELECT * FROM world WHERE id = " + str(rp())).fetchone()
     worlds.append({'id': result[0], 'randomNumber': result[1]})
   connection.close()
   return ujson.dumps(worlds)
@@ -68,22 +75,21 @@ def get_random_world_single_raw():
   connection.close()
   return ujson.dumps(worlds)
 
[email protected]("/update")
-def update_worlds(db):
-  num_queries = int(request.query.queries or '1')
-  if num_queries < 1:
-    num_queries = 1
-  elif num_queries > 500:
-    num_queries = 500
[email protected]("/fortune")
+def fortune_orm(db):
+  fortunes=db.query(Fortune).all()
+  fortunes.append(Fortune(message="Additional fortune added at request time."))
+  fortunes=sorted(fortunes, key=attrgetter('message'))
+  return template('fortune-obj', fortunes=fortunes)
 
-  worlds = []
-  for i in range(num_queries):
-    wid = randint(1, 10000)
-    world = db.query(World).get(wid)
-    world.randomNumber = randint(1, 10000)
-    db.commit()
-    worlds.append(world.serialize)
-  return ujson.dumps(worlds)
+@app.route("/fortuneraw")
+def fortune_raw():
+  connection = db_engine.connect()
+  fortunes=[(f.id, f.message) for f in connection.execute("SELECT * FROM Fortune")]
+  fortunes.append((0L, u'Additional fortune added at request time.'))
+  fortunes=sorted(fortunes, key=itemgetter(1))
+  connection.close()
+  return template('fortune', fortunes=fortunes)
 
 if __name__ == "__main__":
-    app.run()
+    app.run(host='0.0.0.0', debug=True)

+ 2 - 1
bottle/benchmark_config

@@ -6,7 +6,7 @@
       "json_url": "/json",
       "db_url": "/dbs",
       "query_url": "/db?queries=",
-      "update_url": "/update?queries=",
+      "fortune_url": "/fortune",
       "port": 8080,
       "sort": 88
     },
@@ -14,6 +14,7 @@
       "setup_file": "setup",
       "db_url": "/dbsraw",
       "query_url": "/dbraw?queries=",
+      "fortune_url": "/fortuneraw",
       "port": 8080,
       "sort": 89
     }

+ 13 - 0
bottle/views/fortune-obj.tpl

@@ -0,0 +1,13 @@
+<table>
+<tr>
+<th>id</th>
+<th>message</th>
+</tr>
+% for fortune in fortunes:
+<tr>
+<td>{{ fortune.id }}</td>
+<td>{{ fortune.message }}</td>
+</tr>
+% end
+</table>
+%rebase layout

+ 13 - 0
bottle/views/fortune.tpl

@@ -0,0 +1,13 @@
+<table>
+<tr>
+<th>id</th>
+<th>message</th>
+</tr>
+% for fortune in fortunes:
+<tr>
+<td>{{ fortune[0] }}</td>
+<td>{{ fortune[1] }}</td>
+</tr>
+% end
+</table>
+%rebase layout

+ 9 - 0
bottle/views/layout.tpl

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Fortunes</title>
+</head>
+<body>
+  %include
+</body>
+</html>