refl: add hono Context
This commit is contained in:
+79
-58
@@ -11,76 +11,97 @@ fn main() {
|
||||
mut app := hono.new_hono()
|
||||
|
||||
// 添加静态路由
|
||||
app.get('/api/users', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"users": []}')
|
||||
app.get('/api/users', fn (mut c hono.Context) http.Response {
|
||||
return c.json('{"users": []}')
|
||||
})
|
||||
|
||||
app.get('/api/posts', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"posts": []}')
|
||||
app.get('/api/posts', fn (mut c hono.Context) http.Response {
|
||||
return c.json('{"posts": []}')
|
||||
})
|
||||
|
||||
app.get('/api/comments', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"comments": []}')
|
||||
app.get('/api/comments', fn (mut c hono.Context) http.Response {
|
||||
return c.json('{"comments": []}')
|
||||
})
|
||||
|
||||
// 添加动态路由
|
||||
app.get('/api/users/:id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"user_id": "${req.param["id"]}"}')
|
||||
app.get('/api/users/:id', fn (mut c hono.Context) http.Response {
|
||||
user_id := c.params['id']
|
||||
return c.json('{"id": "${user_id}"}')
|
||||
})
|
||||
|
||||
app.get('/api/posts/:id/comments', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"post_id": "${req.param["id"]}", "comments": []}')
|
||||
app.get('/api/posts/:id/comments', fn (mut c hono.Context) http.Response {
|
||||
post_id := c.params['id']
|
||||
return c.json('{"post_id": "${post_id}", "comments": []}')
|
||||
})
|
||||
|
||||
app.get('/api/**/search', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"search": "wildcard"}')
|
||||
app.get('/api/**/search', fn (mut c hono.Context) http.Response {
|
||||
return c.json('{"search": "wildcard"}')
|
||||
})
|
||||
|
||||
// 添加100个动态路由
|
||||
println('添加100个动态路由...')
|
||||
for i := 1; i <= 100; i++ {
|
||||
// 用户相关路由
|
||||
app.get('/api/users/:id/profile', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"user_id": "${req.param["id"]}", "profile": "data"}')
|
||||
app.get('/api/users/:id/profile', fn (mut c hono.Context) http.Response {
|
||||
user_id := c.params['id']
|
||||
return c.json('{"user_id": "${user_id}", "profile": {}}')
|
||||
})
|
||||
|
||||
app.get('/api/users/:id/posts/:post_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"user_id": "${req.param["id"]}", "post_id": "${req.param["post_id"]}"}')
|
||||
app.get('/api/users/:id/posts/:post_id', fn (mut c hono.Context) http.Response {
|
||||
user_id := c.params['id']
|
||||
post_id := c.params['post_id']
|
||||
return c.json('{"user_id": "${user_id}", "post_id": "${post_id}"}')
|
||||
})
|
||||
|
||||
app.get('/api/users/:id/comments/:comment_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"user_id": "${req.param["id"]}", "comment_id": "${req.param["comment_id"]}"}')
|
||||
app.get('/api/users/:id/comments/:comment_id', fn (mut c hono.Context) http.Response {
|
||||
user_id := c.params['id']
|
||||
comment_id := c.params['comment_id']
|
||||
return c.json('{"user_id": "${user_id}", "comment_id": "${comment_id}"}')
|
||||
})
|
||||
|
||||
// 帖子相关路由
|
||||
app.get('/api/posts/:id/author/:author_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"post_id": "${req.param["id"]}", "author_id": "${req.param["author_id"]}"}')
|
||||
app.get('/api/posts/:id/author/:author_id', fn (mut c hono.Context) http.Response {
|
||||
post_id := c.params['id']
|
||||
author_id := c.params['author_id']
|
||||
return c.json('{"post_id": "${post_id}", "author_id": "${author_id}"}')
|
||||
})
|
||||
|
||||
app.get('/api/posts/:id/tags/:tag_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"post_id": "${req.param["id"]}", "tag_id": "${req.param["tag_id"]}"}')
|
||||
app.get('/api/posts/:id/tags/:tag_id', fn (mut c hono.Context) http.Response {
|
||||
post_id := c.params['id']
|
||||
tag_id := c.params['tag_id']
|
||||
return c.json('{"post_id": "${post_id}", "tag_id": "${tag_id}"}')
|
||||
})
|
||||
|
||||
app.get('/api/posts/:id/categories/:category_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"post_id": "${req.param["id"]}", "category_id": "${req.param["category_id"]}"}')
|
||||
app.get('/api/posts/:id/categories/:category_id', fn (mut c hono.Context) http.Response {
|
||||
post_id := c.params['id']
|
||||
category_id := c.params['category_id']
|
||||
return c.json('{"post_id": "${post_id}", "category_id": "${category_id}"}')
|
||||
})
|
||||
|
||||
// 评论相关路由
|
||||
app.get('/api/comments/:id/author/:author_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"comment_id": "${req.param["id"]}", "author_id": "${req.param["author_id"]}"}')
|
||||
app.get('/api/comments/:id/author/:author_id', fn (mut c hono.Context) http.Response {
|
||||
comment_id := c.params['id']
|
||||
author_id := c.params['author_id']
|
||||
return c.json('{"comment_id": "${comment_id}", "author_id": "${author_id}"}')
|
||||
})
|
||||
|
||||
app.get('/api/comments/:id/post/:post_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"comment_id": "${req.param["id"]}", "post_id": "${req.param["post_id"]}"}')
|
||||
app.get('/api/comments/:id/post/:post_id', fn (mut c hono.Context) http.Response {
|
||||
comment_id := c.params['id']
|
||||
post_id := c.params['post_id']
|
||||
return c.json('{"comment_id": "${comment_id}", "post_id": "${post_id}"}')
|
||||
})
|
||||
|
||||
// 分类相关路由
|
||||
app.get('/api/categories/:id/posts/:post_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"category_id": "${req.param["id"]}", "post_id": "${req.param["post_id"]}"}')
|
||||
app.get('/api/categories/:id/posts/:post_id', fn (mut c hono.Context) http.Response {
|
||||
category_id := c.params['id']
|
||||
post_id := c.params['post_id']
|
||||
return c.json('{"category_id": "${category_id}", "post_id": "${post_id}"}')
|
||||
})
|
||||
|
||||
app.get('/api/categories/:id/tags/:tag_id', fn (req hono.Request) http.Response {
|
||||
return hono.Response.json('{"category_id": "${req.param["id"]}", "tag_id": "${req.param["tag_id"]}"}')
|
||||
app.get('/api/categories/:id/tags/:tag_id', fn (mut c hono.Context) http.Response {
|
||||
category_id := c.params['id']
|
||||
tag_id := c.params['tag_id']
|
||||
return c.json('{"category_id": "${category_id}", "tag_id": "${tag_id}"}')
|
||||
})
|
||||
}
|
||||
|
||||
@@ -88,9 +109,9 @@ fn main() {
|
||||
println('\n--- 静态路由性能测试 ---')
|
||||
mut start := time.now()
|
||||
for i := 0; i < 1000000; i++ {
|
||||
_ = app.hybrid_router.match_route('GET', '/api/users')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/posts')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/comments')
|
||||
app.context_hybrid_router.match_route('GET', '/api/users')
|
||||
app.context_hybrid_router.match_route('GET', '/api/posts')
|
||||
app.context_hybrid_router.match_route('GET', '/api/comments')
|
||||
}
|
||||
mut end := time.now()
|
||||
println('静态路由 1000000次匹配耗时: ${end - start}')
|
||||
@@ -99,13 +120,13 @@ fn main() {
|
||||
println('\n--- 动态路由性能测试(103个动态路由) ---')
|
||||
start = time.now()
|
||||
for i := 0; i < 1000000; i++ { // 减少测试次数,因为路由数量大幅增加
|
||||
_ = app.hybrid_router.match_route('GET', '/api/users/123')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/posts/456/comments')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/anything/search')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/users/789/profile')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/posts/101/author/202')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/comments/303/post/404')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/categories/505/tags/606')
|
||||
app.context_hybrid_router.match_route('GET', '/api/users/123')
|
||||
app.context_hybrid_router.match_route('GET', '/api/posts/456/comments')
|
||||
app.context_hybrid_router.match_route('GET', '/api/anything/search')
|
||||
app.context_hybrid_router.match_route('GET', '/api/users/789/profile')
|
||||
app.context_hybrid_router.match_route('GET', '/api/posts/101/author/202')
|
||||
app.context_hybrid_router.match_route('GET', '/api/comments/303/post/404')
|
||||
app.context_hybrid_router.match_route('GET', '/api/categories/505/tags/606')
|
||||
}
|
||||
end = time.now()
|
||||
println('动态路由 1000000次匹配耗时: ${end - start}')
|
||||
@@ -114,7 +135,7 @@ fn main() {
|
||||
println('\n--- 缓存效果测试 ---')
|
||||
start = time.now()
|
||||
for i := 0; i < 1000000; i++ {
|
||||
_ = app.hybrid_router.match_route('GET', '/api/users/123')
|
||||
app.context_hybrid_router.match_route('GET', '/api/users/123')
|
||||
}
|
||||
end = time.now()
|
||||
println('缓存命中 1000000次匹配耗时: ${end - start}')
|
||||
@@ -124,11 +145,11 @@ fn main() {
|
||||
start = time.now()
|
||||
for i := 0; i < 10000; i++ {
|
||||
// 测试不同路由的匹配
|
||||
_ = app.hybrid_router.match_route('GET', '/api/users/${i}')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/posts/${i}/comments')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/users/${i}/profile')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/posts/${i}/author/${i+1}')
|
||||
_ = app.hybrid_router.match_route('GET', '/api/comments/${i}/post/${i+1}')
|
||||
app.context_hybrid_router.match_route('GET', '/api/users/${i}')
|
||||
app.context_hybrid_router.match_route('GET', '/api/posts/${i}/comments')
|
||||
app.context_hybrid_router.match_route('GET', '/api/users/${i}/profile')
|
||||
app.context_hybrid_router.match_route('GET', '/api/posts/${i}/author/${i+1}')
|
||||
app.context_hybrid_router.match_route('GET', '/api/comments/${i}/post/${i+1}')
|
||||
}
|
||||
end = time.now()
|
||||
println('路由查找 50000次匹配耗时: ${end - start}')
|
||||
@@ -144,22 +165,22 @@ fn main() {
|
||||
println('\n--- Trie 路由树性能测试 ---')
|
||||
start = time.now()
|
||||
for i := 0; i < 1000000; i++ {
|
||||
_ = app.trie_router.match_route('GET', '/api/users')
|
||||
_ = app.trie_router.match_route('GET', '/api/posts')
|
||||
_ = app.trie_router.match_route('GET', '/api/comments')
|
||||
app.context_trie_router.match_route('GET', '/api/users')
|
||||
app.context_trie_router.match_route('GET', '/api/posts')
|
||||
app.context_trie_router.match_route('GET', '/api/comments')
|
||||
}
|
||||
end = time.now()
|
||||
println('Trie静态路由 1000000次匹配耗时: ${end - start}')
|
||||
|
||||
start = time.now()
|
||||
for i := 0; i < 1000000; i++ {
|
||||
_ = app.trie_router.match_route('GET', '/api/users/123')
|
||||
_ = app.trie_router.match_route('GET', '/api/posts/456/comments')
|
||||
_ = app.trie_router.match_route('GET', '/api/anything/search')
|
||||
_ = app.trie_router.match_route('GET', '/api/users/789/profile')
|
||||
_ = app.trie_router.match_route('GET', '/api/posts/101/author/202')
|
||||
_ = app.trie_router.match_route('GET', '/api/comments/303/post/404')
|
||||
_ = app.trie_router.match_route('GET', '/api/categories/505/tags/606')
|
||||
app.context_trie_router.match_route('GET', '/api/users/123')
|
||||
app.context_trie_router.match_route('GET', '/api/posts/456/comments')
|
||||
app.context_trie_router.match_route('GET', '/api/anything/search')
|
||||
app.context_trie_router.match_route('GET', '/api/users/789/profile')
|
||||
app.context_trie_router.match_route('GET', '/api/posts/101/author/202')
|
||||
app.context_trie_router.match_route('GET', '/api/comments/303/post/404')
|
||||
app.context_trie_router.match_route('GET', '/api/categories/505/tags/606')
|
||||
}
|
||||
end = time.now()
|
||||
println('Trie动态路由 1000000次匹配耗时: ${end - start}')
|
||||
|
||||
Reference in New Issue
Block a user