Skip to content

Commit

Permalink
results
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Kohler committed Feb 9, 2024
1 parent 69f2b6a commit c3a4e7c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
17 changes: 15 additions & 2 deletions filters/builtin/stripquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func validHeaderFieldByte(b byte) bool {
}

// make sure we don't generate invalid headers
func sanitize(input string) string {
// temporary public function to benchmark it
func Sanitize(input string) string {
toAscii := strconv.QuoteToASCII(input)
var b bytes.Buffer
for _, i := range toAscii {
Expand All @@ -62,6 +63,18 @@ func sanitize(input string) string {
return b.String()
}

// temporary public function to benchmark it
func NewSanitize(input string) string {
toAscii := strconv.QuoteToASCII(input)
var s strings.Builder
for _, i := range toAscii {
if validHeaderFieldByte(byte(i)) {
s.WriteRune(i)
}
}
return s.String()
}

// Strips the query parameters and optionally preserves them in the X-Query-Param-xyz headers.
func (f *stripQuery) Request(ctx filters.FilterContext) {
r := ctx.Request()
Expand All @@ -85,7 +98,7 @@ func (f *stripQuery) Request(ctx filters.FilterContext) {
if r.Header == nil {
r.Header = http.Header{}
}
r.Header.Add(fmt.Sprintf("X-Query-Param-%s", sanitize(k)), v)
r.Header.Add(fmt.Sprintf("X-Query-Param-%s", Sanitize(k)), v)
}
}

Expand Down
39 changes: 38 additions & 1 deletion filters/builtin/stripquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package builtin

import (
"github.com/zalando/skipper/filters/filtertest"
"fmt"
"net/http"
"net/url"
"strings"
"testing"

"github.com/zalando/skipper/filters/filtertest"
)

func TestCreateStripQueryFilter(t *testing.T) {
Expand Down Expand Up @@ -91,3 +93,38 @@ func TestPreserveQuery(t *testing.T) {
}
}
}

func BenchmarkStripQuery(b *testing.B) {
var table = []struct {
url string
}{
{url: "http://example.org/foo?bar=baz"},
{url: "http://example.org/foo?query=cXVlcnkgewogIG5hbWUKfQo%253D&variables=&apiEndpoint=https%253A%252F%252Fwww.zalando.de%252Fapi%252Fgraphql%252Fpreview&frontendType=web&zalandoFeature=manual"},
{url: "http://example.org/foo?馬鹿=123"},
{url: "http://example.org/foo?a%20b=123"},
}

for i, v := range table {
url, _ := url.ParseRequestURI(v.url)
q := url.Query()
b.Run(fmt.Sprintf("[old sanitize] url %d", i+1), func(b *testing.B) {
for i := 0; i < b.N; i++ {
for k := range q {
Sanitize(k)
}
}
})
}

for i, v := range table {
url, _ := url.ParseRequestURI(v.url)
q := url.Query()
b.Run(fmt.Sprintf("[new sanitize] url %d", i + 1), func(b *testing.B) {
for i := 0; i < b.N; i++ {
for k := range q {
NewSanitize(k)
}
}
})
}
}
14 changes: 14 additions & 0 deletions results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
`go test -benchmem -run=^$ -bench ^BenchmarkStripQuery$ -benchtime=10s github.com/zalando/skipper/filters/builtin`

```
BenchmarkStripQuery/[old_sanitize]_url_1-8 85732707 118.6 ns/op 80 B/op 4 allocs/op
BenchmarkStripQuery/[old_sanitize]_url_2-8 14885370 808.2 ns/op 472 B/op 15 allocs/op
BenchmarkStripQuery/[old_sanitize]_url_3-8 73297474 162.9 ns/op 120 B/op 4 allocs/op
BenchmarkStripQuery/[old_sanitize]_url_4-8 100000000 114.1 ns/op 80 B/op 4 allocs/op
BenchmarkStripQuery/[new_sanitize]_url_1-8 100000000 103.5 ns/op 24 B/op 3 allocs/op
BenchmarkStripQuery/[new_sanitize]_url_2-8 15854868 778.5 ns/op 192 B/op 14 allocs/op
BenchmarkStripQuery/[new_sanitize]_url_3-8 71689101 166.3 ns/op 64 B/op 4 allocs/op
BenchmarkStripQuery/[new_sanitize]_url_4-8 100000000 102.0 ns/op 24 B/op 3 allocs/op
```

0 comments on commit c3a4e7c

Please sign in to comment.