Lua Script Performance Testing: Encapsulating HTTP/HTTPS GET and POST with JSON, FormData, and File Support
This article explains how to use Lua scripts for performance testing by providing reusable functions that encapsulate HTTP and HTTPS GET/POST requests, support JSON and FormData payloads, and enable file uploads, complete with full code examples.
Performance testing is a crucial step in software development to ensure system stability and efficiency, and Lua, a lightweight scripting language, can be employed to write performance test scripts.
Lua is widely used in game development, embedded systems, and server‑side applications due to its fast execution and flexibility; using it for performance testing helps evaluate execution speed and resource consumption.
The following examples show how to encapsulate GET requests for both HTTP and HTTPS using the LuaSocket and LuaSec libraries.
local http = require("socket.http")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local json = require("json")
-- HTTP GET
function httpGet(url)
local response = {}
local _, status = http.request{
url = url,
method = "GET",
sink = ltn12.sink.table(response)
}
if status == 200 then
return table.concat(response)
else
return nil
end
end
-- HTTPS GET
function httpsGet(url)
local response = {}
local _, status = https.request{
url = url,
method = "GET",
sink = ltn12.sink.table(response)
}
if status == 200 then
return table.concat(response)
else
return nil
end
end
-- Example usage
local result = httpGet("http://example.com")
print(result)
result = httpsGet("https://example.com")
print(result)Similarly, POST requests can be wrapped for both HTTP and HTTPS, allowing custom headers and request bodies.
local http = require("socket.http")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local json = require("json")
local mime = require("mime")
-- HTTP POST
function httpPost(url, data, contentType)
local response = {}
local requestBody = mime.b64(data)
local _, status = http.request{
url = url,
method = "POST",
headers = {
["Content-Type"] = contentType,
["Content-Length"] = #requestBody
},
source = ltn12.source.string(requestBody),
sink = ltn12.sink.table(response)
}
if status == 200 then
return table.concat(response)
else
return nil
end
end
-- HTTPS POST
function httpsPost(url, data, contentType)
local response = {}
local requestBody = mime.b64(data)
local _, status = https.request{
url = url,
method = "POST",
headers = {
["Content-Type"] = contentType,
["Content-Length"] = #requestBody
},
source = ltn12.source.string(requestBody),
sink = ltn12.sink.table(response)
}
if status == 200 then
return table.concat(response)
else
return nil
end
end
-- Example usage
local result = httpPost("http://example.com", "data", "application/json")
print(result)
result = httpsPost("https://example.com", "data", "application/json")
print(result)The next example demonstrates how to handle JSON and multipart/form‑data payloads and how to read files for requests.
local http = require("socket.http")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local json = require("json")
local mime = require("mime")
-- POST supporting JSON and FormData, with file upload
function httpPost(url, data, contentType)
local response = {}
local requestBody
if contentType == "application/json" then
requestBody = json.encode(data)
elseif contentType == "multipart/form-data" then
requestBody = mime.easymime(data)
end
local _, status = http.request{
url = url,
method = "POST",
headers = {
["Content-Type"] = contentType,
["Content-Length"] = #requestBody
},
source = ltn12.source.string(requestBody),
sink = ltn12.sink.table(response)
}
if status == 200 then
return table.concat(response)
else
return nil
end
end
-- Example: send JSON
local jsonData = { key = "value" }
local result = httpPost("http://example.com", jsonData, "application/json")
print(result)
-- Example: send FormData with file
local formData = {
file = {
data = "path/to/file",
filename = "filename.txt",
contentType = "text/plain"
}
}
local result = httpPost("http://example.com", formData, "multipart/form-data")
print(result)By using these reusable functions, you can flexibly simulate real‑world GET and POST requests over HTTP/HTTPS, handle different content types, and incorporate file uploads, enabling thorough performance evaluation of your Lua‑based services.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.