-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests) add performance test (#112)
- Loading branch information
Showing
6 changed files
with
293 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
local ffi = require "ffi" | ||
local C = ffi.C | ||
local ITER = 2000 | ||
|
||
local get_duration | ||
do | ||
ffi.cdef [[ | ||
|
||
typedef long time_t; | ||
typedef int clockid_t; | ||
typedef struct timespec { | ||
time_t tv_sec; /* seconds */ | ||
long tv_nsec; /* nanoseconds */ | ||
} nanotime; | ||
|
||
int clock_gettime(clockid_t clk_id, struct timespec *tp); | ||
|
||
]] | ||
local time_ns | ||
do | ||
local nanop = ffi.new("nanotime[1]") | ||
function time_ns() | ||
-- CLOCK_REALTIME -> 0 | ||
C.clock_gettime(0, nanop) | ||
local t = nanop[0] | ||
|
||
return tonumber(t.tv_sec) * 1e9 + tonumber(t.tv_nsec) | ||
end | ||
end | ||
|
||
local last = 0 | ||
get_duration = function() | ||
local n = time_ns() | ||
local d = n - last | ||
last = n | ||
return d | ||
end | ||
end | ||
|
||
local function hmt(t) | ||
if t > 1e9 * 0.01 then | ||
return string.format("%.3f s", t/1e9) | ||
elseif t > 1e6 * 0.01 then | ||
return string.format("%.3f ms", t/1e6) | ||
else | ||
return string.format("%d ns", t) | ||
end | ||
end | ||
|
||
-- return sum, avg, max | ||
local function stat(t) | ||
if not t then | ||
return 0, 0, 0 | ||
end | ||
|
||
local v = 0 | ||
local max = 0 | ||
for _, i in ipairs(t) do | ||
v = v + i | ||
if i > max then | ||
max = i | ||
end | ||
end | ||
return v, v/#t, max | ||
end | ||
|
||
local function test(desc, r, iter) | ||
print("RUNNING " .. ITER .. " ITERATIONS FOR " .. desc) | ||
local data = table.new(ITER, 0) | ||
for i=1, ITER do | ||
get_duration() | ||
local ok, err = r() | ||
data[i] = get_duration() | ||
assert(ok, err) | ||
end | ||
|
||
local sum, avg, max = stat(data) | ||
|
||
print(string.format("FINISHED in\t%s (%d op/s)\nAVG\t%s\tMAX\t%s", hmt(sum), 1e9/avg, hmt(avg), hmt(max))) | ||
print(string.rep("-", 64)) | ||
end | ||
|
||
local function set_iteration(i) | ||
ITER = i | ||
end | ||
|
||
print("LOADING TEST FROM " .. arg[0]) | ||
print(string.rep("=", 64)) | ||
|
||
return { | ||
test = test, | ||
set_iteration = set_iteration, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
local path = debug.getinfo(1, "S").source:sub(2):match("(.*/)") | ||
package.path = path .. "/?.lua;" .. package.path | ||
|
||
local test = require "framework".test | ||
local set_iteration = require "framework".set_iteration | ||
local cipher = require "resty.openssl.cipher" | ||
local version = require("resty.openssl.version") | ||
|
||
local key = string.rep("0", 32) | ||
local iv = string.rep("0", 16) | ||
local data = string.rep("1", 4096) | ||
local aad = string.rep("2", 10) | ||
|
||
set_iteration(100000) | ||
|
||
for _, t in ipairs({"aes-256-cbc", "aes-256-gcm", "chacha20-poly1305"}) do | ||
for _, op in ipairs({"encrypt", "decrypt"}) do | ||
-- the fips version of boringssl we used seems don't have chacha20 | ||
if t == "chacha20-poly1305" and (not version.OPENSSL_111_OR_LATER or version.BORINGSSL) then | ||
goto continue | ||
end | ||
|
||
local c = assert(cipher.new(t)) | ||
local _iv = iv | ||
local _aad | ||
if t == "aes-256-gcm" or t == "chacha20-poly1305" then | ||
_iv = string.rep("0", 12) | ||
_aad = aad | ||
end | ||
|
||
if op == "encrypt" then | ||
test("encrypt with " .. t .. " on " .. #data .. " bytes", function() | ||
return c:encrypt(key, _iv, data, false, _aad) | ||
end) | ||
|
||
else | ||
local ciphertext = assert(c:encrypt(key, _iv, data, false, _aad)) | ||
|
||
local tag | ||
if t == "aes-256-gcm" or t == "chacha20-poly1305" then | ||
tag = assert(c:get_aead_tag()) | ||
end | ||
test("decrypt with " .. t .. " on " .. #ciphertext .. " bytes", function() | ||
return c:decrypt(key, _iv, ciphertext, false, _aad, tag) | ||
end) | ||
end | ||
::continue:: | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
local path = debug.getinfo(1, "S").source:sub(2):match("(.*/)") | ||
package.path = path .. "/?.lua;" .. package.path | ||
|
||
local test = require "framework".test | ||
local set_iteration = require "framework".set_iteration | ||
local pkey = require "resty.openssl.pkey" | ||
local version = require("resty.openssl.version") | ||
local data = string.rep("=", 200) | ||
|
||
set_iteration(1000) | ||
|
||
local rsa = pkey.new({ type = "RSA", bits = 4096 }) | ||
|
||
for _, op in ipairs({"encrypt", "decrypt"}) do | ||
if op == "encrypt" then | ||
test("encrypt with RSA on " .. #data .. " bytes", function() | ||
return rsa:encrypt(data) | ||
end) | ||
|
||
else | ||
|
||
local ciphertext = assert(rsa:encrypt(data)) | ||
test("decrypt with RSA on " .. #ciphertext .. " bytes", function() | ||
return rsa:decrypt(ciphertext) | ||
end) | ||
end | ||
end | ||
|
||
|
||
for _, t in ipairs({"RSA", "EC", "Ed25519", "Ed448"}) do | ||
for _, op in ipairs({"sign", "verify"}) do | ||
-- the fips version of boringssl we used seems don't have ed448 | ||
if (t == "Ed25519" and not version.OPENSSL_111_OR_LATER) or (t == "Ed448" and version.BORINGSSL) then | ||
goto continue | ||
end | ||
|
||
local opts = { type = t } | ||
if t == "EC" then | ||
opts.curve = "prime256v1" | ||
elseif t == "RSA" then | ||
opts.bits = 4096 | ||
end | ||
|
||
local c = assert(pkey.new(opts)) | ||
local md_alg | ||
if t == "RSA" or t == "EC" then | ||
md_alg = "SHA256" | ||
end | ||
|
||
if op == "sign" then | ||
test("sign with " .. t .. (md_alg and ("-" .. md_alg) or "") .. " on " .. #data .. " bytes", function() | ||
return c:sign(data, md_alg) | ||
end) | ||
|
||
else | ||
local sig = assert(c:sign(data, md_alg)) | ||
|
||
test("verify with " .. t .. (md_alg and ("-" .. md_alg) or "") .. " on " .. #data .. " bytes", function() | ||
return c:verify(sig, data, md_alg) | ||
end) | ||
end | ||
::continue:: | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
local path = debug.getinfo(1, "S").source:sub(2):match("(.*/)") | ||
package.path = path .. "/?.lua;" .. package.path | ||
|
||
local test = require "framework".test | ||
local pkey = require "resty.openssl.pkey" | ||
local example_pkey = assert(pkey.new()) | ||
|
||
for _, op in ipairs({"load", "export"}) do | ||
for _, t in ipairs({"PEM", "DER", "JWK"}) do | ||
for _, p in ipairs({"public", "private"}) do | ||
|
||
if op == "load" then | ||
local txt = assert(example_pkey:tostring(p, t)) | ||
local opts = { | ||
format = t, | ||
} | ||
if t ~= "JWK" then | ||
opts.type = p == "public" and "pu" or "pr" | ||
end | ||
|
||
test("load " .. t .. " " .. p .. " key", function() | ||
return pkey.new(txt, opts) | ||
end) | ||
|
||
else | ||
test("export " .. t .. " " .. p .. " key", function() | ||
return example_pkey:tostring(p, t) | ||
end) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
local path = debug.getinfo(1, "S").source:sub(2):match("(.*/)") | ||
package.path = path .. "/?.lua;" .. package.path | ||
|
||
local test = require "framework".test | ||
local x509 = require "resty.openssl.x509" | ||
local cert = assert(io.open(path .. "../../t/fixtures/Github.pem")):read("*a") | ||
local example_x509 = assert(x509.new(cert)) | ||
|
||
for _, op in ipairs({"load", "export"}) do | ||
for _, t in ipairs({"PEM", "DER"}) do | ||
if op == "load" then | ||
local txt = assert(example_x509:tostring(t)) | ||
test("load " .. t .. " x509", function() | ||
return x509.new(txt, t) | ||
end) | ||
|
||
else | ||
test("export " .. t .. " x509", function() | ||
return example_x509:tostring(t) | ||
end) | ||
end | ||
end | ||
end |