Skip to content

Commit

Permalink
feat(enhancement): remove unnecessary map for raw path params instead…
Browse files Browse the repository at this point in the history
… use path params (#924)

* feat(enhancement): remove unnecessary map for raw path params instead use path params

- correct digest roundtrip return value on error scenario
- streamline content-length value on buffer and partial io.Reader flow
- correct typo of ContentDecompressor -> ContentDecompressor
- refactor createHTTPRequest -> createRawRequest
- update c.log -> c.Logger()
- update redundant err check and if conditions
- address curl cmd non-execute request error flow
- prevent reset buffer during buffer copy on request clone
- improve test cases and corner coverage
- move benchmarks into a dedicated file
- update godoc

* ci: add v3 branch in github actions config

- update test cmd with gotestsum executable to display test name
- apply mod tidy
  • Loading branch information
jeevatkm authored Dec 2, 2024
1 parent 6454923 commit 7b5db4e
Show file tree
Hide file tree
Showing 19 changed files with 505 additions and 442 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: CI
on:
push:
branches:
- v3
- v2
paths-ignore:
- '**.md'
Expand All @@ -11,6 +12,7 @@ on:
pull_request:
branches:
- main
- v3
- v2
paths-ignore:
- '**.md'
Expand Down Expand Up @@ -47,7 +49,7 @@ jobs:
run: diff -u <(echo -n) <(go fmt $(go list ./...))

- name: Test
run: go test ./... -race -count=1 -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... -shuffle=on
run: go run gotest.tools/gotestsum@latest -f testname -- ./... -race -count=1 -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... -shuffle=on

- name: Upload coverage to Codecov
if: ${{ matrix.os == 'ubuntu-latest' && matrix.go == 'stable' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: diff -u <(echo -n) <(go fmt $(go list ./...))

- name: Test
run: go test ./... -race -count=1 -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... -shuffle=on
run: go run gotest.tools/gotestsum@latest -f testname -- ./... -race -count=1 -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... -shuffle=on

- name: Upload coverage to Codecov
if: ${{ matrix.os == 'ubuntu-latest' && matrix.go == 'stable' }}
Expand Down
205 changes: 205 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package resty

import (
"bytes"
"strings"
"testing"
)

func Benchmark_parseRequestURL_PathParams(b *testing.B) {
c := New().SetPathParams(map[string]string{
"foo": "1",
"bar": "2",
}).SetRawPathParams(map[string]string{
"foo": "3",
"xyz": "4",
})
r := c.R().SetPathParams(map[string]string{
"foo": "5",
"qwe": "6",
}).SetRawPathParams(map[string]string{
"foo": "7",
"asd": "8",
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.URL = "https://example.com/{foo}/{bar}/{xyz}/{qwe}/{asd}"
if err := parseRequestURL(c, r); err != nil {
b.Errorf("parseRequestURL() error = %v", err)
}
}
}

func Benchmark_parseRequestURL_QueryParams(b *testing.B) {
c := New().SetQueryParams(map[string]string{
"foo": "1",
"bar": "2",
})
r := c.R().SetQueryParams(map[string]string{
"foo": "5",
"qwe": "6",
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.URL = "https://example.com/"
if err := parseRequestURL(c, r); err != nil {
b.Errorf("parseRequestURL() error = %v", err)
}
}
}

func Benchmark_parseRequestHeader(b *testing.B) {
c := New()
r := c.R()
c.SetHeaders(map[string]string{
"foo": "1", // ignored, because of the same header in the request
"bar": "2",
})
r.SetHeaders(map[string]string{
"foo": "3",
"xyz": "4",
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestHeader(c, r); err != nil {
b.Errorf("parseRequestHeader() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_string(b *testing.B) {
c := New()
r := c.R()
r.SetBody("foo").SetContentLength(true)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_byte(b *testing.B) {
c := New()
r := c.R()
r.SetBody([]byte("foo")).SetContentLength(true)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_reader(b *testing.B) {
c := New()
r := c.R()
r.SetBody(bytes.NewBufferString("foo"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_struct(b *testing.B) {
type FooBar struct {
Foo string `json:"foo"`
Bar string `json:"bar"`
}
c := New()
r := c.R()
r.SetBody(FooBar{Foo: "1", Bar: "2"}).SetContentLength(true).SetHeader(hdrContentTypeKey, jsonContentType)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_struct_xml(b *testing.B) {
type FooBar struct {
Foo string `xml:"foo"`
Bar string `xml:"bar"`
}
c := New()
r := c.R()
r.SetBody(FooBar{Foo: "1", Bar: "2"}).SetContentLength(true).SetHeader(hdrContentTypeKey, "text/xml")
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_map(b *testing.B) {
c := New()
r := c.R()
r.SetBody(map[string]string{
"foo": "1",
"bar": "2",
}).SetContentLength(true).SetHeader(hdrContentTypeKey, jsonContentType)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_slice(b *testing.B) {
c := New()
r := c.R()
r.SetBody([]string{"1", "2"}).SetContentLength(true).SetHeader(hdrContentTypeKey, jsonContentType)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_FormData(b *testing.B) {
c := New()
r := c.R()
c.SetFormData(map[string]string{"foo": "1", "bar": "2"})
r.SetFormData(map[string]string{"foo": "3", "baz": "4"}).SetContentLength(true)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}

func Benchmark_parseRequestBody_MultiPart(b *testing.B) {
c := New()
r := c.R()
c.SetFormData(map[string]string{"foo": "1", "bar": "2"})
r.SetFormData(map[string]string{"foo": "3", "baz": "4"}).
SetMultipartFormData(map[string]string{"foo": "5", "xyz": "6"}).
SetFileReader("qwe", "qwe.txt", strings.NewReader("7")).
SetMultipartFields(
&MultipartField{
Name: "sdj",
ContentType: "text/plain",
Reader: strings.NewReader("8"),
},
).
SetContentLength(true).
SetMethod(MethodPost)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := parseRequestBody(c, r); err != nil {
b.Errorf("parseRequestBody() error = %v", err)
}
}
}
Loading

0 comments on commit 7b5db4e

Please sign in to comment.