Skip to content

Commit

Permalink
Initial PoC commit with minimally working echo grpc server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
smirnov committed Jun 16, 2019
0 parents commit ed77b45
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 0 deletions.
114 changes: 114 additions & 0 deletions grpc-client.go
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
}
28 changes: 28 additions & 0 deletions grpc-server.go
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)
}
161 changes: 161 additions & 0 deletions pb/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pb/service.proto
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) {}
}

0 comments on commit ed77b45

Please sign in to comment.