Skip to content

Commit 372dd18

Browse files
authored
Global debug (#340)
* 把debug相关代码从dataflow模块中提出来 * 重构debug: 冗余命名,去掉多层结构引用 * Debug: 全局debug和保存文件
1 parent 1d93dc6 commit 372dd18

24 files changed

+602
-342
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
protobuf:
22
protoc --go_out=. --go_opt=paths=source_relative testdata/test.proto
3+
test:
4+
go test ./...

README.md

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ gout 是go写的http 客户端,为提高工作效率而开发
7878
- [unix socket](#unix-socket)
7979
- [http2 doc](#http2-doc)
8080
- [debug mode](#debug-mode)
81-
- [Turn on debug mode](#Turn-on-debug-mode)
82-
- [Turn off color highlighting in debug mode](#Turn-off-color-highlighting-in-debug-mode)
83-
- [Custom debug mode](#Custom-debug-mode)
81+
- [Turn on debug mode](#turn-on-debug-mode)
82+
- [Turn off color highlighting in debug mode](#turn-off-color-highlighting-in-debug-mode)
83+
- [Custom debug mode](#custom-debug-mode)
8484
- [trace info](#trace-info)
85+
- [save to writer](#save-to-writer)
86+
- [save to file](#save-to-file)
8587
- [benchmark](#benchmark)
8688
- [benchmarking a certain number of times](#benchmarking-a-certain-number-of-times)
8789
- [benchmarking for a certain time](#benchmark-duration)
@@ -1419,12 +1421,16 @@ func main() {
14191421
}
14201422
```
14211423
### Turn off color highlighting in debug mode
1422-
使用gout.NoColor()传入Debug函数关闭颜色高亮
1424+
使用debug.NoColor()传入Debug函数关闭颜色高亮
14231425
```go
1426+
import (
1427+
"github.com/guonaihong/gout"
1428+
"github.com/guonaihong/gout/debug"
1429+
)
14241430
func main() {
14251431
14261432
err := gout.POST(":8080/colorjson").
1427-
Debug(gout.NoColor()).
1433+
Debug(debug.NoColor()).
14281434
SetJSON(gout.H{"str": "foo",
14291435
"num": 100,
14301436
"bool": false,
@@ -1447,11 +1453,12 @@ package main
14471453
import (
14481454
"fmt"
14491455
"github.com/guonaihong/gout"
1456+
"github.com/guonaihong/gout/debug"
14501457
"os"
14511458
)
14521459
1453-
func IOSDebug() gout.DebugOpt {
1454-
return gout.DebugFunc(func(o *gout.DebugOption) {
1460+
func IOSDebug() debug.Apply {
1461+
return gout.DebugFunc(func(o *debug.Options) {
14551462
if len(os.Getenv("IOS_DEBUG")) > 0 {
14561463
o.Debug = true
14571464
}
@@ -1477,18 +1484,19 @@ func main() {
14771484
// env IOS_DEBUG=true go run customize.go
14781485
```
14791486
### trace info
1480-
gout.Trace()可输出http各个阶段的耗时,比如dns lookup时间,tcp连接时间等等。可以很方便的做些性能调优。
1487+
debug.Trace()可输出http各个阶段的耗时,比如dns lookup时间,tcp连接时间等等。可以很方便的做些性能调优。
14811488
```go
14821489
package main
14831490
14841491
import (
14851492
"fmt"
14861493
"github.com/guonaihong/gout"
1494+
"github.com/guonaihong/gout/debug"
14871495
)
14881496
14891497
func openDebugTrace() {
14901498
err := gout.POST(":8080/colorjson").
1491-
Debug(gout.Trace()).
1499+
Debug(debug.Trace()).
14921500
SetJSON(gout.H{"str": "foo",
14931501
"num": 100,
14941502
"bool": false,
@@ -1515,6 +1523,53 @@ func openDebugTrace() {
15151523
TotalDuration : 2.13921ms
15161524
=================== Trace Info(E): ===================
15171525
```
1526+
### save to writer
1527+
`debug.ToWriter`可以传递任何io.Writer对象,比如`bytes.Buffer`, 文件等。。。
1528+
```go
1529+
import (
1530+
"github.com/guonaihong/gout"
1531+
"github.com/guonaihong/gout/debug"
1532+
)
1533+
func main() {
1534+
var buf bytes.Buffer
1535+
err := gout.POST(":8080/colorjson").
1536+
Debug(debug.ToWriter(&buf, false)).
1537+
SetJSON(gout.H{"str": "foo",
1538+
"num": 100,
1539+
"bool": false,
1540+
"null": nil,
1541+
"array": gout.A{"foo", "bar", "baz"},
1542+
"obj": gout.H{"a": 1, "b": 2},
1543+
}).Do()
1544+
1545+
if err != nil {
1546+
fmt.Printf("err = %v\n", err)
1547+
}
1548+
}
1549+
```
1550+
### save to file
1551+
```go
1552+
import (
1553+
"github.com/guonaihong/gout"
1554+
"github.com/guonaihong/gout/debug"
1555+
)
1556+
func main() {
1557+
1558+
err := gout.POST(":8080/colorjson").
1559+
Debug(debug.ToFile("./req.txt", false)).
1560+
SetJSON(gout.H{"str": "foo",
1561+
"num": 100,
1562+
"bool": false,
1563+
"null": nil,
1564+
"array": gout.A{"foo", "bar", "baz"},
1565+
"obj": gout.H{"a": 1, "b": 2},
1566+
}).Do()
1567+
1568+
if err != nil {
1569+
fmt.Printf("err = %v\n", err)
1570+
}
1571+
}
1572+
```
15181573
## benchmark
15191574
### benchmarking a certain number of times
15201575
下面的例子,起了20并发。对:8080端口的服务,发送3000次请求进行压测,内容为json结构

dataflow/dataflow.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"time"
1111

12+
"github.com/guonaihong/gout/debug"
1213
"github.com/guonaihong/gout/decode"
1314
"github.com/guonaihong/gout/encode"
1415
api "github.com/guonaihong/gout/interface"
@@ -179,29 +180,29 @@ func (df *DataFlow) SetHeader(obj ...interface{}) *DataFlow {
179180

180181
// SetJSON send json to the http body, Support raw json(string, []byte)/struct/map types
181182
func (df *DataFlow) SetJSON(obj interface{}) *DataFlow {
182-
df.out.opt.ReqBodyType = "json"
183+
df.ReqBodyType = "json"
183184
df.Req.bodyEncoder = encode.NewJSONEncode(obj)
184185
return df
185186
}
186187

187188
// SetXML send xml to the http body
188189
func (df *DataFlow) SetXML(obj interface{}) *DataFlow {
189-
df.out.opt.ReqBodyType = "xml"
190+
df.ReqBodyType = "xml"
190191
df.Req.bodyEncoder = encode.NewXMLEncode(obj)
191192
return df
192193
}
193194

194195
// SetYAML send yaml to the http body, Support struct,map types
195196
func (df *DataFlow) SetYAML(obj interface{}) *DataFlow {
196-
df.out.opt.ReqBodyType = "yaml"
197+
df.ReqBodyType = "yaml"
197198
df.Req.bodyEncoder = encode.NewYAMLEncode(obj)
198199
return df
199200
}
200201

201202
// SetProtoBuf send yaml to the http body, Support struct types
202203
// obj必须是结构体指针或者[]byte类型
203204
func (df *DataFlow) SetProtoBuf(obj interface{}) *DataFlow {
204-
df.out.opt.ReqBodyType = "protobuf"
205+
df.ReqBodyType = "protobuf"
205206
df.Req.bodyEncoder = encode.NewProtoBufEncode(obj)
206207
return df
207208
}
@@ -309,7 +310,7 @@ func (df *DataFlow) BindJSON(obj interface{}) *DataFlow {
309310
if obj == nil {
310311
return df
311312
}
312-
df.out.opt.RspBodyType = "json"
313+
df.out.RspBodyType = "json"
313314
df.Req.bodyDecoder = append(df.Req.bodyDecoder, decode.NewJSONDecode(obj))
314315
return df
315316

@@ -321,7 +322,7 @@ func (df *DataFlow) BindYAML(obj interface{}) *DataFlow {
321322
if obj == nil {
322323
return df
323324
}
324-
df.out.opt.RspBodyType = "yaml"
325+
df.RspBodyType = "yaml"
325326
df.Req.bodyDecoder = append(df.Req.bodyDecoder, decode.NewYAMLDecode(obj))
326327
return df
327328
}
@@ -332,7 +333,7 @@ func (df *DataFlow) BindXML(obj interface{}) *DataFlow {
332333
if obj == nil {
333334
return df
334335
}
335-
df.out.opt.RspBodyType = "xml"
336+
df.RspBodyType = "xml"
336337
df.Req.bodyDecoder = append(df.Req.bodyDecoder, decode.NewXMLDecode(obj))
337338
return df
338339
}
@@ -398,10 +399,10 @@ func (df *DataFlow) Debug(d ...interface{}) *DataFlow {
398399
switch opt := v.(type) {
399400
case bool:
400401
if opt {
401-
defaultDebug(&df.out.opt)
402+
debug.DefaultDebug(&df.Setting.Options)
402403
}
403-
case DebugOpt:
404-
opt.Apply(&df.out.opt)
404+
case debug.Apply:
405+
opt.Apply(&df.Setting.Options)
405406
}
406407
}
407408

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

422423
func (df *DataFlow) IsDebug() bool {
423-
return df.out.opt.Debug
424+
return df.Setting.Debug
424425
}
425426

426427
// Do send function

dataflow/dataflow_test.go

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dataflow
22

33
import (
4-
"bytes"
54
"context"
65
"errors"
76
"fmt"
@@ -18,7 +17,6 @@ import (
1817
"time"
1918

2019
"github.com/gin-gonic/gin"
21-
"github.com/guonaihong/gout/color"
2220
"github.com/guonaihong/gout/core"
2321
"github.com/stretchr/testify/assert"
2422
)
@@ -537,131 +535,6 @@ func setupDebug(t *testing.T) *gin.Engine {
537535
return r
538536
}
539537

540-
/*
541-
// TODO
542-
type myDup struct {
543-
stdout *os.File
544-
}
545-
546-
func (m *myDup) dup(t *testing.T) {
547-
// stdout备份fd
548-
stdoutFd2, err := syscall.Dup(1)
549-
assert.NoError(t, err)
550-
551-
outFd, err := os.Create("./testdata/my.dat")
552-
assert.NoError(t, err)
553-
554-
// 重定向stdout 到outFd
555-
err = syscall.Dup2(int(outFd.Fd()), 1)
556-
assert.NoError(t, err)
557-
m.stdout = os.NewFile(uintptr(stdoutFd2), "mystdout")
558-
outFd.Close()
559-
}
560-
561-
func (m *myDup) reset() {
562-
// 还原一个stdout
563-
os.Stdout = m.stdout
564-
}
565-
566-
func (m *myDup) empty() bool {
567-
fd, err := os.Open("./testdata/my.dat")
568-
if err != nil {
569-
return false
570-
}
571-
defer fd.Close()
572-
573-
fi, err := fd.Stat()
574-
if err != nil {
575-
return false
576-
}
577-
578-
return fi.Size() == 0
579-
}
580-
*/
581-
582-
func TestDebug(t *testing.T) {
583-
buf := &bytes.Buffer{}
584-
585-
router := setupDebug(t)
586-
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
587-
588-
color.NoColor = false
589-
test := []func() DebugOpt{
590-
// 测试颜色
591-
func() DebugOpt {
592-
return DebugFunc(func(o *DebugOption) {
593-
buf.Reset()
594-
o.Debug = true
595-
o.Color = true
596-
o.Write = buf
597-
})
598-
},
599-
600-
// 测试打开日志输出
601-
func() DebugOpt {
602-
return DebugFunc(func(o *DebugOption) {
603-
//t.Logf("--->1.DebugOption address = %p\n", o)
604-
o.Debug = true
605-
})
606-
},
607-
608-
// 测试修改输出源
609-
func() DebugOpt {
610-
return DebugFunc(func(o *DebugOption) {
611-
//t.Logf("--->2.DebugOption address = %p\n", o)
612-
buf.Reset()
613-
o.Debug = true
614-
o.Write = buf
615-
})
616-
},
617-
618-
// 测试环境变量
619-
func() DebugOpt {
620-
return DebugFunc(func(o *DebugOption) {
621-
buf.Reset()
622-
if len(os.Getenv("IOS_DEBUG")) > 0 {
623-
o.Debug = true
624-
}
625-
o.Write = buf
626-
})
627-
},
628-
629-
// 没有颜色输出
630-
NoColor,
631-
}
632-
633-
s := ""
634-
os.Setenv("IOS_DEBUG", "true")
635-
for k, v := range test {
636-
s = ""
637-
err := GET(ts.URL).
638-
Debug(v()).
639-
SetBody(fmt.Sprintf("%d test debug.", k)).
640-
BindBody(&s).
641-
Do()
642-
assert.NoError(t, err)
643-
644-
if k != 0 {
645-
assert.NotEqual(t, buf.Len(), 0)
646-
}
647-
648-
assert.Equal(t, fmt.Sprintf("%d test debug.", k), s)
649-
}
650-
651-
err := GET(ts.URL).Debug(true).SetBody("true test debug").BindBody(&s).Do()
652-
653-
assert.NoError(t, err)
654-
assert.Equal(t, s, "true test debug")
655-
656-
//d := myDup{}
657-
err = GET(ts.URL).Debug(false).SetBody("false test debug").BindBody(&s).Do()
658-
//d.reset()
659-
660-
//assert.Equal(t, false, d.empty())
661-
assert.NoError(t, err)
662-
assert.Equal(t, s, "false test debug")
663-
}
664-
665538
func setupDataFlow(t *testing.T) *gin.Engine {
666539
router := gin.New()
667540

0 commit comments

Comments
 (0)