Browse Source

Merge branch 'tornado-postgres' of https://github.com/maticz/FrameworkBenchmarks into 671

James Yen 11 years ago
parent
commit
444ee4a7e6
4 changed files with 105 additions and 1 deletions
  1. 2 0
      toolset/setup/linux/installer.py
  2. 21 0
      tornado/benchmark_config
  3. 50 1
      tornado/server.py
  4. 32 0
      tornado/setup_pg.py

+ 2 - 0
toolset/setup/linux/installer.py

@@ -424,6 +424,8 @@ class Installer:
     easy_install('tornado==3.1', two=True, three=True, pypy=True)
     easy_install('tornado==3.1', two=True, three=True, pypy=True)
     easy_install('motor==0.1.2', two=True, three=True, pypy=True)
     easy_install('motor==0.1.2', two=True, three=True, pypy=True)
     easy_install('pymongo==2.5.2', two=True, three=True, pypy=True)
     easy_install('pymongo==2.5.2', two=True, three=True, pypy=True)
+    easy_install('psycopg2==2.5.3', two=True, three=True, pypy=True)
+    easy_install('Momoko==1.1.3', two=True, three=True, pypy=True)
 
 
     # Django
     # Django
     easy_install("https://www.djangoproject.com/download/1.6/tarball/", two=True, three=True, pypy=True)
     easy_install("https://www.djangoproject.com/download/1.6/tarball/", two=True, three=True, pypy=True)

+ 21 - 0
tornado/benchmark_config

@@ -22,6 +22,27 @@
       "notes": "CPython 2.7",
       "notes": "CPython 2.7",
       "versus": ""
       "versus": ""
     },
     },
+    "postgresql-raw": {
+      "setup_file": "setup_pg",
+      "json_url": "/json",
+      "db_url": "/dbraw",
+      "query_url": "/queriesraw?queries=",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "Postgres",
+      "framework": "tornado",
+      "language": "Python",
+      "orm": "Raw",
+      "platform": "Tornado",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "tornado",
+      "notes": "CPython 2.7",
+      "versus": ""
+    },
     "py3": {
     "py3": {
       "setup_file": "setup_py3",
       "setup_file": "setup_py3",
       "json_url": "/json",
       "json_url": "/json",

+ 50 - 1
tornado/server.py

@@ -4,6 +4,7 @@ import sys
 import json
 import json
 from random import randint
 from random import randint
 
 
+import momoko
 import motor
 import motor
 import tornado.ioloop
 import tornado.ioloop
 import tornado.web
 import tornado.web
@@ -19,6 +20,7 @@ if sys.version_info[0] == 3:
 
 
 tornado.options.define('port', default=8888, type=int, help="Server port")
 tornado.options.define('port', default=8888, type=int, help="Server port")
 tornado.options.define('mongo', default='localhost', type=str, help="MongoDB host")
 tornado.options.define('mongo', default='localhost', type=str, help="MongoDB host")
+tornado.options.define('postgres', default=None, type=str, help="PostgreSQL host")
 
 
 
 
 class BaseHandler(tornado.web.RequestHandler):
 class BaseHandler(tornado.web.RequestHandler):
@@ -72,11 +74,54 @@ class QueryTestHandler(BaseHandler):
         self.write(response)
         self.write(response)
 
 
 
 
+class QueryPostgresRawTestHandler(BaseHandler):
+    @gen.coroutine
+    def get(self):
+        sql = "SELECT id, randomNumber FROM World WHERE id=%s"
+
+        random_id = randint(1, 10000)
+        cursor = yield momoko.Op(
+            self.application.db.execute, sql, (random_id,)
+        )
+        row = cursor.fetchone()
+        response = json.dumps({"id": row[0], "randomNumber": row[1]})
+
+        self.set_header("Content-Type", "application/json; charset=UTF-8")
+        self.write(response)
+
+
+class MultipleQueriesPostgresRawTestHandler(BaseHandler):
+    @gen.coroutine
+    def get(self):
+        queries = self.get_argument("queries", "1")
+        try:
+            queries = int(queries.strip())
+        except ValueError:
+            queries = 1
+
+        queries = min(max(1, queries), 500)
+
+        sql = "SELECT id, randomNumber FROM World WHERE id=%s"
+
+        worlds = []
+        for i in xrange(int(queries)):
+            random_id = randint(1, 10000)
+            cursor = yield momoko.Op(
+                self.application.db.execute, sql, (random_id,)
+            )
+            row = cursor.fetchone()
+            worlds.append({"id": row[0], "randomNumber": row[1]})
+        response = json.dumps(worlds)
+        self.set_header("Content-Type", "application/json; charset=UTF-8")
+        self.write(response)
+
 application = tornado.web.Application([
 application = tornado.web.Application([
     (r"/json", JsonSerializeTestHandler),
     (r"/json", JsonSerializeTestHandler),
     (r"/plaintext", PlaintextHandler),
     (r"/plaintext", PlaintextHandler),
     (r"/db", DBTestHandler),
     (r"/db", DBTestHandler),
     (r"/queries", QueryTestHandler),
     (r"/queries", QueryTestHandler),
+    (r"/dbraw", QueryPostgresRawTestHandler),
+    (r"/queriesraw", MultipleQueriesPostgresRawTestHandler)
 ])
 ])
 
 
 
 
@@ -85,5 +130,9 @@ if __name__ == "__main__":
     server = tornado.httpserver.HTTPServer(application)
     server = tornado.httpserver.HTTPServer(application)
     server.bind(options.port)
     server.bind(options.port)
     server.start(0)
     server.start(0)
-    db = motor.MotorClient(options.mongo).open_sync().hello_world
+    if options.postgres:
+        dsn = "user=benchmarkdbuser password=benchmarkdbpass dbname=hello_world host=%s" % options.postgres
+        application.db = momoko.Pool(dsn, size=1)
+    else:
+        db = motor.MotorClient(options.mongo).open_sync().hello_world
     tornado.ioloop.IOLoop.instance().start()
     tornado.ioloop.IOLoop.instance().start()

+ 32 - 0
tornado/setup_pg.py

@@ -0,0 +1,32 @@
+import os
+import subprocess
+import sys
+import time
+
+
+bin_dir = os.path.expanduser('~/FrameworkBenchmarks/installs/py2/bin')
+python = os.path.expanduser(os.path.join(bin_dir, 'python'))
+pip = os.path.expanduser(os.path.join(bin_dir, 'pip'))
+cwd = os.path.expanduser('~/FrameworkBenchmarks/tornado')
+
+
+def start(args, logfile, errfile):
+    subprocess.Popen(
+        python + ' server.py --port=8080 --postgres=%s --logging=error' % (args.database_host,),
+        shell=True, cwd=cwd, stderr=errfile, stdout=logfile)
+    return 0
+
+
+def stop(logfile, errfile):
+    for line in subprocess.check_output(['ps', 'aux']).splitlines():
+        if 'server.py --port=8080' in line:
+            pid = int(line.split(None, 2)[1])
+            os.kill(pid, 9)
+    return 0
+
+if __name__ == '__main__':
+    class DummyArg:
+        database_host = 'localhost'
+    start(DummyArg(), sys.stderr, sys.stderr)
+    time.sleep(1)
+    stop(sys.stderr, sys.stderr)