Skip to content

Commit 7db83cd

Browse files
gRPC protocol updates (#1480)
* gRPC protocol updates --------- Co-authored-by: Heitor Tashiro Sergent <[email protected]>
1 parent 7a7d9b8 commit 7db83cd

File tree

1 file changed

+319
-2
lines changed
  • docs/sources/next/using-k6/protocols

1 file changed

+319
-2
lines changed

docs/sources/next/using-k6/protocols/grpc.md

Lines changed: 319 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,322 @@ The binary format makes data transfer faster and more compact.
1717
In the benchmarks we've seen, gRPC has proved much faster than REST, gRPC's more traditional, JSON-based counterpart.
1818
The messages and services used for gRPC are described in `.proto` files, containing definitions for [Protocol Buffers](https://en.wikipedia.org/wiki/Protocol_Buffers) (protobuf).
1919

20-
k6 also supports unary gRPC requests to the [k6/net/grpc](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc) built-in module.
21-
For further information, read [our tutorial about performance testing gRPC services](https://k6.io/blog/performance-testing-grpc-services/).
20+
## Load testing gRPC services with k6
21+
22+
Starting on k6 v0.49.0, k6 supports unary gRPC requests and streaming as part of the `k6/net/grpc` core module.
23+
24+
### gRPC definitions
25+
26+
Before interacting with a gRPC service, k6 needs to learn the definitions of the messages and services.
27+
28+
One way to do that is to explicitly use the `Client.load()` method and load the client definitions from the local file system. The method accepts a list of import paths and a list of `.proto` files. k6 then loads all the definitions from the files and their dependencies.
29+
30+
{{< code >}}
31+
32+
```javascript
33+
import { Client } from 'k6/net/grpc';
34+
35+
const client = new Client();
36+
client.load(['definitions'], 'hello.proto');
37+
```
38+
39+
{{< /code >}}
40+
41+
Alternatively, you can dynamically load the definitions by using the gRPC reflection protocol. To enable reflection, you can pass the `reflect: true` option to `Client.connect()`. k6 then loads all the definitions from the server and their dependencies.
42+
43+
This option is only possible if the server has been instrumented with reflection support.
44+
45+
{{< code >}}
46+
47+
```javascript
48+
import { Client } from 'k6/net/grpc';
49+
50+
const client = new Client();
51+
client.connect('127.0.0.1:10000', { reflect: true });
52+
```
53+
54+
{{< /code >}}
55+
56+
### Unary gRPC requests
57+
58+
Unary calls work the same way as regular HTTP requests. A single request is sent to a server, and the server replies with a single response.
59+
60+
{{< code >}}
61+
62+
```javascript
63+
import { Client, StatusOK } from 'k6/net/grpc';
64+
import { check, sleep } from 'k6';
65+
66+
const client = new Client();
67+
client.load(['definitions'], 'hello.proto');
68+
69+
export default () => {
70+
client.connect('127.0.0.1:10000', {});
71+
72+
const data = { greeting: 'Bert' };
73+
const response = client.invoke('hello.HelloService/SayHello', data);
74+
75+
check(response, {
76+
'status is OK': (r) => r && r.status === StatusOK,
77+
});
78+
79+
console.log(JSON.stringify(response.message));
80+
81+
client.close();
82+
sleep(1);
83+
};
84+
```
85+
86+
{{< /code >}}
87+
88+
### Server gRPC streaming
89+
90+
In server streaming mode, the client sends a single request to the server, and the server replies with multiple responses.
91+
92+
The example below demonstrates client streaming.
93+
94+
{{< code >}}
95+
96+
```javascript
97+
import { Client, Stream } from 'k6/net/grpc';
98+
import { sleep } from 'k6';
99+
100+
const COORD_FACTOR = 1e7;
101+
102+
const client = new Client();
103+
104+
export default () => {
105+
if (__ITER == 0) {
106+
client.connect('127.0.0.1:10000', { plaintext: true, reflect: true });
107+
}
108+
109+
const stream = new Stream(client, 'main.FeatureExplorer/ListFeatures', null);
110+
111+
stream.on('data', function (feature) {
112+
console.log(
113+
`Found feature called "${feature.name}" at ${feature.location.latitude / COORD_FACTOR}, ${
114+
feature.location.longitude / COORD_FACTOR
115+
}`
116+
);
117+
});
118+
119+
stream.on('end', function () {
120+
// The server has finished sending
121+
client.close();
122+
console.log('All done');
123+
});
124+
125+
// send a message to the server
126+
stream.write({
127+
lo: {
128+
latitude: 400000000,
129+
longitude: -750000000,
130+
},
131+
hi: {
132+
latitude: 420000000,
133+
longitude: -730000000,
134+
},
135+
});
136+
137+
sleep(0.5);
138+
};
139+
```
140+
141+
{{< /code >}}
142+
143+
In the example script, k6 connects to a gRPC server, creates a stream, and sends a message to the server with latitude and longitude coordinates. When the server sends data back, it logs the feature name and its location. When the server finishes sending data, it closes the client connection and logs a completion message.
144+
145+
### Client gRPC streaming
146+
147+
The client streaming mode is the opposite of the server streaming mode. The client sends multiple requests to the server, and the server replies with a single response.
148+
149+
The example below demonstrates client streaming.
150+
151+
{{< code >}}
152+
153+
```javascript
154+
import { Client, Stream } from 'k6/net/grpc';
155+
import { sleep } from 'k6';
156+
157+
const COORD_FACTOR = 1e7;
158+
const client = new Client();
159+
160+
// a sample points collection
161+
const points = [
162+
{
163+
location: { latitude: 407838351, longitude: -746143763 },
164+
name: 'Patriots Path, Mendham, NJ 07945, USA',
165+
},
166+
{
167+
location: { latitude: 408122808, longitude: -743999179 },
168+
name: '101 New Jersey 10, Whippany, NJ 07981, USA',
169+
},
170+
{
171+
location: { latitude: 413628156, longitude: -749015468 },
172+
name: 'U.S. 6, Shohola, PA 18458, USA',
173+
},
174+
{
175+
location: { latitude: 419999544, longitude: -740371136 },
176+
name: '5 Conners Road, Kingston, NY 12401, USA',
177+
},
178+
{
179+
location: { latitude: 414008389, longitude: -743951297 },
180+
name: 'Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA',
181+
},
182+
];
183+
184+
export default () => {
185+
if (__ITER == 0) {
186+
client.connect('127.0.0.1:10000', { plaintext: true, reflect: true });
187+
}
188+
189+
const stream = new Stream(client, 'main.RouteGuide/RecordRoute');
190+
191+
stream.on('data', (stats) => {
192+
console.log(`Finished trip with ${stats.pointCount} points`);
193+
console.log(`Passed ${stats.featureCount} features`);
194+
console.log(`Travelled ${stats.distance} meters`);
195+
console.log(`It took ${stats.elapsedTime} seconds`);
196+
});
197+
198+
stream.on('end', () => {
199+
client.close();
200+
console.log('All done');
201+
});
202+
203+
// send 3 random points
204+
for (let i = 0; i < 3; i++) {
205+
const point = points[Math.floor(Math.random() * points.length)];
206+
pointSender(stream, point);
207+
}
208+
209+
// close the client stream
210+
stream.end();
211+
};
212+
213+
const pointSender = (stream, point) => {
214+
console.log(
215+
`Visiting point ${point.name} ${point.location.latitude / COORD_FACTOR}, ${
216+
point.location.longitude / COORD_FACTOR
217+
}`
218+
);
219+
220+
// send the location to the server
221+
stream.write(point.location);
222+
223+
sleep(0.5);
224+
};
225+
```
226+
227+
{{< /code >}}
228+
229+
In the example script, k6 establishes a connection to a gRPC server, creates a stream, and sends three random points. The server responds with statistics about the trip, which are logged to the console. The code also handles the end of the stream, closing the client and logging a completion message.
230+
231+
### Bidirectional gRPC streaming
232+
233+
In bi-directional streaming mode, the client and the server may send multiple messages.
234+
235+
From the API perspective, it combines the client and server streaming modes, so the code is similar to the examples above.
236+
237+
### Streaming error handling
238+
239+
To catch errors that occur during streaming, you can use the `error` event handler.
240+
241+
The handler receives [an error object](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-net-grpc/stream/stream-error/).
242+
243+
{{< code >}}
244+
245+
```javascript
246+
import { Client, Stream } from 'k6/net/grpc';
247+
248+
const client = new Client();
249+
const stream = new Stream(client, 'main.RouteGuide/RecordRoute');
250+
251+
stream.on('error', function (e) {
252+
// An error has occurred and the stream has been closed.
253+
console.log('Error: ' + JSON.stringify(e));
254+
});
255+
```
256+
257+
{{< /code >}}
258+
259+
### Handle special cases in message Marshaling/Unmarshaling
260+
261+
k6 uses the [protojson](https://pkg.go.dev/google.golang.org/protobuf/encoding/protojson) package for encoding and decoding of messages.
262+
263+
Certain gRPC well-known types or wrappers have specific marshaling/unmarshaling rules.
264+
265+
For instance, if you import `"google/protobuf/wrappers.proto"` and your proto-definitions look like this:
266+
267+
```proto
268+
syntax = "proto3";
269+
270+
package testing;
271+
272+
import "google/protobuf/wrappers.proto";
273+
274+
service Service {
275+
rpc SayHey(google.protobuf.StringValue) returns (google.protobuf.StringValue);
276+
rpc DoubleInteger(google.protobuf.Int64Value) returns (google.protobuf.Int64Value);
277+
}
278+
279+
```
280+
281+
When passing a message, you should use a string or an integer, not an object. As a result, you will receive a type that has already been marshaled.
282+
283+
{{< code >}}
284+
285+
```javascript
286+
import { Client } from 'k6/net/grpc';
287+
const client = new Client();
288+
289+
// an example of passing a string
290+
const respString = client.invoke('testing.Service/SayHey', 'John');
291+
if (respString.message !== 'hey John') {
292+
throw new Error("expected to get 'hey John', but got a " + respString.message);
293+
}
294+
295+
// an example of passing an integer
296+
const respInt = client.invoke('testing.Service/DoubleInteger', '3');
297+
if (respInt.message !== '6') {
298+
throw new Error("expected to get '6', but got a " + respInt.message);
299+
}
300+
```
301+
302+
{{< /code >}}
303+
304+
Another special case could be usage of `oneof`. Let's say you have a proto-definition like this:
305+
306+
```proto
307+
syntax = "proto3";
308+
309+
package testing;
310+
311+
service Service {
312+
rpc Test(Foo) returns (Foo) {}
313+
}
314+
315+
message Foo {
316+
oneof Bar {
317+
string code = 1;
318+
uint32 id = 2;
319+
}
320+
}
321+
```
322+
323+
In this case, you should pass an object either with `code` or `id` fields.
324+
325+
{{< code >}}
326+
327+
```javascript
328+
import { Client } from 'k6/net/grpc';
329+
const client = new Client();
330+
331+
// calling RPC with filled code field
332+
const respWithCode = client.invoke('testing.Service/Test', { code: 'abc-123' });
333+
334+
// calling RPC with filled id field
335+
const respWithID = client.invoke('testing.Service/Test', { id: 123 });
336+
```
337+
338+
{{< /code >}}

0 commit comments

Comments
 (0)