Skip to content

Load tests: Improve scripts #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions devenv/docker/custom-config/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
version: '2'

services:
grafana:
image: grafana/grafana:latest
ports:
- 3000
- 3000:3000
environment:
GF_RENDERING_SERVER_URL: http://renderer:8081/render
GF_RENDERING_CALLBACK_URL: http://grafana:3000/
Expand Down
4 changes: 1 addition & 3 deletions devenv/docker/simple/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
version: '2'

services:
grafana:
image: grafana/grafana:latest
ports:
- 3000
- 3000:3000
environment:
GF_RENDERING_SERVER_URL: http://renderer:8081/render
GF_RENDERING_CALLBACK_URL: http://grafana:3000/
Expand Down
10 changes: 9 additions & 1 deletion devenv/loadtest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Runs load tests and checks using [k6](https://k6.io/).

## Prerequisites

Docker
- Docker
- Grafana instance running (you can use the environment in `devenv/docker/simple`)

## Run

Expand All @@ -24,6 +25,7 @@ Run only 1 iteration of the load test (useful for testing):

```bash
$ ./run.sh -i 1
```

Run load test for custom target url:

Expand All @@ -37,6 +39,12 @@ Run load test for 10 virtual users:
$ ./run.sh -v 10
```

Run load test using a service account token instead of default username/password:

```bash
$ ./run.sh -a glsa_xxxx
```

Example output:

```bash
Expand Down
66 changes: 33 additions & 33 deletions devenv/loadtest/modules/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import http from "k6/http";
import http from 'k6/http';

export const DatasourcesEndpoint = class DatasourcesEndpoint {
constructor(httpClient) {
Expand Down Expand Up @@ -68,31 +68,34 @@ export const UIEndpoint = class UIEndpoint {

login(username, pwd) {
const payload = { user: username, password: pwd };
return this.httpClient.formPost('/login', payload);
return this.httpClient.post('/login', JSON.stringify(payload));
}

renderPanel(orgId, dashboardUid, panelId) {
return this.httpClient.get(
`/render/d-solo/${dashboardUid}/graph-panel`,
{
orgId,
panelId,
width: 1000,
height: 500,
tz: 'Europe/Stockholm',
}
);
return this.httpClient.get(`/render/d-solo/${dashboardUid}/graph-panel`, {
orgId,
panelId: `panel-${panelId}`,
width: 1000,
height: 500,
tz: 'Europe/Stockholm',
timeout: '10',
});
}
}
};

export const GrafanaClient = class GrafanaClient {
constructor(httpClient) {
httpClient.onBeforeRequest = (params) => {
params.headers = params.headers || {};

if (this.orgId && this.orgId > 0) {
params.headers = params.headers || {};
params.headers["X-Grafana-Org-Id"] = this.orgId;
params.headers['X-Grafana-Org-Id'] = this.orgId;
}
}

if (this.authToken) {
params.headers['Authorization'] = `Bearer ${this.authToken}`;
}
};

this.raw = httpClient;
this.dashboards = new DashboardsEndpoint(httpClient.withUrl('/api'));
Expand All @@ -118,7 +121,11 @@ export const GrafanaClient = class GrafanaClient {
withOrgId(orgId) {
this.orgId = orgId;
}
}

withAuthToken(authToken) {
this.authToken = authToken;
}
};

export const BaseClient = class BaseClient {
constructor(url, subUrl) {
Expand All @@ -135,35 +142,28 @@ export const BaseClient = class BaseClient {
}

withUrl(subUrl) {
let c = new BaseClient(this.url, subUrl);
let c = new BaseClient(this.url, subUrl);
c.onBeforeRequest = this.onBeforeRequest;
return c;
}

beforeRequest(params) {

}
beforeRequest(params) {}

get(url, queryParams, params) {
params = params || {};
this.onBeforeRequest(params);

if (queryParams) {
url += '?' + Array.from(Object.entries(queryParams)).map(([key, value]) =>
`${key}=${encodeURIComponent(value)}`
).join('&');
url +=
'?' +
Array.from(Object.entries(queryParams))
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join('&');
}

return http.get(this.url + url, params);
}

formPost(url, body, params) {
params = params || {};
this.beforeRequest(params);
this.onBeforeRequest(params);
return http.post(this.url + url, body, params);
}

post(url, body, params) {
params = params || {};
params.headers = params.headers || {};
Expand Down Expand Up @@ -206,8 +206,8 @@ export const BaseClient = class BaseClient {

return http.batch(requests);
}
}
};

export const createClient = (url) => {
return new GrafanaClient(new BaseClient(url, ''));
}
};
41 changes: 21 additions & 20 deletions devenv/loadtest/render_test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { check, group } from 'k6';
import { createClient } from './modules/client.js';
import {
createTestOrgIfNotExists,
upsertTestdataDatasource,
upsertDashboard,
} from './modules/util.js';
import { createTestOrgIfNotExists, upsertTestdataDatasource, upsertDashboard } from './modules/util.js';

export let options = {
noCookiesReset: true,
thresholds: { checks: [ { threshold: 'rate=1', abortOnFail: true } ] },
};

let endpoint = __ENV.URL || 'http://localhost:3000';
let authToken = __ENV.AUTH_TOKEN || '';

const client = createClient(endpoint);
const dashboard = JSON.parse(open('fixtures/graph_panel.json'));

export const setup = () => {
group("user authenticates thru ui with username and password", () => {
let res = client.ui.login('admin', 'admin');
if (!authToken) {
group('user authenticates thru ui with username and password', () => {
let res = client.ui.login('admin', 'admin');

check(res, {
'response status is 200': (r) => r.status === 200,
check(res, {
'response status is 200': (r) => r.status === 200,
});
});
});
} else {
client.withAuthToken(authToken);
}

const orgId = createTestOrgIfNotExists(client);
client.withOrgId(orgId);
Expand All @@ -39,19 +40,19 @@ export default (data) => {
client.loadCookies(data.cookies);
client.withOrgId(data.orgId);

group("render test", () => {
group("render graph panel", () => {
const response = client.ui.renderPanel(
data.orgId,
dashboard.uid,
dashboard.panels[0].id,
);
if (authToken) {
client.withAuthToken(authToken);
}

group('render test', () => {
group('render graph panel', () => {
const response = client.ui.renderPanel(data.orgId, dashboard.uid, dashboard.panels[0].id);
check(response, {
'response status is 200': (r) => r.status === 200,
'response is a PNG': (r) => r.headers['Content-Type'] == 'image/png',
});
});
});
}
};

export const teardown = () => {}
export const teardown = () => {};
9 changes: 7 additions & 2 deletions devenv/loadtest/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ cd "$(dirname $0)"
run() {
local duration='15m'
local url='http://localhost:3000'
local authToken=''
local vus='2'
local iterationsOption=''

while getopts ":d:i:u:v:" o; do
while getopts ":d:i:u:v:a:" o; do
case "${o}" in
d)
duration=${OPTARG}
Expand All @@ -22,6 +23,9 @@ run() {
v)
vus=${OPTARG}
;;
a)
authToken=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
Expand All @@ -31,8 +35,9 @@ run() {
--network=host \
--mount type=bind,source=$PWD,destination=/src \
-e URL=$url \
-e AUTH_TOKEN=$authToken \
--rm \
loadimpact/k6:master run \
grafana/k6:master run \
--vus $vus \
--duration $duration \
$iterationsOption \
Expand Down