Răsfoiți Sursa

[Python] CherryPy: update to v18.8.0 and disable logs (#8122)

Oleg S 2 ani în urmă
părinte
comite
6243260869

+ 1 - 1
frameworks/Python/cherrypy/README.md

@@ -37,4 +37,4 @@ http://localhost:8080/updates?queries=2
 
 ### Fortune
 
-http://localhost:8080/fortune
+http://localhost:8080/fortunes

+ 40 - 12
frameworks/Python/cherrypy/app.py

@@ -1,4 +1,3 @@
-import cgi
 import os
 import sys
 from functools import partial
@@ -11,6 +10,12 @@ from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy import Column
 from sqlalchemy.types import String, Integer
 
+try:
+    from cgi import escape as html_escape
+except:
+    from html import escape as html_escape
+
+
 Base = declarative_base()
 
 if sys.version_info[0] == 3:
@@ -102,35 +107,58 @@ class CherryPyBenchmark(object):
         return worlds
 
     @cherrypy.expose
-    def fortune(self):
-        fortunes = cherrypy.request.db.query(Fortune).all()
-        fortunes.append(
-            Fortune(id=0, message="Additional fortune added at request time."))
-        fortunes.sort(key=attrgetter("message"))
+    def fortunes(self):
+        _fortunes = cherrypy.request.db.query(Fortune).all()
+        _fortunes.append( Fortune(id=0, message="Additional fortune added at request time.") )
+        _fortunes.sort(key=attrgetter("message"))
         html = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>"
-        for f in fortunes:
-            html += "<tr><td>" + str(f.id) + "</td><td>" + cgi.escape(
-                f.message) + "</td></tr>"
+        for f in _fortunes:
+            html += "<tr><td>" + str(f.id) + "</td><td>" + html_escape(f.message) + "</td></tr>"
         html += "</table></body></html>"
         return html
 
 
 if __name__ == "__main__":
+    import logging
+    import multiprocessing
     # Register the SQLAlchemy plugin
     from saplugin import SAEnginePlugin
     DBDRIVER = 'mysql'
-    DATABASE_URI = '%s://benchmarkdbuser:benchmarkdbpass@tfb-database:3306/hello_world?charset=utf8' % (
-        DBDRIVER)
+    DBUSER = 'benchmarkdbuser'
+    DBPSWD = 'benchmarkdbpass'
+    DATABASE_URI = '%s://%s:%s@tfb-database:3306/hello_world?charset=utf8' % (
+        DBDRIVER, DBUSER, DBPSWD)
     SAEnginePlugin(cherrypy.engine, DATABASE_URI).subscribe()
 
+    _is_travis = os.environ.get('TRAVIS') == 'true'
+
+    workers = int(multiprocessing.cpu_count())
+    if _is_travis:
+        workers = 3
+
+    cherrypy._cpconfig.environments['staging']['log.screen'] = False
+    logging.getLogger("cherrypy").propagate = False
+    log_fmt = "%(asctime)s.%(msecs)03d [%(levelname)s] (%(name)s) %(message)s"
+    logging.basicConfig(level=logging.CRITICAL, format=log_fmt, datefmt="%Y-%m-%d %H:%M:%S")
+    cherrypy.config.update({
+            'tools.db.on': True,
+            'environment': 'production',
+            'log.screen': False,
+            'log.access_file': '',
+            'log.error_file': ''
+    })    
+
     # Register the SQLAlchemy tool
     from satool import SATool
     cherrypy.tools.db = SATool()
     cherrypy.server.socket_host = '0.0.0.0'
+    cherrypy.server.socket_port = 8080
+    cherrypy.server.thread_pool = workers
     cherrypy.quickstart(CherryPyBenchmark(), '', {
         '/': {
             'tools.db.on': True,
             'log.screen': False,
-            'log.access_file': ''
+            'log.access_file': '',
+            'log.error_file': ''
         }
     })

+ 5 - 5
frameworks/Python/cherrypy/benchmark_config.json

@@ -5,7 +5,7 @@
       "json_url": "/json",
       "db_url": "/db",
       "query_url": "/queries?queries=",
-      "fortune_url": "/fortune",
+      "fortune_url": "/fortunes",
       "update_url": "/updates?queries=",
       "plaintext_url": "/plaintext",
       "port": 8080,
@@ -20,14 +20,14 @@
       "webserver": "None",
       "os": "Linux",
       "database_os": "Linux",
-      "display_name": "CherryPy",
+      "display_name": "CherryPy [py2]",
       "notes": "CPython 2.7"
     },
-     "py3": {
+    "py3": {
       "json_url": "/json",
       "db_url": "/db",
       "query_url": "/queries?queries=",
-      "fortune_url": "/fortune",
+      "fortune_url": "/fortunes",
       "update_url": "/updates?queries=",
       "plaintext_url": "/plaintext",
       "port": 8080,
@@ -43,7 +43,7 @@
       "os": "Linux",
       "database_os": "Linux",
       "display_name": "CherryPy",
-      "notes": "CPython 3.4"
+      "notes": "CPython 3.9"
     }
   }]
 }

+ 1 - 1
frameworks/Python/cherrypy/cherrypy-py3.dockerfile

@@ -1,4 +1,4 @@
-FROM python:3.6.6-stretch
+FROM python:3.9-bullseye
 
 ADD ./ /cherrypy
 

+ 2 - 2
frameworks/Python/cherrypy/config.toml

@@ -7,7 +7,7 @@ urls.json = "/json"
 urls.db = "/db"
 urls.query = "/queries?queries="
 urls.update = "/updates?queries="
-urls.fortune = "/fortune"
+urls.fortune = "/fortunes"
 approach = "Realistic"
 classification = "Micro"
 database = "MySQL"
@@ -24,7 +24,7 @@ urls.json = "/json"
 urls.db = "/db"
 urls.query = "/queries?queries="
 urls.update = "/updates?queries="
-urls.fortune = "/fortune"
+urls.fortune = "/fortunes"
 approach = "Realistic"
 classification = "Micro"
 database = "MySQL"

+ 4 - 3
frameworks/Python/cherrypy/requirements.txt

@@ -1,9 +1,10 @@
-cheroot==6.0.0
-CherryPy==13.1.0
+cheroot==8.6.0
+CherryPy==17.4.2 ; python_version=='2.7'
+CherryPy==18.8.0 ; python_version>'3.5'
 more-itertools==4.1.0
 mysqlclient==1.3.12
 portend==2.2
 pytz==2017.3
 six==1.11.0
-SQLAlchemy==1.3.0
+SQLAlchemy==1.4.47
 tempora==1.10