Skip to content
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

Add self-contained setup for load test #275

Merged
merged 5 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
133 changes: 122 additions & 11 deletions devenv/loadtest/modules/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
import http from "k6/http";

export const DatasourcesEndpoint = class DatasourcesEndpoint {
constructor(httpClient) {
this.httpClient = httpClient;
}

getAll() {
return this.httpClient.get('/datasources');
}

getById(id) {
return this.httpClient.get(`/datasources/${id}`);
}

getByName(name) {
return this.httpClient.get(`/datasources/name/${name}`);
}

create(payload) {
return this.httpClient.post(`/datasources`, JSON.stringify(payload));
}

update(id, payload) {
return this.httpClient.put(`/datasources/${id}`, JSON.stringify(payload));
}

delete(id) {
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
return this.httpClient.delete(`/datasources/${id}`);
}
};

export const DashboardsEndpoint = class DashboardsEndpoint {
constructor(httpClient) {
this.httpClient = httpClient;
}

getAll() {
return this.httpClient.get('/dashboards');
}

upsert(payload) {
return this.httpClient.post(`/dashboards/db`, JSON.stringify(payload));
}

delete(id) {
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
return this.httpClient.delete(`/dashboards/uid/${id}`);
}
};

export const OrganizationsEndpoint = class OrganizationsEndpoint {
constructor(httpClient) {
this.httpClient = httpClient;
}

getById(id) {
return this.httpClient.get(`/orgs/${id}`);
}

getByName(name) {
return this.httpClient.get(`/orgs/name/${name}`);
}

create(name) {
let payload = {
name: name,
};
return this.httpClient.post(`/orgs`, JSON.stringify(payload));
}

delete(id) {
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
return this.httpClient.delete(`/orgs/${id}`);
}
};

export const UIEndpoint = class UIEndpoint {
constructor(httpClient) {
this.httpClient = httpClient;
Expand All @@ -10,32 +83,53 @@ export const UIEndpoint = class UIEndpoint {
return this.httpClient.formPost('/login', payload);
}

render() {
return this.httpClient.get('/render/d-solo/_CPokraWz/graph-panel?orgId=1&panelId=1&width=1000&height=500&tz=Europe%2FStockholm')
renderPanel(orgId, dashboardUid, panelId) {
return this.httpClient.get(
`/render/d-solo/${dashboardUid}/graph-panel`,
{
orgId,
panelId,
width: 1000,
height: 500,
tz: 'Europe/Stockholm',
}
);
}
}

export const GrafanaClient = class GrafanaClient {
constructor(httpClient) {
httpClient.onBeforeRequest = this.onBeforeRequest;
httpClient.onBeforeRequest = (params) => {
if (this.orgId && this.orgId > 0) {
params.headers = params.headers || {};
params.headers["X-Grafana-Org-Id"] = this.orgId;
}
}

this.raw = httpClient;
this.dashboards = new DashboardsEndpoint(httpClient.withUrl('/api'));
this.datasources = new DatasourcesEndpoint(httpClient.withUrl('/api'));
this.orgs = new OrganizationsEndpoint(httpClient.withUrl('/api'));
this.ui = new UIEndpoint(httpClient);
}

loadCookies(cookies) {
for (let [name, value] of Object.entries(cookies)) {
http.cookieJar().set(this.raw.url, name, value);
}
}

saveCookies() {
return http.cookieJar().cookiesForURL(this.raw.url + '/');
}

batch(requests) {
return this.raw.batch(requests);
}

withOrgId(orgId) {
this.orgId = orgId;
}

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

export const BaseClient = class BaseClient {
Expand All @@ -62,10 +156,17 @@ export const BaseClient = class BaseClient {

}

get(url, params) {
get(url, queryParams, params) {
params = params || {};
this.beforeRequest(params);
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
this.onBeforeRequest(params);

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

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

Expand All @@ -86,6 +187,16 @@ export const BaseClient = class BaseClient {
return http.post(this.url + url, body, params);
}

put(url, body, params) {
params = params || {};
params.headers = params.headers || {};
params.headers['Content-Type'] = 'application/json';

this.beforeRequest(params);
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
this.onBeforeRequest(params);
return http.put(this.url + url, body, params);
}

delete(url, params) {
params = params || {};
this.beforeRequest(params);
Expand Down
65 changes: 45 additions & 20 deletions devenv/loadtest/render_test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
import { check, group } from 'k6';
import { check, fail, group } from 'k6';
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
import { createClient } from './modules/client.js';
import {
createTestOrgIfNotExists,
upsertTestdataDatasource,
upsertTestdataDashboard,
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
} from './modules/util.js';
pianohacker marked this conversation as resolved.
Show resolved Hide resolved

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

let endpoint = __ENV.URL || 'http://localhost:3000';
const client = createClient(endpoint);
const dashboard = JSON.parse(open('fixtures/image-renderer-perf-dashboard.json'));
pianohacker marked this conversation as resolved.
Show resolved Hide resolved

export default () => {
group("render test", () => {
if (__ITER === 0) {
group("user authenticates thru ui with username and password", () => {
let res = client.ui.login('admin', 'admin');
export const setup = () => {
let grafanaSession;
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
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,
});
});
}

if (__ITER !== 0) {
group("render graph panel", () => {
const response = client.ui.render();
check(response, {
'response status is 200': (r) => r.status === 200,
});
check(res, {
'response status is 200': (r) => r.status === 200,
});
});

const orgId = createTestOrgIfNotExists(client);
client.withOrgId(orgId);
upsertTestdataDatasource(client);
upsertTestdataDashboard(client, dashboard);
pianohacker marked this conversation as resolved.
Show resolved Hide resolved

return {
orgId,
cookies: client.saveCookies(),
};
};

import http from 'k6/http';
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
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,
);
check(response, {
'response status is 200': (r) => r.status === 200,
'response is a PNG': (r) => r.headers['Content-Type'] == 'image/png',
});
}
});
});
}

Expand Down
16 changes: 10 additions & 6 deletions devenv/loadtest/run.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#/bin/bash

PWD=$(pwd)
cd "$(dirname $0)"

run() {
duration='15m'
url='http://localhost:3000'
vus='2'
local duration='15m'
local url='http://localhost:3000'
local vus='2'
local iterationsOption=''
AgnesToulet marked this conversation as resolved.
Show resolved Hide resolved

while getopts ":d:u:v:" o; do
while getopts ":d:i:u:v:" o; do
case "${o}" in
d)
duration=${OPTARG}
;;
i)
iterationsOption="--iterations ${OPTARG}"
pianohacker marked this conversation as resolved.
Show resolved Hide resolved
AgnesToulet marked this conversation as resolved.
Show resolved Hide resolved
;;
u)
url=${OPTARG}
;;
Expand All @@ -22,7 +26,7 @@ run() {
done
shift $((OPTIND-1))

docker run -t --network=host -v $PWD:/src -e URL=$url --rm -i loadimpact/k6:master run --vus $vus --duration $duration src/render_test.js
docker run -t --network=host -v $PWD:/src -e URL=$url --rm -i loadimpact/k6:master run --vus $vus --duration $duration $iterationsOption /src/render_test.js
}

run "$@"