Test: add basic protocol benchmark

This commit is contained in:
Dreamacro 2021-07-18 17:23:22 +08:00
parent a461c2306a
commit 91ed0118f6
9 changed files with 1020 additions and 46 deletions

View file

@ -619,6 +619,41 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
assert.NoError(t, testPacketConnTimeout(t, pc))
}
func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) {
l, err := net.Listen("tcp", ":10001")
if err != nil {
assert.FailNow(b, err.Error())
}
go func() {
c, err := l.Accept()
if err != nil {
assert.FailNow(b, err.Error())
}
io.Copy(io.Discard, c)
c.Close()
}()
chunk := make([]byte, 1024)
conn, err := proxy.DialContext(context.Background(), &C.Metadata{
Host: localIP.String(),
DstPort: "10001",
AddrType: socks5.AtypDomainName,
})
if err != nil {
assert.FailNow(b, err.Error())
}
b.SetBytes(1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := conn.Write(chunk); err != nil {
assert.FailNow(b, err.Error())
}
}
}
func TestClash_Basic(t *testing.T) {
basic := `
mixed-port: 10000

View file

@ -3,24 +3,17 @@ module clash-test
go 1.16
require (
github.com/Dreamacro/clash v1.6.1-0.20210516120541-06fdd3abe0ab
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/containerd/containerd v1.4.4 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.6+incompatible
github.com/Dreamacro/clash v1.6.5
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/containerd/containerd v1.5.3 // indirect
github.com/docker/docker v20.10.7+incompatible
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/miekg/dns v1.1.42
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/miekg/dns v1.1.43
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210510120150-4163338589ed
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
google.golang.org/grpc v1.36.0 // indirect
gotest.tools/v3 v3.0.3 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 // indirect
google.golang.org/grpc v1.39.0 // indirect
)

File diff suppressed because it is too large Load diff

View file

@ -119,3 +119,40 @@ func TestClash_Snell(t *testing.T) {
time.Sleep(waitTime)
testSuit(t, proxy)
}
func Benchmark_Snell(b *testing.B) {
cfg := &container.Config{
Image: ImageSnell,
ExposedPorts: defaultExposedPorts,
Cmd: []string{"-c", "/config.conf"},
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{fmt.Sprintf("%s:/config.conf", C.Path.Resolve("snell-http.conf"))},
}
id, err := startContainer(cfg, hostCfg, "snell-http")
if err != nil {
assert.FailNow(b, err.Error())
}
b.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewSnell(outbound.SnellOption{
Name: "snell",
Server: localIP.String(),
Port: 10002,
Psk: "password",
ObfsOpts: map[string]interface{}{
"mode": "http",
},
})
if err != nil {
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime)
benchmarkProxy(b, proxy)
}

View file

@ -170,3 +170,38 @@ func TestClash_ShadowsocksV2RayPlugin(t *testing.T) {
time.Sleep(waitTime)
testSuit(t, proxy)
}
func Benchmark_Shadowsocks(b *testing.B) {
cfg := &container.Config{
Image: ImageShadowsocks,
Env: []string{"SS_MODULE=ss-server", "SS_CONFIG=-s 0.0.0.0 -u -v -p 10002 -m chacha20-ietf-poly1305 -k FzcLbKs2dY9mhL"},
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
}
id, err := startContainer(cfg, hostCfg, "ss")
if err != nil {
assert.FailNow(b, err.Error())
}
b.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
Password: "FzcLbKs2dY9mhL",
Cipher: "chacha20-ietf-poly1305",
UDP: true,
})
if err != nil {
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime)
benchmarkProxy(b, proxy)
}

View file

@ -92,3 +92,43 @@ func TestClash_TrojanGrpc(t *testing.T) {
time.Sleep(waitTime)
testSuit(t, proxy)
}
func Benchmark_Trojan(b *testing.B) {
cfg := &container.Config{
Image: ImageTrojan,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{
fmt.Sprintf("%s:/config/config.json", C.Path.Resolve("trojan.json")),
fmt.Sprintf("%s:/path/to/certificate.crt", C.Path.Resolve("example.org.pem")),
fmt.Sprintf("%s:/path/to/private.key", C.Path.Resolve("example.org-key.pem")),
},
}
id, err := startContainer(cfg, hostCfg, "trojan")
if err != nil {
assert.FailNow(b, err.Error())
}
b.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewTrojan(outbound.TrojanOption{
Name: "trojan",
Server: localIP.String(),
Port: 10002,
Password: "password",
SNI: "example.org",
SkipCertVerify: true,
UDP: true,
})
if err != nil {
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime)
benchmarkProxy(b, proxy)
}

12
test/util_other_test.go Normal file
View file

@ -0,0 +1,12 @@
// +build !darwin
package main
import (
"errors"
"net"
)
func defaultRouteIP() (net.IP, error) {
return nil, errors.New("not supported")
}

View file

@ -345,3 +345,41 @@ func TestClash_VmessGrpc(t *testing.T) {
time.Sleep(waitTime)
testSuit(t, proxy)
}
func Benchmark_Vmess(b *testing.B) {
configPath := C.Path.Resolve("vmess.json")
cfg := &container.Config{
Image: ImageVmess,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{fmt.Sprintf("%s:/etc/v2ray/config.json", configPath)},
}
id, err := startContainer(cfg, hostCfg, "vmess")
if err != nil {
assert.FailNow(b, err.Error())
}
b.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess",
Server: localIP.String(),
Port: 10002,
UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
Cipher: "auto",
AlterID: 32,
UDP: true,
})
if err != nil {
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime)
benchmarkProxy(b, proxy)
}