Explorar o código

implement db query with japronto (#7422)

* implement db query through japronto

* declare the specific git commit's hash

Co-authored-by: BillyZee <[email protected]>
IterableTrucks %!s(int64=3) %!d(string=hai) anos
pai
achega
b8dfdbc11b

+ 51 - 0
frameworks/Python/japronto/app_postgres.py

@@ -0,0 +1,51 @@
+import multiprocessing
+from wsgiref.handlers import format_date_time
+import random
+
+import japronto
+import ujson as json
+
+from db import init_db, close_db
+
+
+def get_headers():
+    return {
+        'Server': 'Japronto/0.1.1',
+        'Date': format_date_time(None),
+    }
+
+
+def json_view(request):
+    return request.Response(
+        text=json.dumps({'message': 'Hello, world!'}),
+        mime_type='application/json',
+        headers=get_headers(),
+    )
+
+
+def plaintext_view(request):
+    return request.Response(
+        body=b'Hello, world!',
+        mime_type='text/plain',
+        headers=get_headers(),
+    )
+
+
+async def db_view(request):
+    async with app.db_pool.acquire() as conn:
+        world = await conn.fetchrow("select id,randomnumber from world where id=%s" % random.randint(1, 10000))
+    return request.Response(
+        text=json.dumps(dict(world)),
+        mime_type='application/json', headers=get_headers())
+
+
+app = japronto.Application()
+app.on_startup.append(init_db)
+app.on_cleanup.append(close_db)
+app.router.add_route('/json', json_view, 'GET')
+app.router.add_route('/plaintext', plaintext_view, 'GET')
+app.router.add_route('/db', db_view, 'GET')
+
+
+if __name__ == '__main__':
+    app.run('0.0.0.0', 8080, worker_num=multiprocessing.cpu_count())

+ 20 - 0
frameworks/Python/japronto/benchmark_config.json

@@ -18,6 +18,26 @@
       "database": "None",
       "display_name": "Japronto",
       "notes": ""
+    },
+    "postgres": {
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "db_url":"/db",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "framework": "japronto",
+      "database": "Postgres",
+      "database_os": "Linux",
+      "language": "Python",
+      "flavor": "Python3",
+      "platform": "None",
+      "webserver": "None",
+      "os": "Linux",
+      "orm": "Raw",
+      "database_os": "Linux",
+      "display_name": "Japronto",
+      "notes": ""
     }
   }]
 }

+ 45 - 0
frameworks/Python/japronto/db.py

@@ -0,0 +1,45 @@
+import asyncio
+from contextlib import asynccontextmanager
+import asyncpg
+import os
+
+
+class Connection(asyncpg.Connection):
+    async def reset(self, *, timeout=None):
+        pass
+
+
+class Pool:
+    def __init__(self, connect_url, max_size=10, connection_class=None):
+        self._connect_url = connect_url
+        self._connection_class = connection_class or Connection
+        self._queue = asyncio.LifoQueue(max_size)
+
+    def __await__(self):
+        return self._async_init__().__await__()
+
+    async def _async_init__(self):
+        for _ in range(self._queue.maxsize):
+            self._queue.put_nowait(await asyncpg.connect(self._connect_url, connection_class=self._connection_class))
+        return self
+
+    @asynccontextmanager
+    async def acquire(self):
+        conn = await self._queue.get()
+        try:
+            yield conn
+        finally:
+            self._queue.put_nowait(conn)
+
+    async def close(self):
+        for _ in range(self._queue.maxsize):
+            conn = await self._queue.get()
+            await conn.close()
+
+
+async def init_db(app):
+    app.db_pool = await Pool("postgresql://%s:%s@tfb-database:5432/hello_world" % (os.getenv("PGUSER", "benchmarkdbuser"), os.getenv("PSPASS", "benchmarkdbpass")), connection_class=asyncpg.Connection)
+
+
+async def close_db(app):
+    await asyncio.wait_for(app.db_pool.close(), timeout=1)

+ 11 - 0
frameworks/Python/japronto/japronto-postgres.dockerfile

@@ -0,0 +1,11 @@
+FROM python:3.9.7
+
+ADD ./ /japronto
+
+WORKDIR /japronto
+
+RUN pip3 install -r /japronto/requirements_postgres.txt
+
+EXPOSE 8080
+
+CMD python3 app_postgres.py

+ 3 - 0
frameworks/Python/japronto/requirements_postgres.txt

@@ -0,0 +1,3 @@
+git+https://github.com/IterableTrucks/japronto.git@0d848d96dd010f6701729b14e6b8ec0330002b5c
+asyncpg==0.25.0
+ujson==5.2.0