Skip to content

Commit

Permalink
GRPC streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Jun 14, 2023
1 parent 9cf4101 commit 7a845d7
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ excerpt: "k6 experimental APIs"
| [timers](/javascript-api/k6-experimental/timers/) | `setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` |
| [tracing](/javascript-api/k6-experimental/tracing/) | Support for instrumenting HTTP requests with tracing information. |
| [webcrypto](/javascript-api/k6-experimental/webcrypto/) | Implements the [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API). |
| [websockets](/javascript-api/k6-experimental/websockets/) | Implements the browser's [WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). |
| [websockets](/javascript-api/k6-experimental/websockets/) | Implements the browser's [WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). |
| [grpc](/javascript-api/k6-experimental/grpc/) | Extends `k6/net/grpc` with the streaming capabilities. |
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
---
title: "k6/experimental/grpc"
title: "grpc"
excerpt: "Experimental GRPC module"
---

The `k6/experimental/grpc` module, provides a [gRPC](https://grpc.io/) client for Remote Procedure Calls (RPC) over HTTP/2.
The `k6/experimental/grpc` module is an extension of the [`k6/net/grpc`](/javascript-api/k6-net-grpc/). It provides a [gRPC](https://grpc.io/) client for Remote Procedure Calls (RPC) over HTTP/2.

The key-difference between the two modules is new `Stream` class, which provides client and server streaming support. Our long-term goal is to make this module part of k6 core, and long-term to replace the [`k6/net/grpc`](/javascript-api/k6-net-grpc/) module.

| Class/Method | Description |
|--------------|-------------|
| [Client](/javascript-api/k6-experimental/grpc/client) | gRPC client used for making RPC calls to a gRPC Server. |
| [Client.load(importPaths, ...protoFiles)](/javascript-api/k6-experimental/grpc/client/client-load) | Loads and parses the given protocol buffer definitions to be made available for RPC requests. |
| [Client.connect(address [,params])](/javascript-api/k6-experimental/grpc/client/client-connect) | Connects to a given gRPC service. |
| [Client.invoke(url, request [,params])](/javascript-api/k6-experimental/grpc/client/client-invoke) | Makes an unary RPC for the given service/method and returns a [Response](/javascript-api/k6-experimental/grpc/response). |
| [Client.invoke(url, request [,params])](/javascript-api/k6-experimental/grpc/client/client-invoke) | Makes a unary RPC for the given service/method and returns a [Response](/javascript-api/k6-experimental/grpc/response). |
| [Client.close()](/javascript-api/k6-experimental/grpc/client/client-close) | Close the connection to the gRPC service. |
| [Params](/javascript-api/k6-experimental/grpc/params) | RPC Request specific options. |
| [Response](/javascript-api/k6-experimental/grpc/response) | Returned by RPC requests. |
| [Constants](/javascript-api/k6-experimental/grpc/constants) | Define constants to distinguish between [gRPC Response](/javascript-api/k6-experimental/grpc/response) statuses. |
| [Stream](/javascript-api/k6-experimental/grpc/stream) | Creates a new GRPC stream. |
| [Stream.on](/javascript-api/k6-experimental/grpc/stream-on) | Adds a new listener to one of the possible stream event's. |
| [Stream.write](/javascript-api/k6-experimental/grpc/stream-write) | Writes a message to the stream. |
| [Stream.end](/javascript-api/k6-experimental/grpc/stream-end) | Signals to server that client finished sending. |

## gRPC metrics

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: "Stream.on()"
excerpt: 'Set up handler functions for various events on the GRPC stream.'
---

Set up handler functions for various events on the GRPC stream.

| Parameter | Type | Description |
| --------- | -------- | -------------------------------------------- |
| event | string | The event name to define a handler for. |
| handler | function | The function to call when the event happens. |

Possible events:

| Event name | Description |
| --------- | -------- |
| data | Emitted when the server sends data|
| error | Emitted when an error occurs. In case of the error, an [`Error`](/javascript-api/k6-experimental/grpc/stream/stream-error) object sends to the handler function.|
| end | Emitted when stream closes |


### Example

<div class="code-group" data-props='{"labels": ["Simple example"], "lineNumbers": [true]}'>

```javascript
import { Client, Stream } from 'k6/experimental/grpc';
import { sleep } from 'k6';

const COORD_FACTOR = 1e7;

const client = new Client();
client.load([], '../../grpc_server/route_guide.proto');

export default () => {
if (__ITER == 0) {
client.connect('127.0.0.1:10000', { plaintext: true });
}

const stream = new Stream(client, 'main.RouteGuide/RecordRoute');

// sets up a handler for the data (server sends data) event
stream.on('data', (stats) => {
console.log('Finished trip with', stats.pointCount, 'points');
console.log('Passed', stats.featureCount, 'features');
console.log('Traveled', stats.distance, 'meters');
console.log('It took', stats.elapsedTime, 'seconds');
});

// sets up a handler for the end event (stream closes)
stream.on('end', function () {
// The server has finished sending
client.close();
console.log('All done');
});

// sets up a handler for the error event (an error occurs)
stream.on('error', function (e) {
// An error has occurred and the stream has been closed.
console.log('Error: ' + JSON.stringify(e));
});

sleep(1);
};
```
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: "Error"
head_title: 'gRPC.Error'
excerpt: 'The error object of a gRPC stream.'
---

The error object is the object that is passed to the `error` event handler function.

| Name | Type | Description |
|------|------|-------------|
| `Error.code` | number | A gRPC error code. |
| `Error.details` | array | A list details attached to the error. |
| `Error.message` | string | An original error message. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: "Stream.end()"
excerpt: 'Signals to the server that the client has finished sending.'
---

Signals to the server that the client has finished sending messages.

### Example

<div class="code-group" data-props='{"labels": ["Simple example"], "lineNumbers": [true]}'>

```javascript
import { Client, Stream } from 'k6/experimental/grpc';
import { sleep } from 'k6';

const COORD_FACTOR = 1e7;

const client = new Client();
client.load([], '../../grpc_server/route_guide.proto');

export default () => {
if (__ITER == 0) {
client.connect('127.0.0.1:10000', { plaintext: true });
}

const stream = new Stream(client, 'main.RouteGuide/RecordRoute');

stream.on('data', (stats) => {
console.log('Finished trip with', stats.pointCount, 'points');
console.log('Passed', stats.featureCount, 'features');
console.log('Traveled', stats.distance, 'meters');
console.log('It took', stats.elapsedTime, 'seconds');
});

// send 2 items
stream.write({ latitude: 406109563, longitude: -742186778 });
stream.write({ latitude: 416802456, longitude: -742370183 });

// send a end signal to the server
stream.end();

sleep(1);
};
```
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: "Stream.write()"
excerpt: 'Writes a message to the stream.'
---

Writes a message to the stream.

### Example

<div class="code-group" data-props='{"labels": ["Simple example"], "lineNumbers": [true]}'>

```javascript
import { Client, Stream } from 'k6/experimental/grpc';
import { sleep } from 'k6';

const COORD_FACTOR = 1e7;

const client = new Client();
client.load([], '../../grpc_server/route_guide.proto');

export default () => {
if (__ITER == 0) {
client.connect('127.0.0.1:10000', { plaintext: true });
}

const stream = new Stream(client, 'main.RouteGuide/RecordRoute');

stream.on('data', (stats) => {
console.log('Finished trip with', stats.pointCount, 'points');
});

// send 1 items
stream.write({ latitude: 406109563, longitude: -742186778 });

// send a end signal to the server
stream.end();

sleep(1);
};
```
</div>
Loading

0 comments on commit 7a845d7

Please sign in to comment.