You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sources/next/using-k6/protocols/grpc.md
+319-2Lines changed: 319 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,5 +17,322 @@ The binary format makes data transfer faster and more compact.
17
17
In the benchmarks we've seen, gRPC has proved much faster than REST, gRPC's more traditional, JSON-based counterpart.
18
18
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).
19
19
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
+
constclient=newClient();
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.
`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.
`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/).
0 commit comments