Przeglądaj źródła

Python/api_hour updates fix & cleanup (#3248)

Nate 7 lat temu
rodzic
commit
5ceda02d6e

+ 0 - 3
frameworks/Python/api_hour/aiohttp.web/etc/default/hello

@@ -1,3 +0,0 @@
-# Start the daemon by default, let the user disable it.
-START_DAEMON=yes
-DAEMON_ARGS="--config_dir=/etc/hello"

+ 1 - 1
frameworks/Python/api_hour/aiohttp.web/etc/hello/api_hour/gunicorn_conf.py

@@ -10,6 +10,6 @@ if _is_travis:
 bind = ['0.0.0.0:8080']
 keepalive = 120
 errorlog = '-'
-pidfile = 'api_hour.pid'
+pidfile = '/tmp/api_hour.pid'
 pythonpath = 'hello'
 backlog = 10240000

+ 0 - 7
frameworks/Python/api_hour/aiohttp.web/etc/logrotate.d/hello

@@ -1,7 +0,0 @@
-/var/log/hello/hello.log {
-    daily
-    rotate 15
-    compress
-    copytruncate
-    missingok
-}

+ 0 - 5
frameworks/Python/api_hour/aiohttp.web/etc/monit/conf.d/hello

@@ -1,5 +0,0 @@
-check process cache_updater with pidfile /run/lock/hello_0
-	group hello
-	start program = "/etc/init.d/hello start"
-	stop program = "/etc/init.d/hello stop"
-	if 5 restarts within 5 cycles then timeout

+ 1 - 11
frameworks/Python/api_hour/aiohttp.web/hello/__init__.py

@@ -5,8 +5,6 @@ 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 aiomysql
@@ -27,16 +25,12 @@ 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', '/db_mysql', endpoints.world.db_mysql)
         self.servers['http'].router.add_route('GET', '/queries', endpoints.world.queries)
-        # self.servers['http'].router.add_route('GET', '/queries_redis', endpoints.world.queries_redis)
         self.servers['http'].router.add_route('GET', '/queries_mysql', endpoints.world.queries_mysql)
         self.servers['http'].router.add_route('GET', '/fortunes', endpoints.world.fortunes)
-        # self.servers['http'].router.add_route('GET', '/fortunes_redis', endpoints.world.fortunes_redis)
         self.servers['http'].router.add_route('GET', '/fortunes_mysql', endpoints.world.fortunes_mysql)
         self.servers['http'].router.add_route('GET', '/updates', endpoints.world.updates)
-        # self.servers['http'].router.add_route('GET', '/updates_redis', endpoints.world.updates_redis)
         self.servers['http'].router.add_route('GET', '/updates_mysql', endpoints.world.updates_mysql)
         self.servers['http'].router.add_route('GET', '/plaintext', endpoints.world.plaintext)
 
@@ -51,6 +45,7 @@ class Container(api_hour.Container):
     def start(self):
         yield from super().start()
         LOG.info('Starting engines...')
+        print('Starting engines...')
         self.engines['pg'] = self.loop.create_task(aiopg.create_pool(host=os.environ.get('DBHOST', self.config['engines']['pg']['host']),
                                                                      port=int(self.config['engines']['pg']['port']),
                                                                      sslmode='disable',
@@ -74,11 +69,6 @@ class Container(api_hour.Container):
                 use_unicode=True,
                 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 !')
 

+ 1 - 33
frameworks/Python/api_hour/aiohttp.web/hello/endpoints/world.py

@@ -7,7 +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, mysql
+from ..services import mysql
 
 LOG = logging.getLogger(__name__)
 
@@ -23,13 +23,6 @@ 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 db_mysql(request):
     """Test type 2: Single database query"""
@@ -45,14 +38,6 @@ def queries(request):
 
     return JSON((yield from get_random_records(container, limit)))
 
[email protected]
-def queries_redis(request):
-    """Test type 3: Multiple database queries"""
-    container = request.app.ah_container
-    limit = queries_number(request.GET.get('queries', 1))
-
-    return JSON((yield from redis.get_random_records(container, limit)))
-
 @asyncio.coroutine
 def queries_mysql(request):
     """Test type 3: Multiple database queries"""
@@ -70,15 +55,6 @@ def fortunes(request):
                                           request,
                                           {'fortunes': (yield from get_fortunes(container))})
 
[email protected]
-def fortunes_redis(request):
-    """Test type 4: Fortunes"""
-    container = request.app.ah_container
-
-    return aiohttp_jinja2.render_template('fortunes.html.j2',
-                                          request,
-                                          {'fortunes': (yield from redis.get_fortunes(container))})
-
 @asyncio.coroutine
 def fortunes_mysql(request):
     """Test type 4: Fortunes"""
@@ -96,14 +72,6 @@ def updates(request):
 
     return JSON((yield from update_random_records(container, limit)))
 
[email protected]
-def updates_redis(request):
-    """Test type 5: Database updates"""
-    container = request.app.ah_container
-    limit = queries_number(request.GET.get('queries', 1))
-
-    return JSON((yield from redis.update_random_records(container, limit)))
-
 @asyncio.coroutine
 def updates_mysql(request):
     """Test type 5: Database updates"""

+ 1 - 0
frameworks/Python/api_hour/aiohttp.web/hello/services/mysql.py

@@ -39,6 +39,7 @@ def update_random_records(container, limit):
             world['RandomNumber'] = randint(1, 10000)
             yield from cur.execute('UPDATE world SET randomnumber=%(random_number)s WHERE id=%(idx)s',
                                    {'random_number': world['RandomNumber'], 'idx': world['Id']})
+            yield from mysql_conn.commit()
             results.append(world)
     return results
 

+ 0 - 45
frameworks/Python/api_hour/aiohttp.web/hello/services/redis.py

@@ -1,45 +0,0 @@
-from operator import itemgetter
-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}
-
[email protected]
-def get_random_records(container, limit):
-    results = []
-    for i in range(limit):
-        idx = randint(1, 10000)
-        random_number = yield from container.engines['redis'].get('world:%i' % idx)
-        results.append({'Id': idx, 'RandomNumber': random_number})
-
-    return results
-
[email protected]
-def update_random_records(container, limit):
-    results = []
-    for i in range(limit):
-        idx = randint(1, 10000)
-        random_number = yield from container.engines['redis'].get('world:%i' % idx)
-        yield from container.engines['redis'].set('world:%i' % idx, str(randint(1, 10000)))
-        results.append({'Id': idx, 'RandomNumber': random_number})
-    return results
-
[email protected]
-def get_fortunes(container):
-    results = []
-    list_reply = yield from container.engines['redis'].lrange('fortunes')
-    fortunes = yield from list_reply.aslist()
-    i = 1
-    for fortune in fortunes:
-        results.append({'id': i, 'message': fortune})
-        i += 1
-
-    results.append({'id': 0, 'message': 'Additional fortune added at request time.'})
-    results.sort(key=itemgetter('message'))
-
-    return results

+ 1 - 1
frameworks/Python/api_hour/aiohttp.web/hello/services/world.py

@@ -1,4 +1,5 @@
 import asyncio
+import sys
 from random import randint
 from operator import itemgetter
 
@@ -28,7 +29,6 @@ def get_random_records(container, limit):
 def update_random_records(container, limit):
     results = []
     pg = yield from container.engines['pg']
-
     with (yield from pg.cursor()) as cur:
         for i in range(limit):
             yield from cur.execute('SELECT id AS "Id", randomnumber AS "RandomNumber" FROM world WHERE id=%(idx)s LIMIT 1',

+ 1 - 1
frameworks/Python/api_hour/benchmark_config.json

@@ -71,7 +71,7 @@
       "port": 8081,
       "approach": "Stripped",
       "classification": "Platform",
-      "database": "None",
+      "database": "Postgres",
       "framework": "api_hour",
       "language": "Python",
       "flavor": "Python3",

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

@@ -10,6 +10,6 @@ if _is_travis:
 bind = ['0.0.0.0:8080', '0.0.0.0:8081', '0.0.0.0:8082']
 keepalive = 120
 errorlog = '-'
-pidfile = 'api_hour.pid'
+pidfile = '/tmp/api_hour.pid'
 pythonpath = 'hello'
 backlog = 10240000

+ 1 - 5
frameworks/Python/api_hour/yocto_http/etc/hello/main/main.yaml

@@ -1,14 +1,10 @@
 ---
 engines:
   pg:
-    host: 127.0.0.1
+    host: TFB-database
     port: 5432
     dbname: hello_world
     user: benchmarkdbuser
     password: benchmarkdbpass
     minsize: 22
     maxsize: 22
-  redis:
-    host: 127.0.0.1
-    port: 6379
-    poolsize: 40

+ 0 - 7
frameworks/Python/api_hour/yocto_http/etc/logrotate.d/hello

@@ -1,7 +0,0 @@
-/var/log/hello/hello.log {
-    daily
-    rotate 15
-    compress
-    copytruncate
-    missingok
-}

+ 0 - 5
frameworks/Python/api_hour/yocto_http/etc/monit/conf.d/hello

@@ -1,5 +0,0 @@
-check process cache_updater with pidfile /run/lock/hello_0
-	group hello
-	start program = "/etc/init.d/hello start"
-	stop program = "/etc/init.d/hello stop"
-	if 5 restarts within 5 cycles then timeout

+ 1 - 10
frameworks/Python/api_hour/yocto_http/hello/__init__.py

@@ -5,8 +5,6 @@ import os
 import aiopg
 import jinja2
 import psycopg2.extras
-import asyncio_redis
-from asyncio_redis.protocol import HiRedisProtocol
 import api_hour
 
 from . import endpoints
@@ -48,11 +46,6 @@ class Container(api_hour.Container):
                                                                      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,8 +58,6 @@ 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()

+ 0 - 1
frameworks/Python/api_hour/yocto_http/hello/endpoints/world.py

@@ -4,7 +4,6 @@ import ujson
 
 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__)
 

+ 0 - 45
frameworks/Python/api_hour/yocto_http/hello/services/redis.py

@@ -1,45 +0,0 @@
-from operator import itemgetter
-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}
-
[email protected]
-def get_random_records(container, limit):
-    results = []
-    for i in range(limit):
-        idx = randint(1, 10000)
-        random_number = yield from container.engines['redis'].get('world:%i' % idx)
-        results.append({'Id': idx, 'RandomNumber': random_number})
-
-    return results
-
[email protected]
-def update_random_records(container, limit):
-    results = []
-    for i in range(limit):
-        idx = randint(1, 10000)
-        random_number = yield from container.engines['redis'].get('world:%i' % idx)
-        yield from container.engines['redis'].set('world:%i' % idx, str(randint(1, 10000)))
-        results.append({'Id': idx, 'RandomNumber': random_number})
-    return results
-
[email protected]
-def get_fortunes(container):
-    results = []
-    list_reply = yield from container.engines['redis'].lrange('fortunes')
-    fortunes = yield from list_reply.aslist()
-    i = 1
-    for fortune in fortunes:
-        results.append({'id': i, 'message': fortune})
-        i += 1
-
-    results.append({'id': 0, 'message': 'Additional fortune added at request time.'})
-    results.sort(key=itemgetter('message'))
-
-    return results

+ 2 - 1
frameworks/Python/api_hour/yocto_http/hello/services/world.py

@@ -34,8 +34,9 @@ def update_random_records(container, limit):
             yield from cur.execute('SELECT id AS "Id", randomnumber AS "RandomNumber" FROM world WHERE id=%(idx)s LIMIT 1',
                                    {'idx': randint(1, 10000)})
             world = yield from cur.fetchone()
+            world['RandomNumber'] = randint(1, 10000)
             yield from cur.execute('UPDATE world SET randomnumber=%(random_number)s WHERE id=%(idx)s',
-                                   {'random_number': randint(1, 10000), 'idx': world['Id']})
+                                   {'random_number': world['RandomNumber'], 'idx': world['Id']})
             results.append(world)
     return results