Skip to content

Instantly share code, notes, and snippets.

@gspncr
Last active December 28, 2022 16:02
Show Gist options
  • Save gspncr/0099e745cc10c923b3d77e2f1d2dd2d6 to your computer and use it in GitHub Desktop.
Save gspncr/0099e745cc10c923b3d77e2f1d2dd2d6 to your computer and use it in GitHub Desktop.
new relic x k6 test
import http from 'k6/http';
export class NewRel {
constructor(apiKey, log = false) {
this.log = log;
this.params = {
headers: {
'Content-Type': 'application/json',
'API-Key': apiKey,
},
};
this.urls = {
graphql: 'https://api.newrelic.com/graphql',
};
}
PrintAlertingStatus() {
let payload = JSON.stringify({
query: `
{
actor {
entitySearch(query: "name LIKE 'Node Workshop' AND domain IN ('APM')") {
results {
entities {
... on ApmApplicationEntityOutline {
alertSeverity
}
}
}
}
}
}
`,
});
let res = http.post(this.urls.graphql, payload, this.params);
if (this.log) {
console.log('New Relic Check HTTP Status is: ' + res.status);
}
if (res.status === 200) {
let body = JSON.parse(res.body);
var result = body.data.actor.entitySearch.results.entities[0].alertSeverity;
console.log('New Relic Status: ' + result);
}
}
AppID() {
// From NerdGraph, copy the GraphQL payload from tools > copy as cURL > take the entire {"query"} section
let payload = JSON.stringify({
query: `
{
actor {
entitySearch(query: "name LIKE 'Node Workshop' AND domain IN ('APM')") {
results {
entities {
... on ApmApplicationEntityOutline {
applicationId
}
}
}
}
}
}
`
});
let res = http.post(this.urls.graphql, payload, this.params);
// Check we are not experiencing HTTP 400. If you are, the payload is likely wrong.
if (this.log) {
console.log('New Relic Check HTTP Status is: ' + res.status);
}
if (res.status === 200) {
let body = JSON.parse(res.body);
/* result will depend on the query. This query is built on alertSeverity result.
You need to modify the selector if you are performing a different query */
var result = JSON.stringify(
body.data.actor.entitySearch.results.entities[0].applicationId
);
} else {
throw new Error('Could not fetch AppID from New Relic')
}
return result;
}
//Send a deployment marker with start/end information on load test.
Notify(testName, state, description, user) {
var url =
'https://api.newrelic.com/v2/applications/' + this.AppID() + '/deployments.json';
console.log(url);
// From NerdGraph, copy the GraphQL payload from tools > copy as cURL > take the entire {"query"} section
let payload = JSON.stringify({
deployment: {
revision: testName,
changelog: 'k6 load test ' + state,
description: description,
user: user,
},
});
let res = http.post(url, payload, this.params);
// Check we are not experiencing HTTP 400. If you are, the payload is likely wrong.
if (![200, 201].includes(res.status)) {
throw new Error(`Could not notify New Relic about test state (res: ${res.status})`)
}
return JSON.stringify(res.status);
}
}
import { check, sleep } from 'k6';
import { NewRel } from './util/newrelic.js'
import http from 'k6/http';
const apiKey = '<New Relic API Key>'
const nr = new NewRel(apiKey);
export const options = {
vus: 10,
duration: '10s',
tags: {
testName: 'Tuesdays are for testing',
user: 'nerd@newrelic.com'
}
}
export function setup() {
nr.PrintAlertingStatus();
nr.Notify(
'Sunday load test - short',
'START',
'Beginning E2E load test script',
'gspencer@newrelic.com',
);
}
export default function () {
execFrontPage();
execMathUrl('http://nodeworkshop.eu-west-2.elasticbeanstalk.com/badmaths')
execMathUrl('http://nodeworkshop.eu-west-2.elasticbeanstalk.com/maths')
execMathUrl('http://nodeworkshop.eu-west-2.elasticbeanstalk.com/weirdmaths');
sleep(1);
}
function execFrontPage() {
const res = http.get(
'http://nodeworkshop.eu-west-2.elasticbeanstalk.com',
{ type: 'part-of-test' },
);
check(res, {
'was succesful': (r) => r.status == 200,
'contains the header': (r) => r.body.includes('New Relic Node Workshop')
})
}
function execMathUrl(url) {
const res = http.get(url, { type: 'part-of-test' });
return check(res, {
'was succesful': (r) => r.status === 200,
'has body': (r) => r.body && r.body != '',
'returns 10': (r) => parseInt(JSON.parse(r.body).x, 10) === 10
})
}
export function teardown(data) {
nr.Notify(
'Sunday load test - short',
'END',
'Finishing E2E load test script',
'gspencer@newrelic.com'
);
nr.PrintAlertingStatus();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment