github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/api/rpc/liteclient/client.go (about) 1 package liteclient 2 3 import ( 4 "context" 5 "io" 6 "log" 7 8 pb "github.com/laof/lite-speed-test/api/rpc/lite" 9 "github.com/laof/lite-speed-test/download" 10 "google.golang.org/grpc" 11 "google.golang.org/grpc/credentials/insecure" 12 ) 13 14 func StartClient(addr string, req *pb.TestRequest) ([]*pb.TestReply, error) { 15 conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) 16 if err != nil { 17 return nil, err 18 } 19 defer conn.Close() 20 c := pb.NewTestProxyClient(conn) 21 ctx := context.Background() 22 stream, err := c.StartTest(ctx, req) 23 if err != nil { 24 return nil, err 25 } 26 result := []*pb.TestReply{} 27 for { 28 reply, err := stream.Recv() 29 if err == io.EOF { 30 stream.CloseSend() 31 break 32 } 33 if err != nil { 34 return nil, err 35 } 36 log.Println("id: ", reply.Id, reply.Remarks, "ping:", reply.Ping, "avg:", download.ByteCountIECTrim(reply.AvgSpeed), "max:", download.ByteCountIECTrim(reply.MaxSpeed)) 37 result = append(result, reply) 38 39 } 40 return result, nil 41 }