Skip to content

Commit

Permalink
all tests are green
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Dec 17, 2023
1 parent 5964033 commit 97e1c9c
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 269 deletions.
2 changes: 1 addition & 1 deletion benchmark/match-parameter2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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"
local path = "/user100000/gordon/a/b/c/d/e/f/g/h/i/j/k/l"
for _ = 1, match_times do
res = rx:match(path)
end
Expand Down
27 changes: 27 additions & 0 deletions benchmark/match-prefix.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

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

local rx = radix.new(routes)

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

local res
local path = "/" .. ngx.md5(500) .. "/a"
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))
153 changes: 68 additions & 85 deletions spec/trie_spec.lua
Original file line number Diff line number Diff line change
@@ -1,96 +1,79 @@
--require 'busted.runner'()
require 'busted.runner'()

local trie = require "router-tree.trie"
local Trie = require "router-tree.trie"
local cjson = require "cjson"
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)
describe("Trie", function()
it("", function()
local root = Trie.new()
root:add("key", "value")
assert.equal("key", root.path)
assert.equal("value", root.value)
end)

it("", function()
local root = Trie.new()
root:add("cat", "cat")
root:add("dog", "dog")
--print(inspect(root))
--print(cjson.encode(root))
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
it("需要修复", function()
local root = Trie.new()
root:add("/:name/foo", "1")
root:add("/:name/foo*", "2")
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
it("sanity", function()
local paths = {
"/hi",
"/contact",
"/co",
"/c",
"/a",
"/ab",
"/doc/",
"/doc/go_faq.html",
"/doc/go1.html",
--"/α",
--"/β",
}
local root = Trie.new()
for _, path in ipairs(paths) do
root:add(path, path)
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
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:traverse(request[1])
assert(request[2] == v)
end
--print(inspect(root))
end)
end)

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

-- test params
local function test3()

local function test1()
local mobdebug = require "mobdebug"
mobdebug.start("localhost", 28172)
local root = Trie.new()
root:add("/:name/foo", "1")
root:add("/:name/foo*", "2")
print("doge")
end

--test1()
test2()
test1()
8 changes: 1 addition & 7 deletions src/resty/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local BYTE_COLON = byte(":")
local BYTE_ASTERISK = byte("*")

local Parser = {}
local mt = {__index = Parser }
local mt = { __index = Parser }

local debug = false

Expand All @@ -28,12 +28,6 @@ function Parser.new(path)
return setmetatable(self, mt)
end

-- /info/:user/project/:sub -> ["/info/", ":user", "/project/", ":sub"]
-- /api/v1/pay/*
-- /api/v1/pay/*path

-- /users/:id/xxx.*

function Parser.next(self)
local c

Expand Down
1 change: 1 addition & 0 deletions src/resty/parser/ant.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- TODO
Loading

0 comments on commit 97e1c9c

Please sign in to comment.