Skip to content

Commit

Permalink
Global debug (#340)
Browse files Browse the repository at this point in the history
* 把debug相关代码从dataflow模块中提出来

* 重构debug: 冗余命名,去掉多层结构引用

* Debug: 全局debug和保存文件
  • Loading branch information
guonaihong authored Jul 23, 2022
1 parent 1d93dc6 commit 372dd18
Show file tree
Hide file tree
Showing 24 changed files with 602 additions and 342 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
protobuf:
protoc --go_out=. --go_opt=paths=source_relative testdata/test.proto
test:
go test ./...
73 changes: 64 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ gout 是go写的http 客户端,为提高工作效率而开发
- [unix socket](#unix-socket)
- [http2 doc](#http2-doc)
- [debug mode](#debug-mode)
- [Turn on debug mode](#Turn-on-debug-mode)
- [Turn off color highlighting in debug mode](#Turn-off-color-highlighting-in-debug-mode)
- [Custom debug mode](#Custom-debug-mode)
- [Turn on debug mode](#turn-on-debug-mode)
- [Turn off color highlighting in debug mode](#turn-off-color-highlighting-in-debug-mode)
- [Custom debug mode](#custom-debug-mode)
- [trace info](#trace-info)
- [save to writer](#save-to-writer)
- [save to file](#save-to-file)
- [benchmark](#benchmark)
- [benchmarking a certain number of times](#benchmarking-a-certain-number-of-times)
- [benchmarking for a certain time](#benchmark-duration)
Expand Down Expand Up @@ -1419,12 +1421,16 @@ func main() {
}
```
### Turn off color highlighting in debug mode
使用gout.NoColor()传入Debug函数关闭颜色高亮
使用debug.NoColor()传入Debug函数关闭颜色高亮
```go
import (
"github.com/guonaihong/gout"
"github.com/guonaihong/gout/debug"
)
func main() {
err := gout.POST(":8080/colorjson").
Debug(gout.NoColor()).
Debug(debug.NoColor()).
SetJSON(gout.H{"str": "foo",
"num": 100,
"bool": false,
Expand All @@ -1447,11 +1453,12 @@ package main
import (
"fmt"
"github.com/guonaihong/gout"
"github.com/guonaihong/gout/debug"
"os"
)
func IOSDebug() gout.DebugOpt {
return gout.DebugFunc(func(o *gout.DebugOption) {
func IOSDebug() debug.Apply {
return gout.DebugFunc(func(o *debug.Options) {
if len(os.Getenv("IOS_DEBUG")) > 0 {
o.Debug = true
}
Expand All @@ -1477,18 +1484,19 @@ func main() {
// env IOS_DEBUG=true go run customize.go
```
### trace info
gout.Trace()可输出http各个阶段的耗时,比如dns lookup时间,tcp连接时间等等。可以很方便的做些性能调优。
debug.Trace()可输出http各个阶段的耗时,比如dns lookup时间,tcp连接时间等等。可以很方便的做些性能调优。
```go
package main
import (
"fmt"
"github.com/guonaihong/gout"
"github.com/guonaihong/gout/debug"
)
func openDebugTrace() {
err := gout.POST(":8080/colorjson").
Debug(gout.Trace()).
Debug(debug.Trace()).
SetJSON(gout.H{"str": "foo",
"num": 100,
"bool": false,
Expand All @@ -1515,6 +1523,53 @@ func openDebugTrace() {
TotalDuration : 2.13921ms
=================== Trace Info(E): ===================
```
### save to writer
`debug.ToWriter`可以传递任何io.Writer对象,比如`bytes.Buffer`, 文件等。。。
```go
import (
"github.com/guonaihong/gout"
"github.com/guonaihong/gout/debug"
)
func main() {
var buf bytes.Buffer
err := gout.POST(":8080/colorjson").
Debug(debug.ToWriter(&buf, false)).
SetJSON(gout.H{"str": "foo",
"num": 100,
"bool": false,
"null": nil,
"array": gout.A{"foo", "bar", "baz"},
"obj": gout.H{"a": 1, "b": 2},
}).Do()
if err != nil {
fmt.Printf("err = %v\n", err)
}
}
```
### save to file
```go
import (
"github.com/guonaihong/gout"
"github.com/guonaihong/gout/debug"
)
func main() {
err := gout.POST(":8080/colorjson").
Debug(debug.ToFile("./req.txt", false)).
SetJSON(gout.H{"str": "foo",
"num": 100,
"bool": false,
"null": nil,
"array": gout.A{"foo", "bar", "baz"},
"obj": gout.H{"a": 1, "b": 2},
}).Do()
if err != nil {
fmt.Printf("err = %v\n", err)
}
}
```
## benchmark
### benchmarking a certain number of times
下面的例子,起了20并发。对:8080端口的服务,发送3000次请求进行压测,内容为json结构
Expand Down
23 changes: 12 additions & 11 deletions dataflow/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"time"

"github.com/guonaihong/gout/debug"
"github.com/guonaihong/gout/decode"
"github.com/guonaihong/gout/encode"
api "github.com/guonaihong/gout/interface"
Expand Down Expand Up @@ -179,29 +180,29 @@ func (df *DataFlow) SetHeader(obj ...interface{}) *DataFlow {

// SetJSON send json to the http body, Support raw json(string, []byte)/struct/map types
func (df *DataFlow) SetJSON(obj interface{}) *DataFlow {
df.out.opt.ReqBodyType = "json"
df.ReqBodyType = "json"
df.Req.bodyEncoder = encode.NewJSONEncode(obj)
return df
}

// SetXML send xml to the http body
func (df *DataFlow) SetXML(obj interface{}) *DataFlow {
df.out.opt.ReqBodyType = "xml"
df.ReqBodyType = "xml"
df.Req.bodyEncoder = encode.NewXMLEncode(obj)
return df
}

// SetYAML send yaml to the http body, Support struct,map types
func (df *DataFlow) SetYAML(obj interface{}) *DataFlow {
df.out.opt.ReqBodyType = "yaml"
df.ReqBodyType = "yaml"
df.Req.bodyEncoder = encode.NewYAMLEncode(obj)
return df
}

// SetProtoBuf send yaml to the http body, Support struct types
// obj必须是结构体指针或者[]byte类型
func (df *DataFlow) SetProtoBuf(obj interface{}) *DataFlow {
df.out.opt.ReqBodyType = "protobuf"
df.ReqBodyType = "protobuf"
df.Req.bodyEncoder = encode.NewProtoBufEncode(obj)
return df
}
Expand Down Expand Up @@ -309,7 +310,7 @@ func (df *DataFlow) BindJSON(obj interface{}) *DataFlow {
if obj == nil {
return df
}
df.out.opt.RspBodyType = "json"
df.out.RspBodyType = "json"
df.Req.bodyDecoder = append(df.Req.bodyDecoder, decode.NewJSONDecode(obj))
return df

Expand All @@ -321,7 +322,7 @@ func (df *DataFlow) BindYAML(obj interface{}) *DataFlow {
if obj == nil {
return df
}
df.out.opt.RspBodyType = "yaml"
df.RspBodyType = "yaml"
df.Req.bodyDecoder = append(df.Req.bodyDecoder, decode.NewYAMLDecode(obj))
return df
}
Expand All @@ -332,7 +333,7 @@ func (df *DataFlow) BindXML(obj interface{}) *DataFlow {
if obj == nil {
return df
}
df.out.opt.RspBodyType = "xml"
df.RspBodyType = "xml"
df.Req.bodyDecoder = append(df.Req.bodyDecoder, decode.NewXMLDecode(obj))
return df
}
Expand Down Expand Up @@ -398,10 +399,10 @@ func (df *DataFlow) Debug(d ...interface{}) *DataFlow {
switch opt := v.(type) {
case bool:
if opt {
defaultDebug(&df.out.opt)
debug.DefaultDebug(&df.Setting.Options)
}
case DebugOpt:
opt.Apply(&df.out.opt)
case debug.Apply:
opt.Apply(&df.Setting.Options)
}
}

Expand All @@ -420,7 +421,7 @@ func (df *DataFlow) NoAutoContentType() *DataFlow {
}

func (df *DataFlow) IsDebug() bool {
return df.out.opt.Debug
return df.Setting.Debug
}

// Do send function
Expand Down
127 changes: 0 additions & 127 deletions dataflow/dataflow_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dataflow

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -18,7 +17,6 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/guonaihong/gout/color"
"github.com/guonaihong/gout/core"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -537,131 +535,6 @@ func setupDebug(t *testing.T) *gin.Engine {
return r
}

/*
// TODO
type myDup struct {
stdout *os.File
}
func (m *myDup) dup(t *testing.T) {
// stdout备份fd
stdoutFd2, err := syscall.Dup(1)
assert.NoError(t, err)
outFd, err := os.Create("./testdata/my.dat")
assert.NoError(t, err)
// 重定向stdout 到outFd
err = syscall.Dup2(int(outFd.Fd()), 1)
assert.NoError(t, err)
m.stdout = os.NewFile(uintptr(stdoutFd2), "mystdout")
outFd.Close()
}
func (m *myDup) reset() {
// 还原一个stdout
os.Stdout = m.stdout
}
func (m *myDup) empty() bool {
fd, err := os.Open("./testdata/my.dat")
if err != nil {
return false
}
defer fd.Close()
fi, err := fd.Stat()
if err != nil {
return false
}
return fi.Size() == 0
}
*/

func TestDebug(t *testing.T) {
buf := &bytes.Buffer{}

router := setupDebug(t)
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))

color.NoColor = false
test := []func() DebugOpt{
// 测试颜色
func() DebugOpt {
return DebugFunc(func(o *DebugOption) {
buf.Reset()
o.Debug = true
o.Color = true
o.Write = buf
})
},

// 测试打开日志输出
func() DebugOpt {
return DebugFunc(func(o *DebugOption) {
//t.Logf("--->1.DebugOption address = %p\n", o)
o.Debug = true
})
},

// 测试修改输出源
func() DebugOpt {
return DebugFunc(func(o *DebugOption) {
//t.Logf("--->2.DebugOption address = %p\n", o)
buf.Reset()
o.Debug = true
o.Write = buf
})
},

// 测试环境变量
func() DebugOpt {
return DebugFunc(func(o *DebugOption) {
buf.Reset()
if len(os.Getenv("IOS_DEBUG")) > 0 {
o.Debug = true
}
o.Write = buf
})
},

// 没有颜色输出
NoColor,
}

s := ""
os.Setenv("IOS_DEBUG", "true")
for k, v := range test {
s = ""
err := GET(ts.URL).
Debug(v()).
SetBody(fmt.Sprintf("%d test debug.", k)).
BindBody(&s).
Do()
assert.NoError(t, err)

if k != 0 {
assert.NotEqual(t, buf.Len(), 0)
}

assert.Equal(t, fmt.Sprintf("%d test debug.", k), s)
}

err := GET(ts.URL).Debug(true).SetBody("true test debug").BindBody(&s).Do()

assert.NoError(t, err)
assert.Equal(t, s, "true test debug")

//d := myDup{}
err = GET(ts.URL).Debug(false).SetBody("false test debug").BindBody(&s).Do()
//d.reset()

//assert.Equal(t, false, d.empty())
assert.NoError(t, err)
assert.Equal(t, s, "false test debug")
}

func setupDataFlow(t *testing.T) *gin.Engine {
router := gin.New()

Expand Down
Loading

0 comments on commit 372dd18

Please sign in to comment.