-
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.
Initial PoC commit with minimally working echo grpc server and client
- Loading branch information
0 parents
commit ed77b45
Showing
4 changed files
with
315 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
pb "github.com/smirnov/grpc-echo/pb" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
warmupRounds = 200 | ||
rounds = 1000 | ||
) | ||
|
||
var ( | ||
address = "localhost:50051" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) > 1 { | ||
address = os.Args[1] | ||
} | ||
// Set up a connection to the server. | ||
//var durations []time.Duration | ||
|
||
//Warmup run | ||
min := time.Hour | ||
max := time.Nanosecond | ||
sum := time.Nanosecond * 0 | ||
for i := 0; i < warmupRounds; i++ { | ||
duration, err := establishConnectionAndCallRemote(address) | ||
if err == nil { | ||
//append(durations, duration) | ||
if min > duration { | ||
min = duration | ||
} | ||
if max < duration { | ||
max = duration | ||
} | ||
sum = sum + duration | ||
} | ||
} | ||
avg := sum / warmupRounds | ||
fmt.Printf("Measurement run for %d warmup rounds\n", warmupRounds) | ||
fmt.Printf("avg: %s min: %s max: %s\n", avg, min, max) | ||
|
||
//Repeat for measurement run | ||
min = time.Hour | ||
max = time.Nanosecond | ||
sum = time.Nanosecond * 0 | ||
for i := 0; i < rounds; i++ { | ||
duration, err := establishConnectionAndCallRemote(address) | ||
if err == nil { | ||
//append(durations, duration) | ||
if min > duration { | ||
min = duration | ||
} | ||
if max < duration { | ||
max = duration | ||
} | ||
sum = sum + duration | ||
} | ||
} | ||
avg = sum / rounds | ||
fmt.Printf("Measurement run for %d rounds with new connection for every call\n", rounds) | ||
fmt.Printf("avg: %s min: %s max: %s\n", avg, min, max) | ||
|
||
//Repeat without re-establishing connection | ||
min = time.Hour | ||
max = time.Nanosecond | ||
sum = time.Nanosecond * 0 | ||
conn, err := grpc.Dial(address, grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
c := pb.NewEchoServiceClient(conn) | ||
for i := 0; i < rounds; i++ { | ||
start := time.Now() | ||
_, err = c.Echo(context.Background(), &pb.Message{Msg: "1234"}) | ||
end := time.Now() | ||
if err == nil { | ||
duration := end.Sub(start) | ||
//append(durations, duration) | ||
if min > duration { | ||
min = duration | ||
} | ||
if max < duration { | ||
max = duration | ||
} | ||
sum = sum + duration | ||
} | ||
} | ||
avg = sum / rounds | ||
fmt.Printf("Measurement run for %d rounds without re-establishing connection\n", rounds) | ||
fmt.Printf("avg: %s min: %s max: %s\n", avg, min, max) | ||
} | ||
|
||
func establishConnectionAndCallRemote(address string) (time.Duration, error) { | ||
start := time.Now() | ||
conn, err := grpc.Dial(address, grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v\n Have you tried appending port ':50051'?", err) | ||
} | ||
defer conn.Close() | ||
c := pb.NewEchoServiceClient(conn) | ||
_, err = c.Echo(context.Background(), &pb.Message{Msg: "1234"}) | ||
end := time.Now() | ||
return end.Sub(start), err | ||
} |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
context "context" | ||
"net" | ||
|
||
pb "github.com/smirnov/grpc-echo/pb" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type EchoServer struct { | ||
} | ||
|
||
//Simple echoing functionality returning inbound message as is | ||
func (s EchoServer) Echo(ctx context.Context, inbound *pb.Message) (*pb.Message, error) { | ||
return inbound, nil | ||
} | ||
|
||
func main() { | ||
echoServer := EchoServer{} | ||
listen, err := net.Listen("tcp", ":50051") | ||
if err != nil { | ||
panic(err) | ||
} | ||
grpcServer := grpc.NewServer() | ||
pb.RegisterEchoServiceServer(grpcServer, echoServer) | ||
grpcServer.Serve(listen) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
syntax = "proto3"; | ||
option go_package = "echo"; | ||
|
||
package echo; | ||
|
||
message Message { | ||
string msg = 1; | ||
} | ||
|
||
service EchoService { | ||
rpc Echo(Message) returns (Message) {} | ||
} |