|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +import asyncpg as pg |
| 5 | +from slimeweb import Slime, SlimeCompression |
| 6 | + |
| 7 | +app = Slime(__file__) |
| 8 | + |
| 9 | + |
| 10 | +def load_json_processing_file(): |
| 11 | + with open("/data/dataset.json", "r") as file: |
| 12 | + return json.load(file) |
| 13 | + |
| 14 | + |
| 15 | +JSON_DATASET = load_json_processing_file() |
| 16 | +QUERY_STMT = """ |
| 17 | +SELECT id, name, category, price, quantity, active, tags, rating_score, rating_count |
| 18 | +FROM items |
| 19 | +WHERE price BETWEEN $1 AND $2 |
| 20 | +LIMIT $3 |
| 21 | +""" |
| 22 | +DB_POOL = None |
| 23 | + |
| 24 | + |
| 25 | +@app.route("/baseline11", method=["GET", "POST"]) |
| 26 | +def baseline_test(req, resp): |
| 27 | + if req.method == "GET": |
| 28 | + result = 0 |
| 29 | + for q_val in req.query.values(): |
| 30 | + try: |
| 31 | + result += int(q_val) |
| 32 | + except ValueError: |
| 33 | + pass |
| 34 | + return resp.plain(str(result)) |
| 35 | + else: |
| 36 | + result = 0 |
| 37 | + for q_val in req.query.values(): |
| 38 | + try: |
| 39 | + result += int(q_val) |
| 40 | + except ValueError: |
| 41 | + pass |
| 42 | + try: |
| 43 | + result += int(req.text) |
| 44 | + except ValueError: |
| 45 | + pass |
| 46 | + return resp.plain(str(result)) |
| 47 | + |
| 48 | + |
| 49 | +@app.route("/pipeline", method="GET") |
| 50 | +def pipeline_test(req, resp): |
| 51 | + return resp.plain("ok") |
| 52 | + |
| 53 | + |
| 54 | +# body_size by default it will read 10MB |
| 55 | +# setting read_size as 25MB |
| 56 | +@app.route("/upload", method="POST", body_size=1024 * 1024 * 25) |
| 57 | +def upload_test(req, resp): |
| 58 | + result = len(req.body) |
| 59 | + return resp.plain(str(result)) |
| 60 | + |
| 61 | + |
| 62 | +@app.route( |
| 63 | + "/json/{count}", method="GET", compression=SlimeCompression.All, comp_level=1 |
| 64 | +) |
| 65 | +def json_test(req, resp): |
| 66 | + global JSON_DATASET |
| 67 | + count = int(req.params["count"]) |
| 68 | + multiplier = int(req.query["m"]) |
| 69 | + result = [] |
| 70 | + for data in JSON_DATASET[:count]: |
| 71 | + result.append( |
| 72 | + { |
| 73 | + "id": data["id"], |
| 74 | + "name": data["name"], |
| 75 | + "category": data["category"], |
| 76 | + "price": data["price"], |
| 77 | + "quantity": data["quantity"], |
| 78 | + "active": data["active"], |
| 79 | + "tags": data["tags"], |
| 80 | + "rating": { |
| 81 | + "score": data["rating"]["score"], |
| 82 | + "count": data["rating"]["count"], |
| 83 | + }, |
| 84 | + "total": data["price"] * data["quantity"] * multiplier, |
| 85 | + } |
| 86 | + ) |
| 87 | + return resp.json({"items": result, "count": count}) |
| 88 | + |
| 89 | + |
| 90 | +# Websocket in slime are event driven |
| 91 | +# echo back based on the message type |
| 92 | +@app.websocket("/ws") |
| 93 | +def websocket_test(req, resp): |
| 94 | + def echo_me(msg): |
| 95 | + if isinstance(msg, str): |
| 96 | + return resp.send_text(msg) |
| 97 | + else: |
| 98 | + return resp.send_bytes(msg) |
| 99 | + |
| 100 | + resp.on_message(echo_me) |
| 101 | + |
| 102 | + |
| 103 | +@app.route("/async-db", method="GET") |
| 104 | +async def async_db_test(req, resp): |
| 105 | + global QUERY_STMT, DB_POOL |
| 106 | + if DB_POOL is None: |
| 107 | + return resp.json({"items": [], "count": 0}) |
| 108 | + min = int(req.query["min"]) |
| 109 | + max = int(req.query["max"]) |
| 110 | + limit = int(req.query["limit"]) |
| 111 | + result = [] |
| 112 | + data_result = None |
| 113 | + async with DB_POOL.acquire() as conn: |
| 114 | + data_result = await conn.fetch(QUERY_STMT, min, max, limit) |
| 115 | + for data in data_result: |
| 116 | + result.append( |
| 117 | + { |
| 118 | + "id": data["id"], |
| 119 | + "name": data["name"], |
| 120 | + "category": data["category"], |
| 121 | + "price": data["price"], |
| 122 | + "quantity": data["quantity"], |
| 123 | + "active": data["active"], |
| 124 | + "tags": json.loads(data["tags"]), |
| 125 | + "rating": { |
| 126 | + "score": data["rating_score"], |
| 127 | + "count": data["rating_count"], |
| 128 | + }, |
| 129 | + } |
| 130 | + ) |
| 131 | + return resp.json({"items": result, "count": len(result)}) |
| 132 | + |
| 133 | + |
| 134 | +@app.start() |
| 135 | +async def init(): |
| 136 | + global DB_POOL |
| 137 | + try: |
| 138 | + DB_POOL = await pg.create_pool( |
| 139 | + dsn=os.environ["DATABASE_URL"], |
| 140 | + min_size=5, |
| 141 | + max_size=int(os.environ.get("DATABASE_MAX_CONN", 256)), |
| 142 | + ) |
| 143 | + print("Pool is created successfully") |
| 144 | + except Exception as e: |
| 145 | + print("Failed to create pool", e) |
| 146 | + DB_POOL = None |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + app.serve(host="0.0.0.0", port=8080, static_path="/data/static") |
0 commit comments