Skip to content

Commit 37b8e2d

Browse files
committed
Fix:修复一些问题
1 parent 8b98221 commit 37b8e2d

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

internal/perf/group.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/http"
99
"strings"
10+
"sync"
1011
"time"
1112

1213
"golang.org/x/time/rate"
@@ -160,6 +161,13 @@ func (tg *TcpGroup) task() {
160161
}
161162
}
162163

164+
// 添加一个 bufio.Reader 对象池
165+
var bufioReaderPool = &sync.Pool{
166+
New: func() interface{} {
167+
return bufio.NewReader(nil)
168+
},
169+
}
170+
163171
func (tg *TcpGroup) doReq(conn net.Conn, httpByte []byte) (*ReqResult, error) {
164172
start := time.Now()
165173

@@ -175,17 +183,22 @@ func (tg *TcpGroup) doReq(conn net.Conn, httpByte []byte) (*ReqResult, error) {
175183
return nil, fmt.Errorf("set read deadline: %w", err)
176184
}
177185

178-
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
186+
// 从对象池获取 reader
187+
br := bufioReaderPool.Get().(*bufio.Reader)
188+
br.Reset(conn)
189+
defer bufioReaderPool.Put(br)
190+
191+
resp, err := http.ReadResponse(br, nil)
179192
if err != nil {
180193
return nil, fmt.Errorf("read response: %w", err)
181194
}
182195
defer resp.Body.Close()
183196

184197
respTime := time.Since(start).Nanoseconds()
185198

186-
return &ReqResult{
187-
code: resp.StatusCode,
188-
start: start,
189-
reqtime: respTime,
190-
}, nil
199+
result := GetReqResult()
200+
result.code = resp.StatusCode
201+
result.start = start
202+
result.reqtime = respTime
203+
return result, nil
191204
}

internal/perf/params.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func newRandomInt(pc *ParamsConf) (*RandomInt, error) {
7979
}
8080

8181
func (r *RandomInt) replace(src []byte) []byte {
82-
return bytes.Replace(src, r.name, Randomer.IntBytes(r.start, r.end), -1)
82+
return bytes.ReplaceAll(src, r.name, Randomer.IntBytes(r.start, r.end))
8383
}
8484

8585
type RandomStr struct {
@@ -103,7 +103,7 @@ func newRandomStr(pc *ParamsConf) (*RandomStr, error) {
103103
}
104104

105105
func (r *RandomStr) replace(src []byte) []byte {
106-
return bytes.Replace(src, r.name, Randomer.StrBytes(r.length), -1)
106+
return bytes.ReplaceAll(src, r.name, Randomer.StrBytes(r.length))
107107
}
108108

109109
// validate 验证参数配置

internal/perf/pool.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,12 @@ func (pool *ConnPool) getConn(dialer *net.Dialer) (net.Conn, error) {
271271
}
272272

273273
func (pool *ConnPool) Get() *MyConn {
274-
myconn := <-pool.connsChan
275-
return myconn
274+
select {
275+
case <-pool.ctx.ctx.Done():
276+
return nil
277+
case myconn := <-pool.connsChan:
278+
return myconn
279+
}
276280
}
277281

278282
// 获取连接
@@ -321,23 +325,22 @@ func (pool *ConnPool) factory() {
321325
select {
322326
case <-pool.ctx.ctx.Done():
323327
return
324-
case conn, ok := <-pool.factoryChan:
328+
case myconn, ok := <-pool.factoryChan:
325329
if !ok {
326330
return
327331
}
328332

329-
conn.Close()
333+
myconn.Close()
330334
atomic.AddInt32(&ActiveConnCount, -1)
331-
332-
newConn, err := pool.getConn(conn.dialer)
335+
newConn, err := pool.getConn(myconn.dialer)
333336
if err != nil {
334-
pool.factoryChan <- conn // retry
337+
pool.factoryChan <- myconn // retry
335338
continue
336339
}
337340

338-
conn.Conn = newConn
341+
myconn.Conn = newConn
339342
atomic.AddInt32(&ActiveConnCount, 1)
340-
pool.connsChan <- conn
343+
pool.connsChan <- myconn
341344
}
342345
}
343346
}

internal/perf/report.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ type ReqResult struct {
4747
reqtime int64
4848
}
4949

50+
var reqResultPool = &sync.Pool{
51+
New: func() interface{} {
52+
return &ReqResult{}
53+
},
54+
}
55+
56+
// GetReqResult 从对象池获取 ReqResult 对象
57+
func GetReqResult() *ReqResult {
58+
return reqResultPool.Get().(*ReqResult)
59+
}
60+
61+
func PutReqResult(r *ReqResult) {
62+
// 重置对象状态
63+
r.code = 0
64+
r.start = time.Time{}
65+
r.reqtime = 0
66+
reqResultPool.Put(r)
67+
}
68+
5069
func NewReport(ctx *RunCtx, maxResult int) *Report {
5170
return &Report{
5271
Success: 0,
@@ -101,6 +120,7 @@ func (r *Report) Printer() {
101120
r.initStartTime(result.start)
102121
isStarted = true
103122
lastPrintTime = result.start
123+
PutReqResult(result)
104124
continue
105125
}
106126

@@ -110,6 +130,7 @@ func (r *Report) Printer() {
110130
r.printProgress(rowFormat)
111131
lastPrintTime = result.start
112132
}
133+
PutReqResult(result)
113134
default:
114135
}
115136
}

internal/server/web.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ func (s *WebServer) handleStopTest(w http.ResponseWriter, r *http.Request) {
122122
atomic.StoreInt32(&s.isRunning, 0)
123123

124124
result := s.getFinshTestResult()
125-
125+
if result.Duration == 0 {
126+
result = TestResult{0, 0, 0, 0, 0, 0, 0, 0, map[int]int{}}
127+
}
126128
w.WriteHeader(http.StatusOK)
127129
json.NewEncoder(w).Encode(map[string]interface{}{
128130
"status": "stopped",

web/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
.layui-card {
3737
margin-bottom: 15px;
3838
}
39+
.layui-card {
40+
margin-bottom: 15px;
41+
border: 1px solid #d9d9d9; /* 加深边框颜色 */
42+
background-color: #f5f5f5; /* 加深背景颜色 */
43+
}
44+
45+
/* 加深卡片头部边框颜色和背景 */
46+
.layui-card-header {
47+
border-bottom: 1px solid #d9d9d9; /* 加深边框颜色 */
48+
background-color: #f0f0f0; /* 加深头部背景颜色 */
49+
}
3950
</style>
4051
</head>
4152
<body class="layui-layout-body">

0 commit comments

Comments
 (0)