A simple demo for 2 useful gRPC tools: gRPCurl and ghz.
An article (in Chinese) for this demo is also available.
Some files of this demo are borrowed from "the route guide server and client" in the "gRPC-Go: The Go implementation of gRPC" repo ("gRPC-Go" for short).
Directories and files:
-
routeguide/: gRPC definition; copied from "gRPC-Go". -
server/: server code; copied from "gRPC-Go". -
server-new/: modified server code with reflection. -
mock/: mock server with grpc-mock. -
testdata.dat: 100 test data for gRPCurl. -
testdata.json: 100 test data for ghz. -
out/: generated executables.
The following software is required to run the demo:
Execute the build.sh script:
$ ./build.shYou'll obtain the generated executables in out/ directory:
$ ls -l out
total 50408
-rwxr-xr-x 1 william.yeh staff 12705500 Apr 11 14:52 server
-rwxr-xr-x 1 william.yeh staff 13102252 Apr 11 14:52 server-newRun the old server at 127.0.0.1:10000:
$ out/serverRun the new server at 127.0.0.1:20000:
$ out/server-newRun the mock server at 127.0.0.1:50051:
$ docker run -tid \
--name mock --rm -p 50051:50051 \
-v $(pwd)/routeguide:/proto \
-v $(pwd)/mock:/mock \
williamyeh/grpc-mock /mock/mock.js
The old server doesn't support gRPC reflection, and clients should have knowledge of its interface definition in order to call it.
Test the old server (127.0.0.1:10000) with gRPCurl + proto files:
$ grpcurl -plaintext -d '@' \
-import-path ./routeguide \
-proto route_guide.proto \
127.0.0.1:10000 \
routeguide.RouteGuide.RecordRoute \
< testdata.datThe mock server with grpc-mock doesn't support gRPC reflection, and clients should have knowledge of its interface definition in order to call it.
Test the mock server (127.0.0.1:50051) with gRPCurl + proto files:
$ grpcurl -plaintext \
-d '{"latitude":407838351, "longitude":-746143763}' \
-import-path ./routeguide \
-proto route_guide.proto \
127.0.0.1:50051 \
routeguide.RouteGuide.GetFeature
The new server supports gRPC reflection, and clients don't need to have full knowledge of its interface definition in order to call it.
Get interface of the new server (127.0.0.1:20000) with gRPCurl:
$ grpcurl -plaintext \
127.0.0.1:20000 \
describeTest the new server (127.0.0.1:20000) with gRPCurl:
$ grpcurl -plaintext -d '@' \
127.0.0.1:20000 \
routeguide.RouteGuide.RecordRoute \
< testdata.datBenchmark the old server (127.0.0.1:10000) with ghz + proto files for 20 seconds:
$ ghz --insecure --data=@ -z 20s \
--import-paths ./routeguide \
--proto route_guide.proto \
--call routeguide.RouteGuide.RecordRoute \
127.0.0.1:10000 \
< testdata.jsonBenchmark the new server (127.0.0.1:20000) with ghz for 20 seconds:
$ ghz --insecure --data=@ -z 20s \
--call routeguide.RouteGuide.RecordRoute \
127.0.0.1:20000 \
< testdata.json