소스 검색

Added Responder Python ASGI framework (#4122)

* Added Responder README, requirements, conf, dockerfile, benchmark config and template

* Added app.py with API tests

* Updated connection

* Renamed Docker file

* Renamed api object to app

* Updated responder and uvicorn

* Fixed header param

* Fixed query params, response header

* Fixed template rendering
Shirish Kadam 6 년 전
부모
커밋
a160c71eb6

+ 22 - 0
frameworks/Python/responder/README.md

@@ -0,0 +1,22 @@
+# Responser Benchmark Test
+
+This is the Responder portion of a [benchmarking tests suite](../../)
+comparing a variety of web development platforms.
+
+The information below is specific to Responser. For further guidance,
+review the [documentation](http://frameworkbenchmarks.readthedocs.org/en/latest/).
+Also note that there is additional information provided in
+the [Python README](../).
+
+## Description
+
+[Responder](https://github.com/kennethreitz/responder) is a familiar HTTP Service Framework for Python
+
+## Test Paths & Sources
+
+All of the test implementations are located within a single file ([app.py](app.py)).
+
+## Resources
+
+* [Starlette on GitHub](https://github.com/kennethreitz/responder)
+* [ASGI specification](https://asgi.readthedocs.io/en/latest/)

+ 122 - 0
frameworks/Python/responder/app.py

@@ -0,0 +1,122 @@
+import asyncio
+import asyncpg
+import os
+import responder
+import jinja2
+from random import randint
+from operator import itemgetter
+
+
+READ_ROW_SQL = 'SELECT "randomnumber" FROM "world" WHERE id = $1'
+WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
+ADDITIONAL_ROW = [0, 'Additional fortune added at request time.']
+
+
+async def setup_database():
+    global connection_pool
+    connection_pool = await asyncpg.create_pool(
+        user=os.getenv('PGUSER', 'benchmarkdbuser'),
+        password=os.getenv('PGPASS', 'benchmarkdbpass'),
+        database='hello_world',
+        host='tfb-database',
+        port=5432
+    )
+
+
+def load_fortunes_template():
+    path = os.path.join('templates', 'fortune.html')
+    with open(path, 'r') as template_file:
+        template_text = template_file.read()
+        return jinja2.Template(template_text)
+
+
+def get_num_queries(request):
+    try:
+        query_string = request.params['queries']
+        query_count = int(query_string)
+    except (KeyError, IndexError, ValueError):
+        return 1
+
+    if query_count < 1:
+        return 1
+    if query_count > 500:
+        return 500
+    return query_count
+
+
+connection_pool = None
+sort_fortunes_key = itemgetter(1)
+template = load_fortunes_template()
+loop = asyncio.get_event_loop()
+loop.run_until_complete(setup_database())
+
+
+app = responder.API()
+
+
[email protected]('/json')
+def json_serialization(req, resp):
+    resp.media = {'message': 'Hello, world!'}
+
+
[email protected]('/db')
+async def single_database_query(req, resp):
+    row_id = randint(1, 10000)
+
+    async with connection_pool.acquire() as connection:
+        number = await connection.fetchval(READ_ROW_SQL, row_id)
+
+    resp.media = {'id': row_id, 'randomNumber': number}
+
+
[email protected]('/queries')
+async def multiple_database_queries(req, resp):
+    num_queries = get_num_queries(req)
+    row_ids = [randint(1, 10000) for _ in range(num_queries)]
+    worlds = []
+
+    async with connection_pool.acquire() as connection:
+        statement = await connection.prepare(READ_ROW_SQL)
+        for row_id in row_ids:
+            number = await statement.fetchval(row_id)
+            worlds.append({'id': row_id, 'randomNumber': number})
+
+    resp.media = worlds
+
+
[email protected]('/fortunes')
+async def fortunes(req, resp):
+    fortune_list = []
+    async with connection_pool.acquire() as connection:
+        fortune_list = await connection.fetch('SELECT * FROM Fortune')
+
+    fortune_list.append(ADDITIONAL_ROW)
+    fortune_list.sort(key=sort_fortunes_key)
+    resp.headers['Content-Type'] = "text/html;charset=utf-8"
+    resp.content = template.render(fortunes=fortune_list)
+
+
[email protected]('/updates')
+async def database_updates(req, resp):
+    num_queries = get_num_queries(req)
+    updates = [(randint(1, 10000), randint(1, 10000)) for _ in range(num_queries)]
+    worlds = [{'id': row_id, 'randomNumber': number} for row_id, number in updates]
+
+    async with connection_pool.acquire() as connection:
+        statement = await connection.prepare(READ_ROW_SQL)
+        for row_id, number in updates:
+            await statement.fetchval(row_id)
+        await connection.executemany(WRITE_ROW_SQL, updates)
+
+    resp.media = worlds
+
+
[email protected]('/plaintext')
+def plaintext(req, resp):
+    resp.headers['Content-Type'] = "text/plain"
+    resp.text = "Hello, world!"
+
+"""
+if __name__ == '__main__':
+    app.run()
+"""

+ 27 - 0
frameworks/Python/responder/benchmark_config.json

@@ -0,0 +1,27 @@
+{
+  "framework": "responder",
+  "tests": [{
+    "default": {
+      "json_url": "/json",
+      "fortune_url": "/fortunes",
+      "plaintext_url": "/plaintext",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "update_url": "/updates?queries=",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "framework": "responder",
+      "language": "Python",
+      "flavor": "Python3",
+      "platform": "None",
+      "webserver": "None",
+      "os": "Linux",
+      "orm": "Raw",
+      "database_os": "Linux",
+      "database": "Postgres",
+      "display_name": "Responder",
+      "notes": ""
+    }
+  }]
+}

+ 6 - 0
frameworks/Python/responder/requirements.txt

@@ -0,0 +1,6 @@
+asyncpg==0.16.0
+gunicorn==19.9.0
+responder >= 0.0.4
+jinja2==2.10
+ujson==1.35
+uvicorn >= 0.3.13

+ 9 - 0
frameworks/Python/responder/responder.dockerfile

@@ -0,0 +1,9 @@
+FROM python:3.6.6-stretch
+
+ADD ./ /responder
+
+WORKDIR /responder
+
+RUN pip3 install -r /responder/requirements.txt
+
+CMD gunicorn app:app -k uvicorn.workers.UvicornWorker -c responder_conf.py

+ 14 - 0
frameworks/Python/responder/responder_conf.py

@@ -0,0 +1,14 @@
+import multiprocessing
+import os
+
+_is_travis = os.environ.get('TRAVIS') == 'true'
+
+workers = multiprocessing.cpu_count()
+if _is_travis:
+    workers = 2
+
+bind = "0.0.0.0:8080"
+keepalive = 120
+errorlog = '-'
+pidfile = '/tmp/responder.pid'
+loglevel = 'error'

+ 10 - 0
frameworks/Python/responder/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>