Browse Source

tornado 3 with mongodb raw async

Waldecir Santos 12 years ago
parent
commit
b354501e7c
6 changed files with 176 additions and 0 deletions
  1. 0 0
      tornado/__init__.py
  2. 12 0
      tornado/benchmark_config
  3. 55 0
      tornado/deploy/nginx.conf
  4. 0 0
      tornado/requirements.txt
  5. 65 0
      tornado/server.py
  6. 44 0
      tornado/setup.py

+ 0 - 0
tornado/__init__.py


+ 12 - 0
tornado/benchmark_config

@@ -0,0 +1,12 @@
+{
+  "framework": "tornado3-raw",
+  "tests": [{
+    "default": {
+      "setup_file": "setup.py"
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "port": 8080,
+      "sort": 44
+  }]
+}

+ 55 - 0
tornado/deploy/nginx.conf

@@ -0,0 +1,55 @@
+user nginx;
+worker_processes 1;
+
+error_log /var/log/nginx/error.log;
+pid /var/run/nginx.pid;
+
+events {
+    worker_connections 1024;
+    use epoll;
+}
+
+http {
+    # Enumerate all the Tornado servers here
+    upstream frontends {
+        server 127.0.0.1:8000;
+        server 127.0.0.1:8001;
+        server 127.0.0.1:8002;
+        server 127.0.0.1:8003;
+    }
+
+    include /etc/nginx/mime.types;
+    default_type application/octet-stream;
+
+    # access_log /var/log/nginx/access.log;
+
+    keepalive_timeout 65;
+    proxy_read_timeout 200;
+    sendfile on;
+    tcp_nopush on;
+    tcp_nodelay on;
+    gzip on;
+    gzip_min_length 1000;
+    gzip_proxied any;
+    gzip_types text/plain text/html text/css text/xml
+               application/x-javascript application/xml
+               application/atom+xml text/javascript;
+
+    # Only retry if there was a communication error, not a timeout
+    # on the Tornado server (to avoid propagating "queries of death"
+    # to all frontends)
+    proxy_next_upstream error;
+
+    server {
+        listen 8080;
+
+        location / {
+            proxy_pass_header Server;
+            proxy_set_header Host $http_host;
+            proxy_redirect false;
+            proxy_set_header X-Real-IP $remote_addr;
+            proxy_set_header X-Scheme $scheme;
+            proxy_pass http://frontends;
+        }
+    }
+}

+ 0 - 0
tornado/requirements.txt


+ 65 - 0
tornado/server.py

@@ -0,0 +1,65 @@
+import tornado.ioloop
+import tornado.web
+from tornado import gen
+import motor
+import random
+from tornado import escape
+import tornado.options
+from tornado.options import options
+
+
+tornado.options.define('port', default=8888, type=int, help=(
+    "Server port"))
+
+
+db = motor.MotorClient("127.0.0.1").open_sync().hello_world
+
+class JsonSerializeTestHandler(tornado.web.RequestHandler):
+    @tornado.web.asynchronous
+    @gen.coroutine
+    def get(self):
+        obj = dict(message="Hello, World!")
+        self.write(obj)
+
+class QueryTestHandler(tornado.web.RequestHandler):
+    @tornado.web.asynchronous
+    @gen.coroutine
+    def get(self):
+        queries = int(self.get_argument("queries", 0))
+
+        if queries == 0:
+            random_id = random.randint(1, 10000)
+            world = yield gen.Task(db.world.find_one,{"randomNumber": random_id}, fields={"_id": 0, "id": 1, "randomNumber": 1})
+            # Get first postion on arguments, and so first postion in mongo return
+            world = world[0][0]
+        else:
+            worlds = []
+            for i in xrange(int(queries)):
+                random_id = random.randint(1, 10000)
+                world = yield gen.Task(db.world.find_one,{"randomNumber": random_id}, fields={"_id": 0, "id": 1, "randomNumber": 1})
+                # Get first postion on arguments, and so first postion in mongo return
+                worlds.append(world[0][0])
+
+            worlds = escape.json_encode(worlds)
+            self.set_header("Content-Type", "application/json; charset=UTF-8")
+
+        self.write(worlds if queries > 0 else world)
+
+
+class SingleQueryTestHandler(tornado.web.RequestHandler):
+    @tornado.web.asynchronous
+    @gen.coroutine
+    def get(self):
+        worlds = []
+        random_id = random.randint(1, 10000)
+        self.write(world[0][0])
+
+application = tornado.web.Application([
+    (r"/json", JsonSerializeTestHandler),
+    (r"/db", QueryTestHandler),
+])
+
+if __name__ == "__main__":
+    tornado.options.parse_command_line()
+    application.listen(options.port)
+    tornado.ioloop.IOLoop.instance().start()

+ 44 - 0
tornado/setup.py

@@ -0,0 +1,44 @@
+import subprocess
+import sys
+import setup_util
+import os
+from os.path import expanduser
+
+home = expanduser("~")
+cwd = "%s/FrameworkBenchmarks/tornado" % home
+
+
+def start(args):
+    setup_util.replace_text(
+        cwd + "/server.py", "127.0.0.1", args.database_host)
+
+    subprocess.Popen("python %s/FrameworkBenchmarks/tornado/server.py --port=8000" % home, shell=True, cwd=cwd)
+    subprocess.Popen("python %s/FrameworkBenchmarks/tornado/server.py --port=8001" % home, shell=True, cwd=cwd)
+    subprocess.Popen("python %s/FrameworkBenchmarks/tornado/server.py --port=8002" % home, shell=True, cwd=cwd)
+    subprocess.Popen("python %s/FrameworkBenchmarks/tornado/server.py --port=8003" % home, shell=True, cwd=cwd)
+    subprocess.check_call("sudo /usr/local/nginx/sbin/nginx -c " + home + "/FrameworkBenchmarks/php/deploy/nginx.conf", shell=True)
+
+    return 0
+
+
+def stop():
+
+    try:
+
+        subprocess.call("sudo /usr/local/nginx/sbin/nginx -s stop", shell=True)
+
+    except subprocess.CalledProcessError:
+        #TODO: Better handle exception.
+        pass
+
+    p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+    out, err = p.communicate()
+    for line in out.splitlines():
+        if 'server.py' in line:
+            try:
+                pid = int(line.split(None, 2)[1])
+                os.kill(pid, 9)
+            except OSError:
+                pass
+
+    return 0