Quellcode durchsuchen

[Python] Bump granian to 0.3 (#8053)

Giovanni Barillari vor 2 Jahren
Ursprung
Commit
29b994a98c

+ 41 - 0
frameworks/Python/granian/app_wrk.py

@@ -0,0 +1,41 @@
+import orjson
+
+JSON_HEADERS = [('content-type', 'application/json')]
+PLAINTEXT_HEADERS = [('content-type', 'text/plain; charset=utf-8')]
+
+json_dumps = orjson.dumps
+
+
+async def route_json(scope, proto):
+    proto.response_bytes(
+        200,
+        JSON_HEADERS,
+        json_dumps({'message': 'Hello, world!'})
+    )
+
+
+async def route_plaintext(scope, proto):
+    proto.response_bytes(
+        200,
+        PLAINTEXT_HEADERS,
+        b'Hello, world!'
+    )
+
+
+async def handle_404(scope, proto):
+    proto.response_bytes(
+        404,
+        PLAINTEXT_HEADERS,
+        b'Not found'
+    )
+
+
+routes = {
+    '/json': route_json,
+    '/plaintext': route_plaintext
+}
+
+
+def main(scope, proto):
+    handler = routes.get(scope.path, handle_404)
+    return handler(scope, proto)

+ 18 - 0
frameworks/Python/granian/benchmark_config.json

@@ -62,6 +62,24 @@
       "display_name": "granian [wsgi]",
       "notes": "",
       "versus": "uvicorn"
+    },
+    "wrk": {
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "Postgres",
+      "framework": "granian",
+      "language": "Python",
+      "orm": "Raw",
+      "platform": "None",
+      "webserver": "granian",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "granian [rsgi][wrk]",
+      "notes": "",
+      "versus": "uvicorn"
     }
   }]
 }

+ 13 - 0
frameworks/Python/granian/config.toml

@@ -47,3 +47,16 @@ orm = "Raw"
 platform = "None"
 webserver = "granian"
 versus = "uvicorn"
+
+[wrk]
+urls.plaintext = "/plaintext"
+urls.json = "/json"
+approach = "Realistic"
+classification = "Platform"
+database = "Postgres"
+database_os = "Linux"
+os = "Linux"
+orm = "Raw"
+platform = "None"
+webserver = "granian"
+versus = "uvicorn"

+ 1 - 1
frameworks/Python/granian/granian-rsgi.dockerfile

@@ -8,4 +8,4 @@ RUN pip install -r /granian/requirements.txt
 
 EXPOSE 8080
 
-CMD python run.py rsgi
+CMD python run.py rsgi runtime

+ 11 - 0
frameworks/Python/granian/granian-wrk.dockerfile

@@ -0,0 +1,11 @@
+FROM python:3.10-slim
+
+ADD ./ /granian
+
+WORKDIR /granian
+
+RUN pip install -r /granian/requirements.txt
+
+EXPOSE 8080
+
+CMD python run.py rsgi workers

+ 1 - 1
frameworks/Python/granian/granian-wsgi.dockerfile

@@ -8,4 +8,4 @@ RUN pip install -r /granian/requirements.txt
 
 EXPOSE 8080
 
-CMD python run.py wsgi
+CMD python run.py wsgi runtime

+ 1 - 1
frameworks/Python/granian/granian.dockerfile

@@ -8,4 +8,4 @@ RUN pip install -r /granian/requirements.txt
 
 EXPOSE 8080
 
-CMD python run.py asgi
+CMD python run.py asgi runtime

+ 1 - 1
frameworks/Python/granian/requirements.txt

@@ -1,4 +1,4 @@
 asyncpg==0.27.0
-granian==0.2.0
+granian>=0.3.0,<0.4.0
 jinja2==3.1.2
 orjson==3.8.1

+ 11 - 3
frameworks/Python/granian/run.py

@@ -6,15 +6,23 @@ from granian import Granian
 
 if __name__ == '__main__':
     interface = sys.argv[1]
+    threading_mode = sys.argv[2]
+    if threading_mode == "runtime":
+        workers = multiprocessing.cpu_count()
+        threads = 2
+    else:
+        workers = multiprocessing.cpu_count() // 2
+        threads = 1
 
     Granian(
         f"app_{interface}:main",
         address="0.0.0.0",
         port=8080,
-        workers=multiprocessing.cpu_count(),
-        threading_mode="runtime",
-        threads=2,
+        workers=workers,
+        threading_mode=threading_mode,
+        threads=threads,
         backlog=2048,
         interface=interface,
+        http="1",
         websockets=False
     ).serve()