Skip to content

Commit

Permalink
feat(examples): add rpc example with twirp
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardobridge committed Jul 3, 2024
1 parent 9f74020 commit 27409aa
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/rpc-twirp-with-custom-function/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Load testing an RPC service created with Twirp

[Twirp](https://github.com/twitchtv/twirp) is a simple RPC framework for service-to-service communication. In the following example, we will use the clients auto-generated from [Twirpscript](https://github.com/tatethurston/TwirpScript), a NodeJS implementation of Twirp, to load test a server using Twirp.

While we could build [a dedicated engine](https://www.artillery.io/blog/extend-artillery-by-creating-your-own-engines), you can also use custom functions and leverage the existing default engine. This example shows you how to do that.

## Pre-requisites

- Protobuf [installed](https://github.com/tatethurston/TwirpScript?tab=readme-ov-file#installation-)

## How the example works

This example imports the auto-generated `.pb.js` file into a processor, and calls the `MakeHat` client method, which will call the server. We also emit [custom metrics](https://www.artillery.io/docs/reference/extension-apis#custom-metrics-api) from the function to track the number of requests and responses, as well as the time taken in the RPC call.

```
twirp.requests: ................................................................ 300
twirp.response_time:
min: ......................................................................... 1.1
max: ......................................................................... 60.7
mean: ........................................................................ 5.4
median: ...................................................................... 2.2
p95: ......................................................................... 22.4
p99: ......................................................................... 55.2
twirp.responses: ............................................................... 300
twirp.responses.success: ....................................................... 300
```

## Running the Twirp server

First, install the dependencies:
```
cd twirp && npm install
```

Then, start the server with `npm start`

## Running Artillery test

Once the server is up and running, execute the test script:

```
npx artillery run ./test/scenario.yml
```
30 changes: 30 additions & 0 deletions examples/rpc-twirp-with-custom-function/test/processor.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { MakeHat } from '../twirp/protos/haberdasher.pb.js';
import { client } from 'twirpscript';

client.baseURL = 'http://localhost:8080';

function recordMetrics(startedAt, ee, error) {
//you can add more domain specific metrics here dependant on the response
ee.emit('counter', 'twirp.requests', 1);
ee.emit('counter', 'twirp.responses', 1);
if (error) {
ee.emit('counter', 'twirp.responses.error', 1);
ee.emit('counter', 'twirp.codes.' + error.code, 1);
} else {
ee.emit('counter', 'twirp.responses.success', 1);
}

const took = Number(process.hrtime.bigint() - startedAt) / 1e6;
ee.emit('histogram', 'twirp.response_time', took);
}

export async function callRpcServer(context, ee, next) {
const startedAt = process.hrtime.bigint();
try {
const res = await MakeHat({ inches: 15, potato: true });
console.log(res);
recordMetrics(startedAt, ee);
} catch (error) {
recordMetrics(startedAt, ee, error);
}
}
11 changes: 11 additions & 0 deletions examples/rpc-twirp-with-custom-function/test/scenario.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
config:
target: "http://localhost:8080"
phases:
- duration: 10
arrivalRate: 30
name: "Phase 1"
processor: "./processor.mjs"

scenarios:
- flow:
- function: "callRpcServer"
12 changes: 12 additions & 0 deletions examples/rpc-twirp-with-custom-function/twirp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "twirp",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"start": "node ./server/index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Loading

0 comments on commit 27409aa

Please sign in to comment.