|
| 1 | +require "kemal" |
| 2 | +require "json" |
| 3 | +require "compress/gzip" |
| 4 | +require "sqlite3" |
| 5 | + |
| 6 | +# --------------------------------------------------------------------------- |
| 7 | +# Startup: load datasets once |
| 8 | +# --------------------------------------------------------------------------- |
| 9 | + |
| 10 | +DATASET_PATH = ENV.fetch("DATASET_PATH", "/data/dataset.json") |
| 11 | +LARGE_DATASET_PATH = "/data/dataset-large.json" |
| 12 | +DB_PATH = "/data/benchmark.db" |
| 13 | +DB_QUERY = "SELECT id, name, category, price, quantity, active, tags, rating_score, rating_count FROM items WHERE price BETWEEN ? AND ? LIMIT 50" |
| 14 | + |
| 15 | +# Process a raw JSON array of items → add "total" field |
| 16 | +def process_items(raw : Array(JSON::Any)) : Array(JSON::Any) |
| 17 | + raw.map do |item| |
| 18 | + obj = item.as_h.dup |
| 19 | + price = item["price"].as_f |
| 20 | + quantity = item["quantity"].as_i |
| 21 | + obj["total"] = JSON::Any.new((price * quantity * 100).round / 100) |
| 22 | + JSON::Any.new(obj) |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | +# Small dataset – processed per-request like Flask does |
| 27 | +DATASET_ITEMS = begin |
| 28 | + if File.exists?(DATASET_PATH) |
| 29 | + Array(JSON::Any).from_json(File.read(DATASET_PATH)) |
| 30 | + else |
| 31 | + nil |
| 32 | + end |
| 33 | +rescue |
| 34 | + nil |
| 35 | +end |
| 36 | + |
| 37 | +# Large dataset – pre-process and pre-compress at startup |
| 38 | +LARGE_GZIP_BUF = begin |
| 39 | + if File.exists?(LARGE_DATASET_PATH) |
| 40 | + raw = Array(JSON::Any).from_json(File.read(LARGE_DATASET_PATH)) |
| 41 | + items = process_items(raw) |
| 42 | + payload = {items: items, count: items.size}.to_json |
| 43 | + io = IO::Memory.new |
| 44 | + Compress::Gzip::Writer.open(io, level: Compress::Gzip::BEST_SPEED) do |gz| |
| 45 | + gz.print payload |
| 46 | + end |
| 47 | + io.to_slice.dup |
| 48 | + else |
| 49 | + nil |
| 50 | + end |
| 51 | +rescue |
| 52 | + nil |
| 53 | +end |
| 54 | + |
| 55 | +DB_AVAILABLE = File.exists?(DB_PATH) |
| 56 | + |
| 57 | +# --------------------------------------------------------------------------- |
| 58 | +# Helpers |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | + |
| 61 | +macro server_header(env) |
| 62 | + {{env}}.response.headers["Server"] = "kemal" |
| 63 | +end |
| 64 | + |
| 65 | +# Thread-local (fiber-local) DB connections |
| 66 | +class DBPool |
| 67 | + @@connections = Hash(UInt64, DB::Database).new |
| 68 | + |
| 69 | + def self.get : DB::Database |
| 70 | + fid = Fiber.current.object_id |
| 71 | + @@connections[fid] ||= DB.open("sqlite3://#{DB_PATH}?mode=ro") |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +# --------------------------------------------------------------------------- |
| 76 | +# Routes |
| 77 | +# --------------------------------------------------------------------------- |
| 78 | + |
| 79 | +get "/pipeline" do |env| |
| 80 | + server_header(env) |
| 81 | + env.response.content_type = "text/plain" |
| 82 | + "ok" |
| 83 | +end |
| 84 | + |
| 85 | +get "/baseline11" do |env| |
| 86 | + server_header(env) |
| 87 | + total = 0_i64 |
| 88 | + env.params.query.each do |_key, value| |
| 89 | + total += value.to_i64 rescue next |
| 90 | + end |
| 91 | + env.response.content_type = "text/plain" |
| 92 | + total.to_s |
| 93 | +end |
| 94 | + |
| 95 | +post "/baseline11" do |env| |
| 96 | + server_header(env) |
| 97 | + total = 0_i64 |
| 98 | + env.params.query.each do |_key, value| |
| 99 | + total += value.to_i64 rescue next |
| 100 | + end |
| 101 | + body = env.request.body.try(&.gets_to_end) |
| 102 | + if body && !body.empty? |
| 103 | + begin |
| 104 | + total += body.strip.to_i64 |
| 105 | + rescue |
| 106 | + end |
| 107 | + end |
| 108 | + env.response.content_type = "text/plain" |
| 109 | + total.to_s |
| 110 | +end |
| 111 | + |
| 112 | +get "/baseline2" do |env| |
| 113 | + server_header(env) |
| 114 | + total = 0_i64 |
| 115 | + env.params.query.each do |_key, value| |
| 116 | + total += value.to_i64 rescue next |
| 117 | + end |
| 118 | + env.response.content_type = "text/plain" |
| 119 | + total.to_s |
| 120 | +end |
| 121 | + |
| 122 | +get "/json" do |env| |
| 123 | + server_header(env) |
| 124 | + if items_raw = DATASET_ITEMS |
| 125 | + items = process_items(items_raw) |
| 126 | + env.response.content_type = "application/json" |
| 127 | + {items: items, count: items.size}.to_json |
| 128 | + else |
| 129 | + env.response.status_code = 500 |
| 130 | + "No dataset" |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +get "/compression" do |env| |
| 135 | + server_header(env) |
| 136 | + if buf = LARGE_GZIP_BUF |
| 137 | + env.response.content_type = "application/json" |
| 138 | + env.response.headers["Content-Encoding"] = "gzip" |
| 139 | + env.response.write(buf) |
| 140 | + nil |
| 141 | + else |
| 142 | + env.response.status_code = 500 |
| 143 | + "No dataset" |
| 144 | + end |
| 145 | +end |
| 146 | + |
| 147 | +get "/db" do |env| |
| 148 | + server_header(env) |
| 149 | + env.response.content_type = "application/json" |
| 150 | + |
| 151 | + unless DB_AVAILABLE |
| 152 | + next %({"items":[],"count":0}) |
| 153 | + end |
| 154 | + |
| 155 | + min_val = (env.params.query["min"]?.try(&.to_f) || 10.0) |
| 156 | + max_val = (env.params.query["max"]?.try(&.to_f) || 50.0) |
| 157 | + |
| 158 | + db = DBPool.get |
| 159 | + items = [] of Hash(String, JSON::Any) |
| 160 | + |
| 161 | + db.query(DB_QUERY, min_val, max_val) do |rs| |
| 162 | + rs.each do |
| 163 | + item = Hash(String, JSON::Any).new |
| 164 | + item["id"] = JSON::Any.new(rs.read(Int64)) |
| 165 | + item["name"] = JSON::Any.new(rs.read(String)) |
| 166 | + item["category"] = JSON::Any.new(rs.read(String)) |
| 167 | + item["price"] = JSON::Any.new(rs.read(Float64)) |
| 168 | + item["quantity"] = JSON::Any.new(rs.read(Int64)) |
| 169 | + item["active"] = JSON::Any.new(rs.read(Int64) != 0) |
| 170 | + tags_raw = rs.read(String) |
| 171 | + item["tags"] = JSON::Any.new(Array(JSON::Any).from_json(tags_raw)) |
| 172 | + rating_score = rs.read(Float64) |
| 173 | + rating_count = rs.read(Int64) |
| 174 | + item["rating"] = JSON::Any.new({"score" => JSON::Any.new(rating_score), "count" => JSON::Any.new(rating_count)}) |
| 175 | + items << item |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + {items: items, count: items.size}.to_json |
| 180 | +end |
| 181 | + |
| 182 | +post "/upload" do |env| |
| 183 | + server_header(env) |
| 184 | + body = env.request.body.try(&.gets_to_end) || "" |
| 185 | + env.response.content_type = "text/plain" |
| 186 | + body.bytesize.to_s |
| 187 | +end |
| 188 | + |
| 189 | +# --------------------------------------------------------------------------- |
| 190 | +# Server config |
| 191 | +# --------------------------------------------------------------------------- |
| 192 | + |
| 193 | +Kemal.config.port = 8080 |
| 194 | +Kemal.config.env = "production" |
| 195 | +Kemal.config.shutdown_message = false |
| 196 | +Kemal.config.logging = false |
| 197 | + |
| 198 | +Kemal.run |
0 commit comments