|
| 1 | +local cjson = require "cjson" |
| 2 | + |
| 3 | +local _M = {} |
| 4 | + |
| 5 | +local json_resp = "" |
| 6 | +local large_resp = "" |
| 7 | +local static_files = {} |
| 8 | + |
| 9 | +function _M.init() |
| 10 | + local mime = { |
| 11 | + css = "text/css", js = "application/javascript", html = "text/html", |
| 12 | + woff2 = "font/woff2", svg = "image/svg+xml", webp = "image/webp", |
| 13 | + json = "application/json", |
| 14 | + } |
| 15 | + |
| 16 | + -- Load small dataset |
| 17 | + local f = io.open(os.getenv("DATASET_PATH") or "/data/dataset.json", "r") |
| 18 | + if f then |
| 19 | + local data = cjson.decode(f:read("*a")) |
| 20 | + f:close() |
| 21 | + for _, item in ipairs(data) do |
| 22 | + item.total = math.floor(item.price * item.quantity * 100 + 0.5) / 100 |
| 23 | + end |
| 24 | + json_resp = cjson.encode({items = data, count = #data}) |
| 25 | + end |
| 26 | + |
| 27 | + -- Load large dataset |
| 28 | + f = io.open("/data/dataset-large.json", "r") |
| 29 | + if f then |
| 30 | + local data = cjson.decode(f:read("*a")) |
| 31 | + f:close() |
| 32 | + for _, item in ipairs(data) do |
| 33 | + item.total = math.floor(item.price * item.quantity * 100 + 0.5) / 100 |
| 34 | + end |
| 35 | + large_resp = cjson.encode({items = data, count = #data}) |
| 36 | + end |
| 37 | + |
| 38 | + -- Load static files |
| 39 | + local handle = io.popen("ls /data/static 2>/dev/null") |
| 40 | + if handle then |
| 41 | + for name in handle:lines() do |
| 42 | + local file = io.open("/data/static/" .. name, "rb") |
| 43 | + if file then |
| 44 | + local ext = name:match("%.(%w+)$") |
| 45 | + static_files[name] = { |
| 46 | + data = file:read("*a"), |
| 47 | + ct = mime[ext] or "application/octet-stream", |
| 48 | + } |
| 49 | + file:close() |
| 50 | + end |
| 51 | + end |
| 52 | + handle:close() |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +local function sum_args() |
| 57 | + local args = ngx.req.get_uri_args() |
| 58 | + local sum = 0 |
| 59 | + for _, v in pairs(args) do |
| 60 | + if type(v) == "table" then |
| 61 | + for _, val in ipairs(v) do |
| 62 | + sum = sum + (tonumber(val) or 0) |
| 63 | + end |
| 64 | + else |
| 65 | + sum = sum + (tonumber(v) or 0) |
| 66 | + end |
| 67 | + end |
| 68 | + return sum |
| 69 | +end |
| 70 | + |
| 71 | +function _M.pipeline() |
| 72 | + ngx.header["Content-Type"] = "text/plain" |
| 73 | + ngx.print("ok") |
| 74 | +end |
| 75 | + |
| 76 | +function _M.baseline11() |
| 77 | + local sum = sum_args() |
| 78 | + if ngx.req.get_method() == "POST" then |
| 79 | + ngx.req.read_body() |
| 80 | + local body = ngx.req.get_body_data() |
| 81 | + if body then |
| 82 | + sum = sum + (tonumber(body) or 0) |
| 83 | + end |
| 84 | + end |
| 85 | + ngx.header["Content-Type"] = "text/plain" |
| 86 | + ngx.print(string.format("%d", sum)) |
| 87 | +end |
| 88 | + |
| 89 | +function _M.baseline2() |
| 90 | + ngx.header["Content-Type"] = "text/plain" |
| 91 | + ngx.print(string.format("%d", sum_args())) |
| 92 | +end |
| 93 | + |
| 94 | +function _M.json() |
| 95 | + if json_resp == "" then |
| 96 | + ngx.status = 500 |
| 97 | + ngx.print("dataset not loaded") |
| 98 | + return |
| 99 | + end |
| 100 | + ngx.header["Content-Type"] = "application/json" |
| 101 | + ngx.print(json_resp) |
| 102 | +end |
| 103 | + |
| 104 | +function _M.compression() |
| 105 | + if large_resp == "" then |
| 106 | + ngx.status = 500 |
| 107 | + ngx.print("dataset not loaded") |
| 108 | + return |
| 109 | + end |
| 110 | + ngx.header["Content-Type"] = "application/json" |
| 111 | + ngx.print(large_resp) |
| 112 | +end |
| 113 | + |
| 114 | +function _M.upload() |
| 115 | + ngx.req.read_body() |
| 116 | + local body = ngx.req.get_body_data() |
| 117 | + if not body then |
| 118 | + local fname = ngx.req.get_body_file() |
| 119 | + if fname then |
| 120 | + local f = io.open(fname, "rb") |
| 121 | + if f then |
| 122 | + body = f:read("*a") |
| 123 | + f:close() |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + if not body then |
| 128 | + ngx.status = 400 |
| 129 | + ngx.print("no body") |
| 130 | + return |
| 131 | + end |
| 132 | + ngx.header["Content-Type"] = "text/plain" |
| 133 | + ngx.print(string.format("%08x", ngx.crc32_long(body))) |
| 134 | +end |
| 135 | + |
| 136 | +function _M.static_file() |
| 137 | + local name = ngx.var.uri:match("/static/(.+)") |
| 138 | + if not name then |
| 139 | + ngx.status = 404 |
| 140 | + ngx.print("not found") |
| 141 | + return |
| 142 | + end |
| 143 | + local sf = static_files[name] |
| 144 | + if not sf then |
| 145 | + ngx.status = 404 |
| 146 | + ngx.print("not found") |
| 147 | + return |
| 148 | + end |
| 149 | + ngx.header["Content-Type"] = sf.ct |
| 150 | + ngx.print(sf.data) |
| 151 | +end |
| 152 | + |
| 153 | +return _M |
0 commit comments