|
| 1 | +// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. |
| 2 | +// resty source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +package resty |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func Benchmark_parseRequestURL_PathParams(b *testing.B) { |
| 15 | + c := New().SetPathParams(map[string]string{ |
| 16 | + "foo": "1", |
| 17 | + "bar": "2", |
| 18 | + }).SetRawPathParams(map[string]string{ |
| 19 | + "foo": "3", |
| 20 | + "xyz": "4", |
| 21 | + }) |
| 22 | + r := c.R().SetPathParams(map[string]string{ |
| 23 | + "foo": "5", |
| 24 | + "qwe": "6", |
| 25 | + }).SetRawPathParams(map[string]string{ |
| 26 | + "foo": "7", |
| 27 | + "asd": "8", |
| 28 | + }) |
| 29 | + b.ResetTimer() |
| 30 | + for i := 0; i < b.N; i++ { |
| 31 | + r.URL = "https://example.com/{foo}/{bar}/{xyz}/{qwe}/{asd}" |
| 32 | + if err := parseRequestURL(c, r); err != nil { |
| 33 | + b.Errorf("parseRequestURL() error = %v", err) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func Benchmark_parseRequestURL_QueryParams(b *testing.B) { |
| 39 | + c := New().SetQueryParams(map[string]string{ |
| 40 | + "foo": "1", |
| 41 | + "bar": "2", |
| 42 | + }) |
| 43 | + r := c.R().SetQueryParams(map[string]string{ |
| 44 | + "foo": "5", |
| 45 | + "qwe": "6", |
| 46 | + }) |
| 47 | + b.ResetTimer() |
| 48 | + for i := 0; i < b.N; i++ { |
| 49 | + r.URL = "https://example.com/" |
| 50 | + if err := parseRequestURL(c, r); err != nil { |
| 51 | + b.Errorf("parseRequestURL() error = %v", err) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func Benchmark_parseRequestHeader(b *testing.B) { |
| 57 | + c := New() |
| 58 | + r := c.R() |
| 59 | + c.SetHeaders(map[string]string{ |
| 60 | + "foo": "1", // ignored, because of the same header in the request |
| 61 | + "bar": "2", |
| 62 | + }) |
| 63 | + r.SetHeaders(map[string]string{ |
| 64 | + "foo": "3", |
| 65 | + "xyz": "4", |
| 66 | + }) |
| 67 | + b.ResetTimer() |
| 68 | + for i := 0; i < b.N; i++ { |
| 69 | + if err := parseRequestHeader(c, r); err != nil { |
| 70 | + b.Errorf("parseRequestHeader() error = %v", err) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func Benchmark_parseRequestBody_string(b *testing.B) { |
| 76 | + c := New() |
| 77 | + r := c.R() |
| 78 | + r.SetBody("foo").SetContentLength(true) |
| 79 | + b.ResetTimer() |
| 80 | + for i := 0; i < b.N; i++ { |
| 81 | + if err := parseRequestBody(c, r); err != nil { |
| 82 | + b.Errorf("parseRequestBody() error = %v", err) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func Benchmark_parseRequestBody_byte(b *testing.B) { |
| 88 | + c := New() |
| 89 | + r := c.R() |
| 90 | + r.SetBody([]byte("foo")).SetContentLength(true) |
| 91 | + b.ResetTimer() |
| 92 | + for i := 0; i < b.N; i++ { |
| 93 | + if err := parseRequestBody(c, r); err != nil { |
| 94 | + b.Errorf("parseRequestBody() error = %v", err) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func Benchmark_parseRequestBody_reader(b *testing.B) { |
| 100 | + c := New() |
| 101 | + r := c.R() |
| 102 | + r.SetBody(bytes.NewBufferString("foo")) |
| 103 | + b.ResetTimer() |
| 104 | + for i := 0; i < b.N; i++ { |
| 105 | + if err := parseRequestBody(c, r); err != nil { |
| 106 | + b.Errorf("parseRequestBody() error = %v", err) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func Benchmark_parseRequestBody_struct(b *testing.B) { |
| 112 | + type FooBar struct { |
| 113 | + Foo string `json:"foo"` |
| 114 | + Bar string `json:"bar"` |
| 115 | + } |
| 116 | + c := New() |
| 117 | + r := c.R() |
| 118 | + r.SetBody(FooBar{Foo: "1", Bar: "2"}).SetContentLength(true).SetHeader(hdrContentTypeKey, jsonContentType) |
| 119 | + b.ResetTimer() |
| 120 | + for i := 0; i < b.N; i++ { |
| 121 | + if err := parseRequestBody(c, r); err != nil { |
| 122 | + b.Errorf("parseRequestBody() error = %v", err) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func Benchmark_parseRequestBody_struct_xml(b *testing.B) { |
| 128 | + type FooBar struct { |
| 129 | + Foo string `xml:"foo"` |
| 130 | + Bar string `xml:"bar"` |
| 131 | + } |
| 132 | + c := New() |
| 133 | + r := c.R() |
| 134 | + r.SetBody(FooBar{Foo: "1", Bar: "2"}).SetContentLength(true).SetHeader(hdrContentTypeKey, "text/xml") |
| 135 | + b.ResetTimer() |
| 136 | + for i := 0; i < b.N; i++ { |
| 137 | + if err := parseRequestBody(c, r); err != nil { |
| 138 | + b.Errorf("parseRequestBody() error = %v", err) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func Benchmark_parseRequestBody_map(b *testing.B) { |
| 144 | + c := New() |
| 145 | + r := c.R() |
| 146 | + r.SetBody(map[string]string{ |
| 147 | + "foo": "1", |
| 148 | + "bar": "2", |
| 149 | + }).SetContentLength(true).SetHeader(hdrContentTypeKey, jsonContentType) |
| 150 | + b.ResetTimer() |
| 151 | + for i := 0; i < b.N; i++ { |
| 152 | + if err := parseRequestBody(c, r); err != nil { |
| 153 | + b.Errorf("parseRequestBody() error = %v", err) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func Benchmark_parseRequestBody_slice(b *testing.B) { |
| 159 | + c := New() |
| 160 | + r := c.R() |
| 161 | + r.SetBody([]string{"1", "2"}).SetContentLength(true).SetHeader(hdrContentTypeKey, jsonContentType) |
| 162 | + b.ResetTimer() |
| 163 | + for i := 0; i < b.N; i++ { |
| 164 | + if err := parseRequestBody(c, r); err != nil { |
| 165 | + b.Errorf("parseRequestBody() error = %v", err) |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func Benchmark_parseRequestBody_FormData(b *testing.B) { |
| 171 | + c := New() |
| 172 | + r := c.R() |
| 173 | + c.SetFormData(map[string]string{"foo": "1", "bar": "2"}) |
| 174 | + r.SetFormData(map[string]string{"foo": "3", "baz": "4"}).SetContentLength(true) |
| 175 | + b.ResetTimer() |
| 176 | + for i := 0; i < b.N; i++ { |
| 177 | + if err := parseRequestBody(c, r); err != nil { |
| 178 | + b.Errorf("parseRequestBody() error = %v", err) |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func Benchmark_parseRequestBody_MultiPart(b *testing.B) { |
| 184 | + c := New() |
| 185 | + r := c.R() |
| 186 | + c.SetFormData(map[string]string{"foo": "1", "bar": "2"}) |
| 187 | + r.SetFormData(map[string]string{"foo": "3", "baz": "4"}). |
| 188 | + SetMultipartFormData(map[string]string{"foo": "5", "xyz": "6"}). |
| 189 | + SetFileReader("qwe", "qwe.txt", strings.NewReader("7")). |
| 190 | + SetMultipartFields( |
| 191 | + &MultipartField{ |
| 192 | + Name: "sdj", |
| 193 | + ContentType: "text/plain", |
| 194 | + Reader: strings.NewReader("8"), |
| 195 | + }, |
| 196 | + ). |
| 197 | + SetContentLength(true). |
| 198 | + SetMethod(MethodPost) |
| 199 | + b.ResetTimer() |
| 200 | + for i := 0; i < b.N; i++ { |
| 201 | + if err := parseRequestBody(c, r); err != nil { |
| 202 | + b.Errorf("parseRequestBody() error = %v", err) |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments