فهرست منبع

Add more strategies with API-Hour+AsyncIO

Ludovic Gasc (GMLudo) 10 سال پیش
والد
کامیت
c3b3772e3e

+ 2 - 2
frameworks/Python/.gitignore

@@ -2,5 +2,5 @@ uwsgi_params
 *.pid
 
 # ignore virtual environments
-pyvenv/
-venv/
+pyvenv*/
+venv*/

+ 35 - 1
frameworks/Python/API-Hour/benchmark_config

@@ -1,5 +1,5 @@
 {
-  "framework": "aiohttp.web",
+  "framework": "AsyncIO",
   "tests": [{
     "default": {
       "setup_file": "setup",
@@ -40,5 +40,39 @@
       "display_name": "API-Hour+aiohttp.web+redis",
       "notes": "Python 3 + AsyncIO + aiohttp.web + Redis"
     }
+    "json": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "port": 8009,
+      "approach": "Stripped",
+      "classification": "Micro",
+      "database": "None",
+      "framework": "AsyncIO",
+      "language": "Python",
+      "orm": "Raw",
+      "platform": "API-Hour",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "None",
+      "display_name": "API-Hour+AsyncIO",
+      "notes": "Python 3 + AsyncIO"
+    }
+    "plaintext": {
+      "setup_file": "setup",
+      "json_url": "/plaintext",
+      "port": 8011,
+      "approach": "Stripped",
+      "classification": "Micro",
+      "database": "None",
+      "framework": "AsyncIO",
+      "language": "Python",
+      "orm": "Raw",
+      "platform": "API-Hour",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "None",
+      "display_name": "API-Hour+AsyncIO",
+      "notes": "Python 3 + AsyncIO"
+    }
   }]
 }

+ 1 - 1
frameworks/Python/API-Hour/hello/etc/hello/api_hour/gunicorn_conf.py

@@ -7,7 +7,7 @@ workers = multiprocessing.cpu_count() * 3
 if _is_travis:
     workers = 2
 
-bind = "0.0.0.0:8008"
+bind = ['0.0.0.0:8008', '0.0.0.0:8009', '0.0.0.0:8011']
 keepalive = 120
 errorlog = '-'
 pidfile = 'api_hour.pid'

+ 4 - 3
frameworks/Python/API-Hour/hello/hello/__init__.py

@@ -12,7 +12,7 @@ import aiohttp_jinja2
 import api_hour
 
 from . import endpoints
-
+from . import servers
 
 LOG = logging.getLogger(__name__)
 
@@ -38,8 +38,9 @@ class Container(api_hour.Container):
                                                   debug=self.worker.cfg.debug,
                                                   keep_alive=self.worker.cfg.keepalive,
                                                   access_log=self.worker.log.access_log,
-                                                  access_log_format=self.worker.cfg.access_log_format)]
-
+                                                  access_log_format=self.worker.cfg.access_log_format),
+                servers.yocto_http.YoctoHttpJson,
+                servers.yocto_http.YoctoHttpText]
 
     @asyncio.coroutine
     def start(self):

+ 1 - 0
frameworks/Python/API-Hour/hello/hello/servers/__init__.py

@@ -0,0 +1 @@
+from . import yocto_http

+ 23 - 0
frameworks/Python/API-Hour/hello/hello/servers/yocto_http.py

@@ -0,0 +1,23 @@
+import asyncio
+
+import ujson
+
+from ..utils import generate_http_response
+
+class YoctoHttpJson(asyncio.Protocol):
+    def connection_made(self, transport):
+        self.transport = transport
+
+    def data_received(self, data):
+        # self.transport.write(data)
+        payload = ujson.dumps({'message': 'Hello, World!'})
+        self.transport.write(generate_http_response(payload))
+
+class YoctoHttpText(asyncio.Protocol):
+    def connection_made(self, transport):
+        self.transport = transport
+
+    def data_received(self, data):
+        # self.transport.write(data)
+        payload = 'Hello, World!'
+        self.transport.write(generate_http_response(payload, 'text/plain; charset=UTF-8'))

+ 12 - 1
frameworks/Python/API-Hour/hello/hello/utils/__init__.py

@@ -1,3 +1,14 @@
 import logging
+from wsgiref.handlers import format_date_time
 
-LOG = logging.getLogger(__name__)
+LOG = logging.getLogger(__name__)
+
+def generate_http_response(payload, content_type='application/json'):
+    return ("""HTTP/1.1 200 OK
+CONTENT-TYPE: %s
+CONTENT-LENGTH: %s
+CONNECTION: keep-alive
+DATE: %s
+SERVER: yocto_http/0.0.1
+
+%s""" % (content_type, len(payload), format_date_time(None), payload)).encode('utf-8')