Browse Source

Add Redis endpoint: /db_redis

Ludovic Gasc (GMLudo) 10 years ago
parent
commit
52d7bd960b

+ 17 - 0
frameworks/Python/API-Hour/benchmark_config

@@ -23,5 +23,22 @@
       "display_name": "API-Hour",
       "notes": "Python 3.4 + AsyncIO"
     }
+    "redis": {
+      "setup_file": "setup",
+      "db_url": "/db_redis",
+      "port": 8008,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "Redis",
+      "framework": "aiohttp.web",
+      "language": "Python",
+      "orm": "Raw",
+      "platform": "API-Hour",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "API-Hour",
+      "notes": "Python 3.4 + AsyncIO"
+    }
   }]
 }

+ 5 - 1
frameworks/Python/API-Hour/hello/etc/hello/main/main.yaml

@@ -7,4 +7,8 @@ engines:
     user: benchmarkdbuser
     password: benchmarkdbpass
     minsize: 22
-    maxsize: 22
+    maxsize: 22
+  redis:
+    host: 127.0.0.1
+    port: 6379
+    poolsize: 40

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

@@ -5,6 +5,8 @@ import os
 import aiopg
 import jinja2
 import psycopg2.extras
+import asyncio_redis
+from asyncio_redis.protocol import HiRedisProtocol
 import aiohttp.web
 import aiohttp_jinja2
 import api_hour
@@ -25,6 +27,7 @@ class Container(api_hour.Container):
         # routes
         self.servers['http'].router.add_route('GET', '/json', endpoints.world.json)
         self.servers['http'].router.add_route('GET', '/db', endpoints.world.db)
+        self.servers['http'].router.add_route('GET', '/db_redis', endpoints.world.db_redis)
         self.servers['http'].router.add_route('GET', '/queries', endpoints.world.queries)
         self.servers['http'].router.add_route('GET', '/fortunes', endpoints.world.fortunes)
         self.servers['http'].router.add_route('GET', '/updates', endpoints.world.updates)
@@ -50,8 +53,14 @@ class Container(api_hour.Container):
                                                                      password=self.config['engines']['pg']['password'],
                                                                      cursor_factory=psycopg2.extras.RealDictCursor,
                                                                      minsize=int(self.config['engines']['pg']['minsize']),
-                                                                     maxsize=int(self.config['engines']['pg']['maxsize'])))
+                                                                     maxsize=int(self.config['engines']['pg']['maxsize']),
+                                                                     loop=self.loop))
         yield from asyncio.wait([self.engines['pg']], return_when=asyncio.ALL_COMPLETED)
+        self.engines['redis'] = yield from asyncio_redis.Pool.create(host=self.config['engines']['redis']['host'],
+                                                                     port=self.config['engines']['redis']['port'],
+                                                                     poolsize=self.config['engines']['redis']['poolsize'],
+                                                                     loop=self.loop,
+                                                                     protocol_class=HiRedisProtocol)
 
         LOG.info('All engines ready !')
 
@@ -65,5 +74,8 @@ class Container(api_hour.Container):
                 yield from self.engines['pg'].result().wait_closed()
             else:
                 yield from self.engines['pg'].cancel()
+        if 'redis' in self.engines:
+            self.engines['redis'].close()
+            yield from asyncio.sleep(1) # wait redis close connection
         LOG.info('All engines stopped !')
         yield from super().stop()

+ 8 - 0
frameworks/Python/API-Hour/hello/hello/endpoints/world.py

@@ -7,6 +7,7 @@ import aiohttp_jinja2
 
 from ..services import queries_number
 from ..services.world import get_random_record, get_random_records, update_random_records, get_fortunes
+from ..services import redis
 
 LOG = logging.getLogger(__name__)
 
@@ -22,6 +23,13 @@ def db(request):
 
     return JSON((yield from get_random_record(container)))
 
[email protected]
+def db_redis(request):
+    """Test type 2: Single database query"""
+    container = request.app.ah_container
+
+    return JSON((yield from redis.get_random_record(container)))
+
 @asyncio.coroutine
 def queries(request):
     """Test type 3: Multiple database queries"""

+ 9 - 0
frameworks/Python/API-Hour/hello/hello/services/redis.py

@@ -0,0 +1,9 @@
+import asyncio
+from random import randint
+
+
[email protected]
+def get_random_record(container):
+    idx = randint(1, 10000)
+    random_number = yield from container.engines['redis'].get('world:%i' % idx)
+    return {'Id': idx, 'RandomNumber': random_number}

+ 8 - 5
frameworks/Python/API-Hour/requirements.txt

@@ -1,11 +1,14 @@
-api_hour==0.6.1
+Jinja2==2.7.3
+MarkupSafe==0.23
+PyYAML==3.11
+aiohttp==0.13.1
+aiohttp-jinja2==0.0.2
 aiopg==0.5.2
+api-hour==0.6.1
+asyncio-redis==0.13.4
 gunicorn==19.1.1
+hiredis==0.1.6
 psycopg2==2.5.4
 setproctitle==1.1.8
 six==1.8.0
 ujson==1.33
-aiohttp==0.13.1
-PyYAML==3.11
-aiohttp-jinja2==0.0.2
-MarkupSafe==0.23