Skip to content

Commit

Permalink
Added benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
chouettevan committed Apr 27, 2024
1 parent c2bdb81 commit 59b2867
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ type testMap[K any,T any] []struct{
output T
}

var statusTests = testMap[string,int]{
{"404 Not found response","HTTP/1.1 404 Not Found",404},
{"200 Ok response","HTTP/2.0 200 Ok",200},
{"302 Found response test","HTTP/1.1 302 Found",302},
};

func TestGetStatus(t *testing.T) {
var statusTests = testMap[string,int]{
{"404 Not found response","HTTP/1.1 404 Not Found",404},
{"200 Ok response","HTTP/2.0 200 Ok",200},
{"302 Found response test","HTTP/1.1 302 Found",302},
};
for _,test := range statusTests {
t.Run(test.name,func(t *testing.T) {
if getStatus(test.input) != test.output {
Expand All @@ -27,11 +28,21 @@ func TestGetStatus(t *testing.T) {
}
}

func BenchmarkGetStatus(b *testing.B) {
for _,test := range statusTests {
b.Run(test.name,func(b *testing.B) {
for i := 0;i < b.N;i++ {
getStatus(test.input)
}
})
}
}

const charset string = "\n ";
var parseTests = testMap[io.Reader,string]{
{"200 Ok with keep-alive",strings.NewReader("HTTP/1.1 200 Ok\nConnection:keep-alive"),"200 37"},
}
func TestHttpParse(t *testing.T) {
var parseTests = testMap[io.Reader,string]{
{"200 Ok with keep-alive",strings.NewReader("HTTP/1.1 200 Ok\nConnection:keep-alive"),"200 37"},
}
for _,test := range parseTests {
t.Run(test.name,func(t *testing.T) {
out := strings.Trim(HttpParse(test.input),charset)
Expand All @@ -41,3 +52,14 @@ func TestHttpParse(t *testing.T) {
})
}
}
func BenchmarkHttpParse(b *testing.B) {
for _,test := range parseTests {
b.Run(test.name,func(b *testing.B) {
for i := 0;i < b.N;i++ {
HttpParse(test.input)
}
})
}

}

0 comments on commit 59b2867

Please sign in to comment.