forked from q191201771/naza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加 /demo/connstat 用于测试 net.Conn.SetWriteDeadline 的性能开销
- Loading branch information
1 parent
856141e
commit 8f7f3be
Showing
10 changed files
with
168 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ coverage.html | |
profile.out | ||
|
||
/.idea | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -x | ||
|
||
ROOT_DIR=`pwd` | ||
echo ${ROOT_DIR}/bin | ||
|
||
if [ ! -d ${ROOT_DIR}/bin ]; then | ||
mkdir bin | ||
fi | ||
|
||
cd ${ROOT_DIR}/demo/connstat && go build -o ${ROOT_DIR}/bin/connstat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -x | ||
|
||
ROOT_DIR=`pwd` | ||
echo ${ROOT_DIR}/bin | ||
|
||
if [ ! -d ${ROOT_DIR}/bin ]; then | ||
mkdir bin | ||
fi | ||
|
||
cd ${ROOT_DIR}/demo/connstat && GOOS=linux GOARCH=amd64 go build -o ${ROOT_DIR}/bin/connstat_linux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"github.com/q191201771/nezha/pkg/errors" | ||
"github.com/q191201771/nezha/pkg/log" | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// 测试 net.Conn.SetWriteDeadline 的性能开销 | ||
// | ||
// 使用 <numOfConn> 个客户端端连接,并行向服务端发送数据 | ||
// 每个客户端发送 <numOfMsgPerConn> 个消息 | ||
// 服务端 MockServer 只接收数据,不做其他逻辑 | ||
|
||
const ( | ||
addr = ":10027" | ||
) | ||
|
||
var ( | ||
numOfConn int | ||
numOfMsgPerConn int | ||
) | ||
|
||
func raw(writeTimeoutSec int) { | ||
log.Infof("> raw. writeTimeoutSec=%d", writeTimeoutSec) | ||
var ms MockServer | ||
var conns []net.Conn | ||
buf := make([]byte, 8) | ||
|
||
ms.start(addr) | ||
for i := 0; i < numOfConn; i++ { | ||
conn, err := net.Dial("tcp", addr) | ||
errors.PanicIfErrorOccur(err) | ||
conns = append(conns, conn) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(numOfConn) | ||
|
||
b := time.Now() | ||
log.Infof("b:%+v", b) | ||
for i := 0; i < numOfConn; i++ { | ||
go func(ii int) { | ||
for j := 0; j < numOfMsgPerConn; j++ { | ||
if writeTimeoutSec != 0 { | ||
err := conns[ii].SetWriteDeadline(time.Now().Add(time.Duration(writeTimeoutSec) * time.Second)) | ||
errors.PanicIfErrorOccur(err) | ||
} | ||
_, err := conns[ii].Write(buf) | ||
errors.PanicIfErrorOccur(err) | ||
} | ||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
log.Infof("cost=%v", time.Now().Sub(b)) | ||
|
||
for i := 0; i < numOfConn; i++ { | ||
err := conns[i].Close() | ||
errors.PanicIfErrorOccur(err) | ||
} | ||
ms.stop() | ||
log.Info("< raw.") | ||
} | ||
|
||
func main() { | ||
c := flag.Int("c", 0, "num of conn") | ||
n := flag.Int("n", 0, "num of msg per conn") | ||
flag.Parse() | ||
if *c == 0 || *n == 0 { | ||
flag.Usage() | ||
return | ||
} | ||
numOfConn = *c | ||
numOfMsgPerConn = *n | ||
|
||
raw(0) | ||
raw(5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import "net" | ||
|
||
// MockServer 开启一个TCP监听,并从accept的连接上循环读取数据 | ||
|
||
type MockServer struct { | ||
l net.Listener | ||
} | ||
|
||
func (ms *MockServer) start(addr string) (err error) { | ||
ms.l, err = net.Listen("tcp", addr) | ||
if err != nil { | ||
return err | ||
} | ||
go func() { | ||
for { | ||
conn, err := ms.l.Accept() | ||
if err != nil { | ||
//fmt.Println(err) | ||
return | ||
} | ||
go func() { | ||
buf := make([]byte, 8) | ||
for { | ||
_, err = conn.Read(buf) | ||
if err != nil { | ||
//fmt.Println(err) | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
}() | ||
return | ||
} | ||
|
||
func (ms *MockServer) stop() { | ||
ms.l.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters