Skip to content

Commit bd1ce74

Browse files
committed
Add cmd/skytest, successed direct test
1 parent 171fd3f commit bd1ce74

29 files changed

+925
-324
lines changed

cmd/skytest/BUILD.bazel

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
2+
# load("@io_bazel_rules_docker//docker:docker.bzl", "docker_build")
3+
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
4+
5+
go_binary(
6+
name = "skytest-client",
7+
srcs = ["client.go"],
8+
deps = [
9+
"//client:go_default_library",
10+
"//cmd/skytest/proto:go_default_library",
11+
"//proto:go_default_library",
12+
"@com_github_binchencoder_letsgo//:go_default_library",
13+
"@com_github_binchencoder_gateway_proto//data:go_default_library",
14+
"@com_github_golang_glog//:go_default_library",
15+
"@com_github_prometheus_client_golang//prometheus:go_default_library",
16+
"@org_golang_x_net//context:go_default_library",
17+
"@org_golang_google_grpc//:go_default_library",
18+
"@org_golang_google_grpc//health/grpc_health_v1:go_default_library",
19+
],
20+
)
21+
22+
go_binary(
23+
name = "skytest-server",
24+
srcs = ["server.go"],
25+
deps = [
26+
"//client:go_default_library",
27+
"//cmd/skytest/proto:go_default_library",
28+
"//server:go_default_library",
29+
"@com_github_binchencoder_letsgo//:go_default_library",
30+
"@com_github_binchencoder_gateway_proto//data:go_default_library",
31+
"@com_github_golang_glog//:go_default_library",
32+
"@com_github_prometheus_client_golang//prometheus:go_default_library",
33+
"@org_golang_x_net//context:go_default_library",
34+
"@org_golang_google_grpc//:go_default_library",
35+
],
36+
)
37+
38+
pkg_tar(
39+
name = "binary",
40+
srcs = [
41+
":skytest-client",
42+
":skytest-server",
43+
],
44+
package_dir = "/skylb",
45+
)
46+
47+
# docker_build(
48+
# name = "latest",
49+
# base = "//bld_tools/docker/ubuntu:base",
50+
# cmd = "/skylb/start.sh",
51+
# repository = "harbor.binchencoder.ltd",
52+
# tag = "skylb/skytest",
53+
# tars = [
54+
# ":binary",
55+
# ],
56+
# )

cmd/skytest/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Overrview
2+
3+
sklb-apiv2/cmd/skytest 是一个测试skylb api的程序
4+
5+
## Build the skytest
6+
7+
#### build gRPC server
8+
9+
```shell
10+
bazel build cmd/skytest:skytest-server
11+
```
12+
13+
#### build gRPC client
14+
15+
```skytest
16+
bazel build cmd/skytest:skytest-client
17+
```
18+
19+
## Run the skytest
20+
21+
#### 确定先启动skylb
22+
23+
```
24+
skylb/bazel-bin/cmd/skylb/linux_amd64_stripped/skylb --etcd-endpoints="http://localhost:2377"
25+
```
26+
27+
#### start gRPC server
28+
29+
1. 注册到skylbserver
30+
31+
```
32+
skylb-apiv2/bazel-bin/cmd/skytest/skytest-server_/skytest-server -skylb-endpoints="127.0.0.1:1900" -v=2 -log_dir=.
33+
```
34+
35+
2. 不注册到skylbserver
36+
```
37+
skylb-apiv2/bazel-bin/cmd/skytest/skytest-server_/skytest-server -port=18000 -alsologtostderr -v=2 -log_dir=.
38+
39+
skylb-apiv2/bazel-bin/cmd/skytest/skytest-server_/skytest-server -port=18001 -alsologtostderr -v=2 -log_dir=.
40+
41+
skylb-apiv2/bazel-bin/cmd/skytest/skytest-server_/skytest-server -port=18002 -alsologtostderr -v=2 -log_dir=.
42+
```
43+
44+
#### start gRPC client
45+
46+
1. 直连
47+
48+
```shell
49+
skylb-apiv2/bazel-bin/cmd/skytest/skytest-server_/skytest-client -debug-svc-endpoint=shared-test-server-service=localhost:18000,localhost:18001,localhost:18002 -alsologtostderr
50+
```
51+
52+
2. 连skylb
53+
54+
```shell
55+
skylb-apiv2/bazel-bin/cmd/skytest/skytest-server_/skytest-client -skylb-endpoints="127.0.0.1:1900" -v=2 -log_dir=.
56+
```
57+
58+

cmd/skytest/client.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"time"
8+
9+
"github.com/golang/glog"
10+
prom "github.com/prometheus/client_golang/prometheus"
11+
"golang.org/x/net/context"
12+
"google.golang.org/grpc"
13+
hpb "google.golang.org/grpc/health/grpc_health_v1"
14+
15+
vexpb "github.com/binchencoder/gateway-proto/data"
16+
"github.com/binchencoder/letsgo"
17+
skylb "github.com/binchencoder/skylb-apiv2/client"
18+
pb "github.com/binchencoder/skylb-apiv2/cmd/skytest/proto"
19+
skypb "github.com/binchencoder/skylb-apiv2/proto"
20+
)
21+
22+
var (
23+
nBatchRequest = flag.Int("n-batch-request", 10000, "The number of batched request")
24+
requestSleep = flag.Duration("request-sleep", 50*time.Millisecond, "The sleep time after each request")
25+
requestTimeout = flag.Duration("request-timeout", 100*time.Millisecond, "The timeout of each request")
26+
27+
spec = skylb.NewServiceSpec(skylb.DefaultNameSpace, vexpb.ServiceId_SHARED_TEST_SERVER_SERVICE, skylb.DefaultPortName)
28+
29+
grpcFailCount = prom.NewCounter(
30+
prom.CounterOpts{
31+
Namespace: "skytest",
32+
Subsystem: "client",
33+
Name: "grpc_call_failure",
34+
Help: "The number of failed gRPC calls.",
35+
},
36+
)
37+
)
38+
39+
func startSkylb(sid vexpb.ServiceId) (skylb.ServiceCli, pb.SkytestClient, hpb.HealthClient) {
40+
skycli := skylb.NewServiceCli(vexpb.ServiceId_SHARED_TEST_CLIENT_SERVICE)
41+
skycli.Resolve(skylb.NewServiceSpec(skylb.DefaultNameSpace, sid, skylb.DefaultPortName))
42+
// skycli.EnableHistogram()
43+
var cli pb.SkytestClient
44+
var healthCli hpb.HealthClient
45+
skycli.Start(func(spec *skypb.ServiceSpec, conn *grpc.ClientConn) {
46+
cli = pb.NewSkytestClient(conn)
47+
healthCli = hpb.NewHealthClient(conn)
48+
})
49+
return skycli, cli, healthCli
50+
}
51+
52+
func usage() {
53+
fmt.Println(`Skytest gRPC client.
54+
55+
Usage:
56+
skytest-client [options]
57+
58+
Options:`)
59+
60+
flag.PrintDefaults()
61+
os.Exit(2)
62+
}
63+
64+
func main() {
65+
letsgo.Init(letsgo.FlagUsage(usage))
66+
67+
testClient()
68+
}
69+
70+
func testClient() {
71+
sl, cli, healthCli := startSkylb(vexpb.ServiceId_SHARED_TEST_SERVER_SERVICE)
72+
for {
73+
for i := 0; i < *nBatchRequest; i++ {
74+
req := pb.GreetingRequest{
75+
Name: fmt.Sprintf("John Doe %d", time.Now().Second()),
76+
}
77+
ctx, cancel := context.WithTimeout(context.Background(), *requestTimeout)
78+
resp, err := cli.Greeting(ctx, &req, grpc.FailFast(false))
79+
if err != nil {
80+
cancel()
81+
glog.Errorf("Failed to greet service, %v", err)
82+
fmt.Printf("Failed to greet service, %v", err)
83+
grpcFailCount.Inc()
84+
time.Sleep(*requestTimeout)
85+
continue
86+
}
87+
88+
glog.Infof("Greeting resp: %v \n", resp)
89+
90+
healthCli.Check(context.Background(), &hpb.HealthCheckRequest{})
91+
time.Sleep(*requestSleep)
92+
}
93+
}
94+
95+
sl.Shutdown()
96+
}

cmd/skytest/grpc.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"time"
8+
9+
"github.com/golang/glog"
10+
"google.golang.org/grpc"
11+
12+
pb "github.com/binchencoder/skylb-apiv2/cmd/skytest/proto"
13+
)
14+
15+
var (
16+
grpcEndpoint = flag.String("grpc-endpoint", "192.168.221.104:18000", "The gRPC server endpoint")
17+
timeout = flag.Duration("timeout", time.Second, "The timeout to call gRPC service")
18+
)
19+
20+
func main() {
21+
flag.Parse()
22+
23+
for {
24+
testDirectGrpc()
25+
time.Sleep(100 * time.Millisecond)
26+
}
27+
}
28+
29+
func testDirectGrpc() {
30+
conn, err := grpc.Dial(*grpcEndpoint, grpc.WithInsecure())
31+
if err != nil {
32+
glog.Fatalf("fail to dial: %v", err)
33+
}
34+
defer conn.Close()
35+
36+
cli := pb.NewSkytestClient(conn)
37+
38+
req := pb.GreetingRequest{
39+
Name: fmt.Sprintf("John Doe %d", time.Now().Second()),
40+
}
41+
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
42+
// fuyc: "cancel" may not be used on all execution paths. Safely ignore
43+
// warnings from "go tool vet".
44+
resp, err := cli.Greeting(ctx, &req, grpc.FailFast(false))
45+
if err != nil {
46+
cancel()
47+
glog.Errorf("Failed to greet service, %v", err)
48+
fmt.Printf("Failed to greet service, %v", err)
49+
time.Sleep(100 * time.Millisecond)
50+
return
51+
}
52+
fmt.Printf("Demo Reply: %s\n", resp.Greeting)
53+
}

cmd/skytest/proto/BUILD.bazel

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package(
2+
default_visibility = ["//visibility:public"],
3+
)
4+
5+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
6+
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
7+
8+
proto_library(
9+
name = "api_proto",
10+
srcs = ["api.proto"],
11+
)
12+
13+
go_proto_library(
14+
name = "api_go_proto",
15+
compilers = [
16+
"@io_bazel_rules_go//proto:go_grpc",
17+
],
18+
importpath = "github.com/binchencoder/skylb-apiv2/cmd/skytest/proto",
19+
proto = ":api_proto",
20+
)
21+
22+
go_library(
23+
name = "go_default_library",
24+
embed = [
25+
":api_go_proto",
26+
],
27+
importpath = "github.com/binchencoder/skylb-apiv2/cmd/skytest/proto",
28+
deps = [
29+
"@com_github_golang_protobuf//proto:go_default_library",
30+
"@org_golang_x_net//context:go_default_library",
31+
"@org_golang_google_grpc//:go_default_library",
32+
],
33+
)
34+
35+
java_proto_library(
36+
name = "api_java_proto",
37+
deps = [
38+
":api_proto",
39+
],
40+
)
41+
42+
# java_lite_proto_library(
43+
# name = "api_java_lite_proto",
44+
# deps = [
45+
# ":api_proto",
46+
# ],
47+
# )
48+
49+
# genproto_java(
50+
# name = "skytest_src",
51+
# srcs = ["api.proto"],
52+
# has_service = 1,
53+
# deps = [
54+
# "@com_github_binchencoder_ease_gateway//httpoptions:options_proto",
55+
# "@binchencoder_third_party_go//vendor/github.com/google/protobuf/src/google/protobuf:protos_java",
56+
# "@com_github_binchencoder_gateway_proto//data:data_srcjar",
57+
# ],
58+
# )
59+
60+
# java_library(
61+
# name = "skytest",
62+
# # srcs = [":api_proto"],
63+
# runtime_deps = [
64+
# ":api_java_proto",
65+
# ],
66+
# deps = [
67+
# # "@com_github_binchencoder_ease_gateway//httpoptions",
68+
# "//vendor/github.com/binchencoder/third-party-java/dependencies/lib:grpc-1.18.0",
69+
# ],
70+
# )

0 commit comments

Comments
 (0)