migr: migration json to x.json2
This commit is contained in:
@@ -478,7 +478,7 @@ fn test_integration(mut stats TestStats) {
|
||||
})
|
||||
|
||||
// 验证路由统计
|
||||
static_count, dynamic_count, cache_count, _ := app.get_router_stats()
|
||||
static_count, dynamic_count, _, _ := app.get_router_stats()
|
||||
|
||||
if static_count + dynamic_count < 2 {
|
||||
stats.fail_test('路由添加失败')
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import hono
|
||||
import time
|
||||
import net.http
|
||||
|
||||
fn main() {
|
||||
@@ -200,7 +199,7 @@ fn test_restful_api_patterns() {
|
||||
|
||||
mut restful_success := 0
|
||||
for test in restful_tests {
|
||||
if match_result := app.fast_router.match_route(test['method'], test['path']) {
|
||||
if _ := app.fast_router.match_route(test['method'], test['path']) {
|
||||
restful_success++
|
||||
println(' ✅ ${test['method']} ${test['path']} - ${test['description']}')
|
||||
} else {
|
||||
|
||||
@@ -400,7 +400,7 @@ fn test_comprehensive_performance() {
|
||||
mut complex_matches := 0
|
||||
|
||||
for _ in 0 .. test_iterations {
|
||||
if match_result := app.fast_router.match_route('GET', test['path']) {
|
||||
if _ := app.fast_router.match_route('GET', test['path']) {
|
||||
complex_matches++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import hono
|
||||
import net.http
|
||||
import json
|
||||
|
||||
fn main() {
|
||||
println('=== 统一错误处理系统测试 ===')
|
||||
|
||||
@@ -338,7 +338,7 @@ fn test_memory_management_integration() {
|
||||
// 测试缓存操作
|
||||
cache.put('test_key', test_data)
|
||||
|
||||
if cached := cache.get('test_key') {
|
||||
if _ := cache.get('test_key') {
|
||||
println(' ✅ 缓存存储和获取正常')
|
||||
} else {
|
||||
println(' ❌ 缓存存储或获取失败')
|
||||
|
||||
@@ -198,7 +198,7 @@ fn test_backward_compatibility() {
|
||||
mut router := hono.FastRouter.new()
|
||||
|
||||
// 测试原有接口是否保持兼容
|
||||
compatibility_tests := [
|
||||
_ := [
|
||||
'add_route() 接口',
|
||||
'match_route() 接口',
|
||||
'get_stats() 接口',
|
||||
@@ -264,7 +264,7 @@ fn test_new_features() {
|
||||
mut router := hono.FastRouter.new_with_cache_size(50)
|
||||
|
||||
// 新功能清单
|
||||
new_features := [
|
||||
_ := [
|
||||
'LRU 缓存机制',
|
||||
'TTL 过期管理',
|
||||
'健康状态检查',
|
||||
|
||||
@@ -74,7 +74,7 @@ fn test_large_scale_repeated_matching() {
|
||||
}
|
||||
|
||||
// 验证缓存没有增长(说明使用了缓存)
|
||||
regex_total_after, regex_compiled_after := router.get_regex_cache_stats()
|
||||
_, regex_compiled_after := router.get_regex_cache_stats()
|
||||
if regex_compiled_after == regex_compiled {
|
||||
println(' ✅ 缓存使用正常 - 没有额外的编译')
|
||||
} else {
|
||||
@@ -178,7 +178,7 @@ fn test_multi_route_cache_effect() {
|
||||
}
|
||||
|
||||
// 验证缓存状态
|
||||
regex_total_final, regex_compiled_final := router.get_regex_cache_stats()
|
||||
_, regex_compiled_final := router.get_regex_cache_stats()
|
||||
if regex_compiled_final == regex_compiled_after_first {
|
||||
println(' ✅ 缓存工作正常 - 没有额外编译')
|
||||
} else {
|
||||
|
||||
@@ -153,7 +153,7 @@ fn test_cache_hit_rate() {
|
||||
}
|
||||
|
||||
// 最终缓存状态
|
||||
regex_total_final, regex_compiled_final := router.get_regex_cache_stats()
|
||||
_, regex_compiled_final := router.get_regex_cache_stats()
|
||||
|
||||
if regex_compiled_final == regex_compiled_after_first {
|
||||
println(' ✅ 缓存命中率100% - 没有额外的编译')
|
||||
|
||||
@@ -87,7 +87,7 @@ fn test_step_by_step_breakdown() {
|
||||
|
||||
// 步骤4: 正则表达式缓存检查
|
||||
start_time4 := time.now()
|
||||
cached_regex := router.regex_cache[route_path]
|
||||
_ := router.regex_cache[route_path]
|
||||
step4_time := time.since(start_time4)
|
||||
println(' 步骤4 - 正则缓存检查: ${step4_time}')
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ fn (router NoCacheRouter) match_route_no_cache(method string, path string) ?hono
|
||||
replaced_path = replaced_path.replace('{', r'\{')
|
||||
replaced_path = replaced_path.replace('}', r'\}')
|
||||
replaced_path = replaced_path.replace('^', r'\^')
|
||||
replaced_path = replaced_path.replace('$', r'\$')
|
||||
replaced_path = replaced_path.replace(r'$', r'\$')
|
||||
replaced_path = replaced_path.replace('|', r'\|')
|
||||
|
||||
// 提取参数名并替换为命名捕获组
|
||||
@@ -68,7 +68,7 @@ fn (router NoCacheRouter) match_route_no_cache(method string, path string) ?hono
|
||||
return '(?P<${param_name}>[^/]+)'
|
||||
})
|
||||
|
||||
replaced_path = '^${replaced_path}$'
|
||||
replaced_path = '^${replaced_path}' + r'$'
|
||||
|
||||
// 每次都重新编译正则表达式(这是性能瓶颈)
|
||||
mut reg := regex.regex_opt(replaced_path) or { continue }
|
||||
@@ -384,4 +384,4 @@ fn test_cache_hit_rate() {
|
||||
|
||||
// 显示性能分析
|
||||
router.analyze_router_performance()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ fn test_step_by_step_performance() {
|
||||
|
||||
// 步骤3: 第二次匹配(使用缓存)
|
||||
start_time3 := time.now()
|
||||
result2 := router.match_route('GET', test_path)
|
||||
_ = router.match_route('GET', test_path)
|
||||
second_match_time := time.since(start_time3)
|
||||
println(' 第二次匹配时间 (使用缓存): ${second_match_time}')
|
||||
|
||||
@@ -58,31 +58,49 @@ fn test_step_by_step_performance() {
|
||||
println(' 路由缓存: ${cache_size}/${cache_capacity}')
|
||||
println(' 正则缓存: ${regex_compiled}/${regex_total}')
|
||||
|
||||
// 步骤5: 测试纯正则匹配时间
|
||||
// 步骤5: 测试纯正则匹配时间(安全访问)
|
||||
if cached := router.regex_cache[route_path] {
|
||||
if cached.compiled {
|
||||
start_time4 := time.now()
|
||||
for _ in 0 .. 1000 {
|
||||
cached.regex.matches_string(test_path)
|
||||
// 先验证正则能匹配
|
||||
if cached.regex.matches_string(test_path) {
|
||||
start_time4 := time.now()
|
||||
for _ in 0 .. 1000 {
|
||||
cached.regex.matches_string(test_path)
|
||||
}
|
||||
pure_regex_time := time.since(start_time4)
|
||||
avg_regex_time := f64(pure_regex_time.microseconds()) / 1000.0
|
||||
println(' 纯正则匹配平均时间: ${avg_regex_time:.3f}μs')
|
||||
} else {
|
||||
println(' ⚠️ 正则表达式无法匹配测试路径')
|
||||
}
|
||||
pure_regex_time := time.since(start_time4)
|
||||
avg_regex_time := f64(pure_regex_time.microseconds()) / 1000.0
|
||||
println(' 纯正则匹配平均时间: ${avg_regex_time:.3f}μs')
|
||||
}
|
||||
} else {
|
||||
println(' ⚠️ 正则缓存中未找到路由')
|
||||
}
|
||||
|
||||
// 步骤6: 测试参数提取时间
|
||||
// 步骤6: 测试参数提取时间(安全访问)
|
||||
if cached := router.regex_cache[route_path] {
|
||||
if cached.compiled {
|
||||
start_time5 := time.now()
|
||||
for _ in 0 .. 1000 {
|
||||
for param_name in cached.param_names {
|
||||
cached.regex.get_group_by_name(test_path, param_name)
|
||||
if cached.compiled && cached.param_names.len > 0 {
|
||||
// 先执行一次匹配确保正则状态正确
|
||||
if cached.regex.matches_string(test_path) {
|
||||
start_time5 := time.now()
|
||||
mut extract_count := 0
|
||||
for _ in 0 .. 1000 {
|
||||
// 每次提取前重新匹配
|
||||
if cached.regex.matches_string(test_path) {
|
||||
for param_name in cached.param_names {
|
||||
group := cached.regex.get_group_by_name(test_path, param_name)
|
||||
if group.len > 0 {
|
||||
extract_count++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
param_extract_time := time.since(start_time5)
|
||||
avg_param_time := f64(param_extract_time.microseconds()) / 1000.0
|
||||
println(' 参数提取平均时间: ${avg_param_time:.3f}μs')
|
||||
println(' 参数数量: ${cached.param_names.len}')
|
||||
}
|
||||
param_extract_time := time.since(start_time5)
|
||||
avg_param_time := f64(param_extract_time.microseconds()) / 1000.0
|
||||
println(' 参数提取平均时间: ${avg_param_time:.3f}μs')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,4 +220,4 @@ fn test_complexity_performance() {
|
||||
for i, path in dynamic_paths {
|
||||
println(' ${i+1}. ${path}')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ fn test_basic_performance() {
|
||||
mut fast_matches := 0
|
||||
for _ in 0 .. iterations {
|
||||
fast_router.clear_cache()
|
||||
if match_result := fast_router.match_route('GET', test_path) {
|
||||
if _ := fast_router.match_route('GET', test_path) {
|
||||
fast_matches++
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ fn test_basic_performance() {
|
||||
for _ in 0 .. iterations {
|
||||
hybrid_router.clear_cache()
|
||||
hybrid_router.clear_regex_cache()
|
||||
if match_result := hybrid_router.match_route('GET', test_path) {
|
||||
if _ := hybrid_router.match_route('GET', test_path) {
|
||||
hybrid_matches++
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,7 @@ fn test_basic_performance() {
|
||||
start_time3 := time.now()
|
||||
mut fast_cache_matches := 0
|
||||
for _ in 0 .. iterations {
|
||||
if match_result := fast_router.match_route('GET', test_path) {
|
||||
if _ := fast_router.match_route('GET', test_path) {
|
||||
fast_cache_matches++
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,7 @@ fn test_basic_performance() {
|
||||
start_time4 := time.now()
|
||||
mut hybrid_cache_matches := 0
|
||||
for _ in 0 .. iterations {
|
||||
if match_result := hybrid_router.match_route('GET', test_path) {
|
||||
if _ := hybrid_router.match_route('GET', test_path) {
|
||||
hybrid_cache_matches++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import hono
|
||||
import net.http
|
||||
import os
|
||||
import time
|
||||
import strings
|
||||
|
||||
@@ -32,14 +29,14 @@ fn test_json_building() {
|
||||
|
||||
// 方法1: 使用字符串插值(优化后)
|
||||
start_time := time.now()
|
||||
for i in 0 .. 10000 {
|
||||
for _ in 0 .. 10000 {
|
||||
_ := '{"message": "测试", "params": {"action": "${test_data['action']}", "id": "${test_data['id']}"}}'
|
||||
}
|
||||
interpolation_time := time.since(start_time)
|
||||
|
||||
// 方法2: 使用StringBuilder(复杂场景)
|
||||
start_time2 := time.now()
|
||||
for i in 0 .. 10000 {
|
||||
for _ in 0 .. 10000 {
|
||||
mut builder := strings.new_builder(256)
|
||||
builder.write_string('{"message": "测试", "params": {"action": "')
|
||||
builder.write_string(test_data['action'])
|
||||
|
||||
Reference in New Issue
Block a user