raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable") TypeError: Object of type Products is not JSON serializable 03/02/2023 23:00:38 PM -werkzeug- Thread-1-16752 - INFO - 127.0.0.1 - - [02/Mar/2023 23:00:38] "GET /products_from_mysql/2 HTTP/1.1" 500 -
#https://github.com/benoitc/gunicorn/issues/1194 keepalive = 75 # needs to be longer than the ELB idle timeout worker_class = 'gevent'
使用locust浅压一下
在本地使用locust创建个压测任务
1 2 3 4 5 6 7 8 9 10 11 12 13
from locust import HttpUser, TaskSet, task import random class MyTaskSet(TaskSet): @task def get_product(self): id = random.randint(1,100) self.client.get("/{id}".format(id=id))
@app.route('/products/<int:product_id>', methods=['GET']) def get_product(product_id): cache_key = f'product:{product_id}' product = cache.get(cache_key) if not product: product = Products.query.get_or_404(product_id).to_dict() app.logger.info(f'Product {product_id} is retrieved from MySQL database: {product}') cache.set(cache_key, product) else: app.logger.info(f'Product {product_id} is retrieved from Redis database: {product}') return jsonify({'product': product})
# 略
对应日志
1 2 3 4
03/03/2023 00:56:01 AM -app- Thread-3-7584 - INFO - Product 20 is retrieved from MySQL database: {'id': 20, 'name': 'Surface Laptop 4', 'price': 1299.99, 'description': '笔记本电脑'} 03/03/2023 00:56:01 AM -werkzeug- Thread-3-7584 - INFO - 127.0.0.1 - - [03/Mar/2023 00:56:01] "GET /products/20 HTTP/1.1" 200 - 03/03/2023 00:56:11 AM -app- Thread-4-15520 - INFO - Product 20 is retrieved from Redis database: {'id': 20, 'name': 'Surface Laptop 4', 'price': 1299.99, 'description': '笔记本电脑'} 03/03/2023 00:56:11 AM -werkzeug- Thread-4-15520 - INFO - 127.0.0.1 - - [03/Mar/2023 00:56:11] "GET /products/20 HTTP/1.1" 200 -
locust压测测试
1 2 3 4 5 6 7 8 9 10 11 12 13
from locust import HttpUser, TaskSet, task import random class MyTaskSet(TaskSet): @task def get_product(self): id = random.randint(1,100) self.client.get("/{id}".format(id=id))