Преглед на файлове

Add fortune test type to 'uvicorn'. (#2867)

* Added uvicorn, an asyncio server for Python 3.

* Include uvicorn in .travis.yml

* Fix content-type on json test case

* Write pid file to /tmp

* Fix content-type of JSON test type

* Add 'fortunes' test type for uvicorn

* Use default port for database connection

* Set database to Postgres in benchmark_config.json

* Resolve postgresql dependancy

* Fix fortunes URL and table name

* Minor correction to 'Additional fortune' text string.

* Fix fortune.html
Tom Christie преди 8 години
родител
ревизия
6b442b732e

+ 62 - 19
frameworks/Python/uvicorn/app.py

@@ -1,48 +1,91 @@
+import asyncio
+import asyncpg
+import jinja2
+import os
 import ujson as json
+from operator import itemgetter
 
 
-def json_endpoint(message):
+async def setup():
+    global pool
+    pool = await asyncpg.create_pool(
+        user=os.getenv('PGUSER', 'benchmarkdbuser'),
+        password=os.getenv('PGPASS', 'benchmarkdbpass'),
+        database='hello_world',
+        host=os.getenv('DBHOST', 'localhost'),
+        port=5432
+    )
+
+
+pool = None
+additional = [0, 'Additional fortune added at request time.']
+key = itemgetter(1)
+template = None
+path = os.path.join('templates', 'fortune.html')
+with open(path, 'r') as template_file:
+    template_text = template_file.read()
+    template = jinja2.Template(template_text)
+
+loop = asyncio.get_event_loop()
+loop.run_until_complete(setup())
+
+
+async def json_endpoint(message, channels):
     content = json.dumps({'message': 'Hello, world!'}).encode('utf-8')
-    response = {
+    await channels['reply'].send({
         'status': 200,
         'headers': [
             [b'content-type', b'application/json'],
         ],
         'content': content
-    }
-    message['reply_channel'].send(response)
+    })
 
 
-def plaintext_endpoint(message):
-    content = b'Hello, world!'
-    response = {
+async def fortunes_endpoint(message, channels):
+    connection = await pool.acquire()
+    try:
+        fortunes = await connection.fetch('SELECT * FROM Fortune')
+        fortunes.append(additional)
+        fortunes.sort(key=key)
+        content = template.render(fortunes=fortunes).encode('utf-8')
+        await channels['reply'].send({
+            'status': 200,
+            'headers': [
+                [b'content-type', b'text/html; charset=utf-8'],
+            ],
+            'content': content
+        })
+    finally:
+        await pool.release(connection)
+
+
+async def plaintext_endpoint(message, channels):
+    await channels['reply'].send({
         'status': 200,
         'headers': [
             [b'content-type', b'text/plain'],
         ],
-        'content': content
-    }
-    message['reply_channel'].send(response)
+        'content': b'Hello, world!'
+    })
 
 
-def handle_404(message):
-    content = b'Not found'
-    response = {
+async def handle_404(message, channels):
+    await channels['reply'].send({
         'status': 404,
         'headers': [
             [b'content-type', b'text/plain'],
         ],
-        'content': content
-    }
-    message['reply_channel'].send(response)
+        'content': b'Not found'
+    })
 
 
 routes = {
     '/json': json_endpoint,
+    '/fortunes': fortunes_endpoint,
     '/plaintext': plaintext_endpoint
 }
 
 
-def main(message):
-    path = message['content']['path']
-    routes.get(path, handle_404)(message)
+async def main(message, channels):
+    path = message['path']
+    await routes.get(path, handle_404)(message, channels)

+ 2 - 1
frameworks/Python/uvicorn/benchmark_config.json

@@ -4,6 +4,7 @@
     "default": {
       "setup_file": "setup",
       "json_url": "/json",
+      "fortune_url": "/fortunes",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "approach": "Realistic",
@@ -16,7 +17,7 @@
       "os": "Linux",
       "orm": "Raw",
       "database_os": "Linux",
-      "database": "None",
+      "database": "Postgres",
       "display_name": "uvicorn",
       "notes": ""
     }

+ 3 - 1
frameworks/Python/uvicorn/requirements.txt

@@ -1,2 +1,4 @@
-uvicorn==0.0.4
+asyncpg==0.11.0
+jinja2==2.9.6
 ujson==1.35
+uvicorn==0.0.10

+ 1 - 1
frameworks/Python/uvicorn/setup.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
-fw_depends python3
+fw_depends postgresql python3
 
 pip3 install --install-option="--prefix=${PY3_ROOT}" -r $TROOT/requirements.txt
 

+ 10 - 0
frameworks/Python/uvicorn/templates/fortune.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head><title>Fortunes</title></head>
+<body>
+<table>
+<tr><th>id</th><th>message</th></tr>
+{% for fortune in fortunes %}<tr><td>{{ fortune[0] }}</td><td>{{ fortune[1]|e }}</td></tr>
+{% endfor %}</table>
+</body>
+</html>