Skip to content

Commit

Permalink
Merge pull request #8 from jianzhiyao/develop
Browse files Browse the repository at this point in the history
travis
  • Loading branch information
jianzhiyao authored Jan 31, 2021
2 parents 89862ab + f2d2e5c commit b1815c9
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 92 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ language: go

go:
- "1.14.x"
- "1.15.x"
env:
global:
- GO_REPO_FULLNAME="github.com/jianzhiyao/gclient"
- GO_REPO_FULLNAME="github.com/jianzhiyao/gclient" TEST_TARGET=http://127.0.0.1:8080/
matrix:
- TEST_TARGET=http://127.0.0.1:8080/ BENCHMARK_TARGET=https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
- TEST_TARGET=http://127.0.0.1:8080/ BENCHMARK_TARGET=https://cdn.jsdelivr.net/npm/axios@0.21.1/dist/axios.min.js
- BENCHMARK_TARGET=http://127.0.0.1:8080/benchmark BENCHMARK_LIMIT=50
- BENCHMARK_TARGET=http://127.0.0.1:8080/benchmark BENCHMARK_LIMIT=100
before_install:
# link the local repo with ${GOPATH}/src/<namespace>/<repo>
- GO_REPO_NAMESPACE=${GO_REPO_FULLNAME%/*}
Expand Down
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,11 @@ if err := resp.XmlUnmarshal(&a); err != nil {
## Benchmark
Gclient VS. net/http.Client
```
# go test -bench=. -benchmem -cpu 1
BenchmarkClient_GClientGet_1_Workers 705 1753318 ns/op 15621 B/op 96 allocs/op
BenchmarkClient_GClientGet_10_Workers 838 1222005 ns/op 15692 B/op 96 allocs/op
BenchmarkClient_GClientGet_100_Workers 915 1195727 ns/op 15702 B/op 96 allocs/op
BenchmarkClient_GClientGet_1000_Workers 1016 1195928 ns/op 17055 B/op 103 allocs/op
BenchmarkClient_HttpClientGet 1008 1053572 ns/op 15131 B/op 88 allocs/op
# go test -bench=. -benchmem -cpu 12
BenchmarkClient_GClientGet_1_Workers-12 844 1499498 ns/op 15706 B/op 96 allocs/op
BenchmarkClient_GClientGet_10_Workers-12 999 1071070 ns/op 15737 B/op 96 allocs/op
BenchmarkClient_GClientGet_100_Workers-12 967 1078593 ns/op 15677 B/op 96 allocs/op
BenchmarkClient_GClientGet_1000_Workers-12 1599 1038410 ns/op 16484 B/op 100 allocs/op
BenchmarkClient_HttpClientGet-12 1282 1023567 ns/op 15342 B/op 88 allocs/op
# requests
BenchmarkClient_GClientGet_1000_Workers-12 2020 694924 ns/op
BenchmarkClient_HttpClientGet-12 2284 751703 ns/op
# new request
Benchmark_Gclient_NewRequest-12 799914 1350 ns/op
Benchmark_Http_NewRequest-12 799903 1321 ns/op
```
24 changes: 14 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ type Client struct {
}

func New(options ...Option) *Client {
pool, _ := ants.NewPool(
1024,
ants.WithNonblocking(false),
)
c := &Client{
headers: http.Header{},
pool: pool,
}

c.Options(options...)
Expand Down Expand Up @@ -81,17 +76,26 @@ func (r *Client) Do(method, url string) (*response.Response, error) {
}

func (r *Client) DoRequest(req *request.Request) (resp *response.Response, err error) {
c := make(chan bool)
_ = r.pool.Submit(func() {
if r.pool == nil {
resp, err = r.do(
req.GetMethod(),
req.GetUrl(),
req.GetBody(),
req.GetHeaders(),
)
c <- true
})
<-c
} else {
c := make(chan bool)
_ = r.pool.Submit(func() {
resp, err = r.do(
req.GetMethod(),
req.GetUrl(),
req.GetBody(),
req.GetHeaders(),
)
c <- true
})
<-c
}
return
}

Expand Down
28 changes: 25 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gclient
import (
"net/http"
"os"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -69,9 +70,9 @@ func TestClient_DoRequest(t *testing.T) {

}

const BenchmarkLimit = 50
func benchmarkWithWorker(b *testing.B, size int) {
limit := make(chan bool, BenchmarkLimit)
benchmarkLimit, _ := strconv.Atoi(os.Getenv(`BENCHMARK_LIMIT`))
limit := make(chan bool, benchmarkLimit)

c := New(
OptWorkerPoolSize(size),
Expand Down Expand Up @@ -114,7 +115,8 @@ func BenchmarkClient_GClientGet_1000_Workers(b *testing.B) {
}

func BenchmarkClient_HttpClientGet(b *testing.B) {
limit := make(chan bool, BenchmarkLimit)
benchmarkLimit, _ := strconv.Atoi(os.Getenv(`BENCHMARK_LIMIT`))
limit := make(chan bool, benchmarkLimit)

c := &http.Client{}
url := os.Getenv(`BENCHMARK_TARGET`)
Expand All @@ -137,3 +139,23 @@ func BenchmarkClient_HttpClientGet(b *testing.B) {
}
wg.Wait()
}

func Benchmark_Gclient_NewRequest(b *testing.B) {
for i := 0; i < b.N; i++ {
if req, err := NewRequest(http.MethodGet, os.Getenv(`TEST_TARGET`)); err != nil {
b.Error(err)
} else if req == nil {
b.Error()
}
}
}

func Benchmark_Http_NewRequest(b *testing.B) {
for i := 0; i < b.N; i++ {
if req, err := http.NewRequest(http.MethodGet, os.Getenv(`TEST_TARGET`), nil); err != nil {
b.Error(err)
} else if req == nil {
b.Error()
}
}
}
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gclient
import (
"context"
"github.com/jianzhiyao/gclient/consts"
"github.com/panjf2000/ants/v2"
"net/http"
"time"
)
Expand Down Expand Up @@ -85,6 +86,12 @@ func OptRetry(times int) Option {
//default size is 1000
func OptWorkerPoolSize(size int) Option {
return func(req *Client) {
if req.pool == nil {
req.pool, _ = ants.NewPool(
1,
ants.WithNonblocking(false),
)
}
req.pool.Tune(size)
}
}
Expand Down
53 changes: 16 additions & 37 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package request
import (
"bytes"
"encoding"
"errors"
"github.com/jianzhiyao/gclient/consts"
"github.com/jianzhiyao/gclient/consts/content_type"
"github.com/jianzhiyao/gclient/consts/transfer_encoding"
Expand All @@ -17,70 +16,50 @@ import (
)

type Request struct {
method string
url string
headers http.Header
body io.ReadCloser
request *http.Request
}

//New base method of new request
func New(method, uri string) (*Request, error) {
switch method {
case http.MethodGet:
case http.MethodPost:
case http.MethodConnect:
case http.MethodDelete:
case http.MethodHead:
case http.MethodOptions:
case http.MethodPatch:
case http.MethodPut:
case http.MethodTrace:
default:
return nil, errors.New("not a valid http method")
}

//check validation of uri
if _, err := url.Parse(uri); err != nil {
if req, err := http.NewRequest(method, uri, nil); err != nil {
return nil, err
} else {
return &Request{
request: req,
}, nil
}

return &Request{
method: method,
url: uri,
headers: nil,
}, nil
}

func (r *Request) SetHeader(key string, value ...string) {
if r.headers == nil {
r.headers = http.Header{}
if r.request.Header == nil {
r.request.Header = http.Header{}
}
r.headers[key] = value
r.request.Header[key] = value
}

func (r *Request) GetUrl() string {
return r.url
return r.request.URL.String()
}

func (r *Request) GetMethod() string {
return r.method
return r.request.Method
}

func (r *Request) GetHeaders() http.Header {
return r.headers
return r.request.Header
}

func (r *Request) GetHeader(key string) (value []string, ok bool) {
if r.headers == nil {
if r.request.Header == nil {
return
}

value, ok = r.headers[key]
value, ok = r.request.Header[key]
return
}

func (r *Request) GetBody() io.Reader {
return r.body
return r.request.Body
}

func (r *Request) Json(body interface{}) (err error) {
Expand Down Expand Up @@ -161,7 +140,7 @@ func (r *Request) Body(body interface{}) (err error) {
if err != nil {
return
}
r.body = ioutil.NopCloser(reader)
r.request.Body = ioutil.NopCloser(reader)

return
}
20 changes: 6 additions & 14 deletions request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,17 @@ func TestNew(t *testing.T) {
t.Error(err)
}

if req.method != method {
t.Error(err)
if req.request.Method != method {
t.Error()
}

if req.url != url {
if req.GetUrl() != url {
t.Error(err)
}
}

}

func TestNew2(t *testing.T) {
method, url := `1`, os.Getenv(`TEST_TARGET`)
_, err := New(method, url)
if err == nil {
t.Error(err)
}
}

func TestRequest_Method(t *testing.T) {
method, url := http.MethodPatch, os.Getenv(`TEST_TARGET`)
req, _ := New(method, url)
Expand Down Expand Up @@ -92,13 +84,13 @@ func TestRequest_SetHeader(t *testing.T) {
req.SetHeader("a", "1")
req.SetHeader("b", "1")

if _, ok := req.headers["a"]; !ok {
if _, ok := req.request.Header["a"]; !ok {
t.Error()
}
if _, ok := req.headers["b"]; !ok {
if _, ok := req.request.Header["b"]; !ok {
t.Error()
}
if _, ok := req.headers["c"]; ok {
if _, ok := req.request.Header["c"]; ok {
t.Error()
}
}
Expand Down
23 changes: 12 additions & 11 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,66 @@ package gclient

import (
"net/http"
"os"
"testing"
)

func TestNewRequest(t *testing.T) {
if c, err := NewRequest(http.MethodPost, ``); c == nil || err != nil {
if c, err := NewRequest(http.MethodPost, os.Getenv(`TEST_TARGET`)); c == nil || err != nil {
t.Error()
}
}

func TestNewRequestDelete(t *testing.T) {
if c, _ := NewRequestDelete(``); c.GetMethod() != http.MethodDelete {
if c, _ := NewRequestDelete(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodDelete {
t.Error()
}
}

func TestNewRequestGet(t *testing.T) {
if c, _ := NewRequestGet(``); c.GetMethod() != http.MethodGet {
if c, _ := NewRequestGet(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodGet {
t.Error()
}
}

func TestNewRequestHead(t *testing.T) {
if c, _ := NewRequestHead(``); c.GetMethod() != http.MethodHead {
if c, _ := NewRequestHead(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodHead {
t.Error()
}
}

func TestNewRequestPatch(t *testing.T) {
if c, _ := NewRequestPatch(``); c.GetMethod() != http.MethodPatch {
if c, _ := NewRequestPatch(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodPatch {
t.Error()
}
}

func TestNewRequestTrace(t *testing.T) {
if c, _ := NewRequestTrace(``); c.GetMethod() != http.MethodTrace {
if c, _ := NewRequestTrace(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodTrace {
t.Error()
}
}

func TestNewRequestPost(t *testing.T) {
if c, _ := NewRequestPost(``); c.GetMethod() != http.MethodPost {
t.Error()
if c, _ := NewRequestPost(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodPost {
t.Error(c.GetMethod())
}
}

func TestNewRequestPut(t *testing.T) {
if c, _ := NewRequestPut(``); c.GetMethod() != http.MethodPut {
if c, _ := NewRequestPut(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodPut {
t.Error()
}
}

func TestNewRequestConnect(t *testing.T) {
if c, _ := NewRequestConnect(``); c.GetMethod() != http.MethodConnect {
if c, _ := NewRequestConnect(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodConnect {
t.Error()
}
}

func TestNewRequestOptions(t *testing.T) {
if c, _ := NewRequestOptions(``); c.GetMethod() != http.MethodOptions {
if c, _ := NewRequestOptions(os.Getenv(`TEST_TARGET`)); c.GetMethod() != http.MethodOptions {
t.Error()
}
}

0 comments on commit b1815c9

Please sign in to comment.