diff --git a/tests/basic_test.go b/tests/basic_test.go index ca899b5..bfb7d9e 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -132,9 +132,8 @@ func BenchmarkRouterLookup(b *testing.B) { path := "/" params := &routers.Params{} - b.ResetTimer() - for i := 0; i < b.N; i++ { - _, _, _, _, _ = luaRouter.GetRouteInfo(method, path, params) + for b.Loop() { + _, _, _, _ = luaRouter.GetRouteInfo(method, path, params) } } @@ -146,13 +145,12 @@ func BenchmarkSimpleLuaExecution(b *testing.B) { method := "GET" path := "/" params := &routers.Params{} - _, bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) ctx := runner.NewContext() defer ctx.Release() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, _ = luaRunner.Run(bytecode, ctx, scriptPath) } } @@ -165,13 +163,12 @@ func BenchmarkComplexLuaExecution(b *testing.B) { method := "GET" path := "/complex" params := &routers.Params{} - _, bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) ctx := runner.NewContext() defer ctx.Release() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, _ = luaRunner.Run(bytecode, ctx, scriptPath) } } @@ -185,10 +182,9 @@ func BenchmarkGetEndpoint(b *testing.B) { path := "/" params := &routers.Params{} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { // Route lookup - _, bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) // Context setup ctx := runner.NewContext() @@ -210,10 +206,9 @@ func BenchmarkPostEndpoint(b *testing.B) { path := "/" params := &routers.Params{} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { // Route lookup - _, bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) // Context setup with form data ctx := runner.NewContext() @@ -237,7 +232,7 @@ func BenchmarkConcurrentExecution(b *testing.B) { method := "GET" path := "/" params := &routers.Params{} - _, bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { @@ -257,7 +252,7 @@ func BenchmarkConcurrentComplexExecution(b *testing.B) { method := "GET" path := "/complex" params := &routers.Params{} - _, bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { @@ -277,23 +272,12 @@ func BenchmarkMiddlewareExecution(b *testing.B) { method := "GET" path := "/api" params := &routers.Params{} - middlewareBytecode, bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { ctx := runner.NewContext() - // Execute middleware chain - for _, mwBytecode := range middlewareBytecode { - if len(mwBytecode) > 0 { - response, _ := luaRunner.Run(mwBytecode, ctx, "middleware") - if response != nil { - runner.ReleaseResponse(response) - } - } - } - - // Execute handler + // Execute combined middleware + handler response, _ := luaRunner.Run(bytecode, ctx, scriptPath) if response != nil { runner.ReleaseResponse(response) @@ -314,8 +298,7 @@ func BenchmarkRouteCompilation(b *testing.B) { routesDir := filepath.Join(tempDir, "routes") os.MkdirAll(routesDir, 0755) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { b.StopTimer() os.RemoveAll(routesDir) os.MkdirAll(routesDir, 0755) @@ -329,7 +312,7 @@ func BenchmarkRouteCompilation(b *testing.B) { // BenchmarkContextCreation measures the cost of creating execution contexts func BenchmarkContextCreation(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { ctx := runner.NewContext() ctx.Release() } @@ -360,10 +343,9 @@ func BenchmarkRunnerExecute(b *testing.B) { method := "GET" path := "/" params := &routers.Params{} - _, bytecode, scriptPath, _ := luaRouter.GetRouteInfo(method, path, params) + bytecode, scriptPath, _, _ := luaRouter.GetRouteInfo(method, path, params) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { ctx := runner.NewContext() _, _ = luaRunner.Execute(context.Background(), bytecode, ctx, scriptPath) ctx.Release()