Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Dec 15, 2023
1 parent e9ca3a1 commit 94830da
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 1,715 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
build:
luarocks make

test:
busted spec
test: build
luajit spec/utils_spec.lua

bench: build
resty -I=./lib -I=./deps/share/lua/5.1 benchmark/match-parameter.lua

28 changes: 28 additions & 0 deletions benchmark/match-parameter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local radix = require("router-tree")
local route_count = 1000 * 100
local match_times = 1000 * 1000 * 10

local routes = {}
for i = 1, route_count do
routes[i] = {paths = {"/user" .. i .. "/:name"}, handler = i}
end

local rx = radix.new(routes)

ngx.update_time()
local start_time = ngx.now()

local res
local path = "/user1/gordon"
for _ = 1, match_times do
res = rx:match(path)
end

ngx.update_time()
local used_time = ngx.now() - start_time
ngx.say("matched res: ", res)
ngx.say("route count: ", route_count)
ngx.say("match times: ", match_times)
ngx.say("time used : ", used_time, " sec")
ngx.say("QPS : ", math.floor(match_times / used_time))
ngx.say("each time : ", used_time * 1000 * 1000 / match_times, " ns")
28 changes: 28 additions & 0 deletions benchmark/match-parameter2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local radix = require("router-tree")
local route_count = 1000 * 100
local match_times = 1000 * 1000 * 10

local routes = {}
for i = 1, route_count do
routes[i] = {paths = {"/user" .. i .. "/:name/a/b/c/d/e/f/g/h/i/j/k/l"}, handler = i}
end

local rx = radix.new(routes)

ngx.update_time()
local start_time = ngx.now()

local res
local path = "/user1/gordon/a/b/c/d/e/f/g/h/i/j/k/l"
for _ = 1, match_times do
res = rx:match(path)
end

ngx.update_time()
local used_time = ngx.now() - start_time
ngx.say("matched res: ", res)
ngx.say("route count: ", route_count)
ngx.say("match times: ", match_times)
ngx.say("time used : ", used_time, " sec")
ngx.say("QPS : ", math.floor(match_times / used_time))
ngx.say("each time : ", used_time * 1000 * 1000 / match_times, " ns")
27 changes: 27 additions & 0 deletions benchmark/match-static.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local radix = require("router-tree")
local route_count = 1000 * 100
local match_times = 1000 * 1000 * 10

local routes = {}
for i = 1, route_count do
routes[i] = {paths = {"/" .. ngx.md5(i)}, handler = i}
end

local rx = radix.new(routes)

ngx.update_time()
local start_time = ngx.now()

local res
local path = "/" .. ngx.md5(500)
for _ = 1, match_times do
res = rx:match(path)
end

ngx.update_time()
local used_time = ngx.now() - start_time
ngx.say("matched res: ", res)
ngx.say("route count: ", route_count)
ngx.say("match times: ", match_times)
ngx.say("time used : ", used_time, " sec")
ngx.say("QPS : ", math.floor(match_times / used_time))
36 changes: 36 additions & 0 deletions lua-resty-router-tree-dev-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local package_name = "lua-resty-router-tree"
local package_version = "dev"
local rockspec_revision = "1"
local github_account_name = "vm-001"
local github_repo_name = "lua-resty-router-tree"
local git_checkout = package_version == "dev" and "master" or ("version_"..package_version)

package = package_name
version = package_version .. "-" .. rockspec_revision

source = {
url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git",
branch = git_checkout
}

description = {
summary = "lua-resty-router-tree module for Lua 5.x",
detailed = [[
]],
license = "MIT",
homepage = "https://github.com/"..github_account_name.."/"..github_repo_name,
}

dependencies = {
"lua >= 5.0, < 5.5"
}

build = {
type = "builtin",
modules = {
["router-tree"] = "src/resty/router.lua",
["router-tree.parser"] = "src/resty/parser.lua",
["router-tree.trie"] = "src/resty/trie.lua",
["router-tree.utils"] = "src/resty/utils.lua",
},
}
2 changes: 1 addition & 1 deletion spec/router_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

--local router = require "router"
local router = require "router-tree"

describe("router", function()

Expand Down
Empty file removed spec/tree_spec.lua
Empty file.
91 changes: 91 additions & 0 deletions spec/trie_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
--require 'busted.runner'()

local trie = require "router-tree.trie"
local inspect = require "inspect"

--describe("test cases from httprouter", function()
-- it("test1", function()
-- local paths = {
-- "/hi",
-- "/contact",
-- "/co",
-- "/c",
-- --"/a",
-- --"/ab",
-- --"/doc/",
-- --"/doc/go_faq.html",
-- --"/doc/go1.html",
-- --"/α",
-- --"/β",
-- }
-- local mobdebug = require "mobdebug"
-- mobdebug.start("localhost", 28172)
-- local root = trie.new()
-- for _, path in ipairs(paths) do
-- root:add(path)
-- end
-- print(inspect(root))
-- end)
--end)


local function test1()
local paths = {
"/hi",
"/contact",
"/co",
"/c",
"/a",
"/ab",
"/doc/",
"/doc/go_faq.html",
"/doc/go1.html",
--"/α",
--"/β",
}
local mobdebug = require "mobdebug"
mobdebug.start("localhost", 28172)
local root = trie.new()
for _, path in ipairs(paths) do
root:add(path, path)
end

local requests = {
{"/a", "/a" },
{"/", nil },
{"/hi", "/hi" },
{"/contact", "/contact" },
{"/co", "/co" },
{"/con", nil },
{"/cona", nil },
{"/no", nil },
{"/ab", "/ab" },
--{"/α", "/α" },
--{"/β", "/β" },
}
for _, request in ipairs(requests) do
local v = root:get(request[1])
assert(request[2] == v)
end
--print(inspect(root))
end

local function test2()
local paths = {
"/user1/:name",
"/user2/:name",
"/user3/:name",
"/user4/:name",
}
local mobdebug = require "mobdebug"
mobdebug.start("localhost", 28172)
local root = trie.new()
for _, path in ipairs(paths) do
root:add(path, path)
end

print(root:get("/user4/doge"))
end

--test1()
test2()
15 changes: 15 additions & 0 deletions spec/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'busted.runner'()

local utils = require "router-tree.utils"

describe("utils", function()
it("starts_with", function()
assert.is_true(utils.starts_with("/abc", "/"))
assert.is_true(utils.starts_with("/abc", "/a"))
assert.is_true(utils.starts_with("/abc", "/ab"))
assert.is_true(utils.starts_with("/abc", "/abc"))

assert.is_false(utils.starts_with("/abc", "/abcd"))
assert.is_false(utils.starts_with("/abc", "/d"))
end)
end)
Loading

0 comments on commit 94830da

Please sign in to comment.