Skip to content

Commit

Permalink
[indexer-grpc] k6 loadtest
Browse files Browse the repository at this point in the history
  • Loading branch information
rustielin committed Aug 4, 2023
1 parent 479ff4e commit 8474357
Show file tree
Hide file tree
Showing 8 changed files with 2,739 additions and 0 deletions.
8 changes: 8 additions & 0 deletions testsuite/loadtest-k6/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": ["@babel/env", "@babel/typescript"],
"plugins": [
"@babel/proposal-class-properties",

"@babel/proposal-object-rest-spread"
]
}
3 changes: 3 additions & 0 deletions testsuite/loadtest-k6/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
!run_*.sh
33 changes: 33 additions & 0 deletions testsuite/loadtest-k6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Typescript k6 load test for Aptos services

See https://github.com/grafana/k6-template-typescript

## Prerequisites

- [k6](https://k6.io/docs/getting-started/installation)
- [NodeJS](https://nodejs.org/en/download/)
- [Yarn](https://yarnpkg.com/getting-started/install) (optional)

## Installation

**Install dependencies**

```bash
yarn install
```

## Running the test

To run a test written in TypeScript, we first have to transpile the TypeScript code into JavaScript and bundle the project

```bash
yarn start
```

This command creates the final test files to the `./dist` folder.

Once that is done, we can run our script the same way we usually do, for instance:

```bash
k6 run dist/${YOUR_TEST}-test.js
```
27 changes: 27 additions & 0 deletions testsuite/loadtest-k6/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "typescript",
"version": "1.0.0",
"main": "index.js",
"repository": "ssh://[email protected]/k6io/example-typescript.git",
"author": "Simon Aronsson <[email protected]>",
"license": "MIT",
"scripts": {
"start": "webpack"
},
"devDependencies": {
"@babel/core": "7.22.9",
"@babel/plugin-proposal-class-properties": "7.18.6",
"@babel/plugin-proposal-object-rest-spread": "7.20.7",
"@babel/preset-env": "7.22.9",
"@babel/preset-typescript": "7.22.5",
"@types/k6": "~0.45.0",
"@types/webpack": "5.28.1",
"babel-loader": "9.1.3",
"clean-webpack-plugin": "4.0.0-alpha.0",
"copy-webpack-plugin": "^9.0.1",
"typescript": "5.0.4",
"webpack": "5.88.2",
"webpack-cli": "5.1.4",
"webpack-glob-entries": "^1.0.1"
}
}
62 changes: 62 additions & 0 deletions testsuite/loadtest-k6/src/indexer-grpc-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { sleep } from "k6";

// see https://k6.io/docs/javascript-api/k6-experimental/grpc/stream/
import grpc from "k6/experimental/grpc";

const GRPC_ADDR = __ENV.GRPC_ADDR || "127.0.0.1:50052";
const GRPC_METHOD = "aptos.indexer.v1.RawData/GetTransactions";

// relative path from this directory to the proto files
const GRPC_IMPORT_PATHS = ["../../../crates/aptos-protos/proto"];
const GRPC_PROTOS = ["aptos/indexer/v1/raw_data.proto"];

const client = new grpc.Client();
client.load(GRPC_IMPORT_PATHS, ...GRPC_PROTOS);

export const options = {
discardResponseBodies: true,
scenarios: {
contacts: {
executor: "constant-vus",
vus: 10,
duration: "30s",
},
},
};

// GetTransactions from raw data stream
// Inspiration from: https://github.com/aptos-labs/aptos-indexer-processors/blob/main/typescript/processors/example-write-set-change-processor/processor.ts
export default () => {
if (__ITER == 0) {
client.connect(GRPC_ADDR, { plaintext: true });
}

const req = {
starting_version: 0,
};

const metadata = {
"x-aptos-data-authorization": "dummy_token",
};

const params = {
metadata,
};

const stream = new grpc.Stream(client, GRPC_METHOD, params);
stream.write(req);

// @ts-ignore // to use string event https://k6.io/docs/javascript-api/k6-experimental/grpc/stream/stream-on/
stream.on("data", (data) => {
// @ts-ignore
console.log(`Got ${data.transactions.length} transactions`);
});

// @ts-ignore // to use string event https://k6.io/docs/javascript-api/k6-experimental/grpc/stream/stream-on/
stream.on("end", () => {
client.close();
console.log("All done");
});

sleep(1);
};
26 changes: 26 additions & 0 deletions testsuite/loadtest-k6/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"moduleResolution": "node",
"module": "commonjs",
"noEmit": true,
"allowJs": true,
"removeComments": false,

"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,

"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,

"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,

"skipLibCheck": true
}
}
48 changes: 48 additions & 0 deletions testsuite/loadtest-k6/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const GlobEntries = require('webpack-glob-entries');

module.exports = {
mode: 'production',
entry: GlobEntries('./src/*test*.ts'), // Generates multiple entry for each test
output: {
path: path.join(__dirname, 'dist'),
libraryTarget: 'commonjs',
filename: '[name].js',
},
resolve: {
extensions: ['.ts', '.js'],
},
module: {
rules: [
{
test: /\.ts$/,
use: 'babel-loader',
exclude: /node_modules/,
},
],
},
target: 'web',
externals: /^(k6|https?\:\/\/)(\/.*)?/,
// Generate map files for compiled scripts
devtool: "source-map",
stats: {
colors: true,
},
plugins: [
new CleanWebpackPlugin(),
// Copy assets to the destination folder
// see `src/post-file-test.ts` for an test example using an asset
new CopyPlugin({
patterns: [{
from: path.resolve(__dirname, 'assets'),
noErrorOnMissing: true
}],
}),
],
optimization: {
// Don't minimize, as it's not used in the browser
minimize: false,
},
};
Loading

0 comments on commit 8474357

Please sign in to comment.