Skip to content

Commit

Permalink
sample tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankatliarchuk committed May 24, 2020
1 parent 06ca8ee commit 913b2d9
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .chglog/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ style: github
template: CHANGELOG.tpl.md
info:
title: CHANGELOG
repository_url: https://github.com/ik-performance/performance-blueprint-project.git
repository_url: https://github.com/ik-performance/k6-blueprint.git
options:
header:
pattern: "^(.*)$"
pattern_maps:
- Subject
- Subject
1 change: 1 addition & 0 deletions Brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
brew "k6"
13 changes: 0 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
<a name="unreleased"></a>
## [Unreleased]



<a name="v0.1.0"></a>
## v0.1.0 - 2020-05-24

- initial version
- Initial commit


[Unreleased]: https://github.com/ik-performance/performance-blueprint-project.git/compare/v0.1.0...HEAD
22 changes: 18 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
.PHONY: pre-commit
SHELL = /bin/bash -o pipefail
DOCKER_IMAGE ?= gatling:local
PROJECT_PATH ?= $(shell 'pwd')

DOCKER_IMAGE ?= loadimpact/k6:0.26.2
K6_NO_CONNECTION_REUSE ?= true
K6_USER_AGENT ?= "MyK6UserAgentString/1.0"

help:
@grep -E '^[/\a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort -u | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@grep -E '^[/\a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

init: ## Commit hooks setup
@pre-commit install
Expand All @@ -17,5 +20,16 @@ validate: ## Validate with pre-commit hooks
change: ## Update changelog
git-chglog -o CHANGELOG.md --next-tag `semtag final -s minor -o`

choose/simulation: ## Show all simulations
@echo "list simulations"
docker/inspect: ## Inspect container
@docker run --rm -it \
--entrypoint /bin/sh \
$(DOCKER_IMAGE) /bin/sh

run/simulation: ## Gatling run scenario 'make sim=k6'
@k6 run ./simulations/json.js

choose/simulation: ## Show all simulations `make choose/simulation sim=initial`
@docker run --rm -i \
-v ${PROJECT_PATH}/simulations/:/opt/ \
$(DOCKER_IMAGE) \
run - <./simulations/$(sim).js
41 changes: 25 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
# performance-blueprint-project
# k6-blueprint

Blueprint performance project

[![](https://img.shields.io/github/license/ik-performance/performance-blueprint-project)](https://github.com/ik-performance/performance-blueprint-project)
![](https://img.shields.io/github/v/tag/ik-performance/performance-blueprint-project)
![](https://img.shields.io/issues/github/ik-performance/performance-blueprint-project)
![](https://img.shields.io/github/issues/ik-performance/performance-blueprint-project)
![](https://img.shields.io/github/issues-closed/ik-performance/performance-blueprint-project)
[![](https://img.shields.io/github/languages/code-size/ik-performance/performance-blueprint-project)](https://github.com/ik-performance/performance-blueprint-project)
[![](https://img.shields.io/github/repo-size/ik-performance/performance-blueprint-project)](https://github.com/ik-performance/performance-blueprint-project)
![](https://img.shields.io/github/languages/top/ik-performance/performance-blueprint-project?color=green&logo=terraform&logoColor=blue)
![](https://img.shields.io/github/commit-activity/m/ik-performance/performance-blueprint-project)
![](https://img.shields.io/github/contributors/ik-performance/performance-blueprint-project)
![](https://img.shields.io/github/last-commit/ik-performance/performance-blueprint-project)

- [performance-blueprint-project](#performance-blueprint-project)
- [Documenation](https://k6.io/docs/using-k6/)
- [Github](https://github.com/loadimpact/k6)

[![](https://img.shields.io/github/license/ik-performance/k6-blueprint)](https://github.com/ik-performance/k6-blueprint)
![](https://img.shields.io/github/v/tag/ik-performance/k6-blueprint)
![](https://img.shields.io/issues/github/ik-performance/k6-blueprint)
![](https://img.shields.io/github/issues/ik-performance/k6-blueprint)
![](https://img.shields.io/github/issues-closed/ik-performance/k6-blueprint)
[![](https://img.shields.io/github/languages/code-size/ik-performance/k6-blueprint)](https://github.com/ik-performance/k6-blueprint)
[![](https://img.shields.io/github/repo-size/ik-performance/k6-blueprint)](https://github.com/ik-performance/k6-blueprint)
![](https://img.shields.io/github/languages/top/ik-performance/k6-blueprint?color=green&logo=terraform&logoColor=blue)
![](https://img.shields.io/github/commit-activity/m/ik-performance/k6-blueprint)
![](https://img.shields.io/github/contributors/ik-performance/k6-blueprint)
![](https://img.shields.io/github/last-commit/ik-performance/k6-blueprint)

- [k6-blueprint](#k6-blueprint)
* [Running locally](#running-locally)
+ [DSL](#dsl)
+ [Scenario's](#scenario-s)
+ [Simulations](#simulations)
* [Mocking](#mocking)
+ [Data](#data)
+ [API](#api)
+ [Alternative Examples](#alternative-examples)
* [Targets](#targets)
* [:memo: Guidelines](#-memo--guidelines)
* [License](#license)
Expand Down Expand Up @@ -54,15 +58,20 @@ For all awailable simulations `make choose/simulation`
- https://jsonplaceholder.typicode.com
- https://test-api.k6.io/

### Alternative Examples

- https://github.com/loadimpact/k6/tree/master/samples
- https://github.com/ik-performance/postman-to-k6/tree/master/example

## Targets

<!-- START makefile-doc -->
```
$ make help
$ make help
change Update changelog
choose/simulation Show all simulations
init Commit hooks setup
validate Validate with pre-commit hooks
validate Validate with pre-commit hooks
```
<!-- END makefile-doc -->

Expand Down
Empty file removed conf/.gitkeep
Empty file.
27 changes: 27 additions & 0 deletions simulations/conf/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"hosts": {
"test.k6.io": "1.2.3.4"
},
"stages": [
{
"duration": "1m",
"target": 10
},
{
"duration": "1m",
"target": 30
},
{
"duration": "1m",
"target": 0
}
],
"thresholds": {
"http_req_duration": [
"avg<100",
"p(95)<200"
]
},
"noConnectionReuse": true,
"userAgent": "MyK6UserAgentString/1.0"
}
8 changes: 8 additions & 0 deletions simulations/conf/influx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"vus": 100,
"collectors": {
"influxdb": {
"tagsAsFields": ["vu","iter", "url", "name"]
}
}
}
24 changes: 24 additions & 0 deletions simulations/conf/k6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"stages": [
{
"duration": "10s",
"target": 10
},
{
"duration": "20s",
"target": 30
},
{
"duration": "10s",
"target": 0
}
],
"thresholds": {
"http_req_duration": [
"avg<100",
"p(95)<200"
]
},
"noConnectionReuse": true,
"userAgent": "MyK6UserAgentString/1.0"
}
16 changes: 16 additions & 0 deletions simulations/data/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"users": [
{
"username": "admin",
"password": "123"
},
{
"username": "test",
"password": "1234"
},
{
"username": "invaliduser",
"password": "password"
}
]
}
32 changes: 32 additions & 0 deletions simulations/initial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import http from "k6/http";
import { check } from "k6";

/*
* Stages (aka ramping) is how you, in code, specify the ramping of VUs.
* That is, how many VUs should be active and generating traffic against
* the target system at any specific point in time for the duration of
* the test.
*
* The following stages configuration will result in up-flat-down ramping
* profile over a 20s total test duration.
*/

export let options = {
stages: [
// Ramp-up from 1 to 5 VUs in 10s
{ duration: "10s", target: 5 },

// Stay at rest on 5 VUs for 5s
{ duration: "5s", target: 5 },

// Ramp-down from 5 to 0 VUs for 5s
{ duration: "5s", target: 0 }
],
"noConnectionReuse": true,
"userAgent": "K6UserAgentString/1.0"
};

export default function () {
let res = http.get("http://httpbin.org/");
check(res, { "status is 200": (r) => r.status === 200 });
}
17 changes: 17 additions & 0 deletions simulations/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import http from "k6/http";
import { check } from "k6";

export default function () {
// Send a JSON encoded POST request
let body = JSON.stringify({ key: "value" });
let res = http.post("http://httpbin.org/post", body, { headers: { "Content-Type": "application/json" } });

// Use JSON.parse to deserialize the JSON (instead of using the r.json() method)
let j = JSON.parse(res.body);

// Verify response
check(res, {
"status is 200": (r) => r.status === 200,
"is key correct": (r) => j.json.key === "value",
});
}
122 changes: 122 additions & 0 deletions simulations/k6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import http from "k6/http";
import { check, group, sleep } from "k6";
import { Counter, Rate, Trend } from "k6/metrics";
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.0.0/index.js";

const loginData = JSON.parse(open("./opt/data/users.json"));

/* Options
Global options for your script
stages - Ramping pattern
thresholds - pass/fail criteria for the test
ext - Options used by Load Impact cloud service test name and distribution
*/
export let options = {
stages: [
{ target: 200, duration: "20s" },
{ target: 200, duration: "1m" },
{ target: 0, duration: "10s" }
],
thresholds: {
"http_req_duration": ["p(95)<500"],
"http_req_duration{staticAsset:yes}": ["p(95)<100"],
"check_failure_rate": ["rate<0.3"]
},
ext: {
loadimpact: {
// projectID: 3113635,
name: "Insights Demo with Cloud Execution",
distribution: {
scenarioLabel1: { loadZone: "amazon:us:ashburn", percent: 50 },
scenarioLabel2: { loadZone: "amazon:ie:dublin", percent: 50 }
}
}
}
};

// Custom metrics
// We instantiate them before our main function
let successfulLogins = new Counter("successful_logins");
let checkFailureRate = new Rate("check_failure_rate");
let timeToFirstByte = new Trend("time_to_first_byte", true);


/* Main function
The main function is what the virtual users will loop over during test execution.
*/
export default function () {
// We define our first group. Pages natually fit a concept of a group
// You may have other similar actions you wish to "group" together
group("Front page", function () {
let res = null;
// As mentioned above, this logic just forces the performance alert for too many urls, use env URL_ALERT to force it
// It also highlights the ability to programmatically do things right in your script
if (__ENV.URL_ALERT) {
res = http.get("http://test.k6.io/?ts=" + Math.round(randomIntBetween(1, 2000)));
} else {
res = http.get("http://test.k6.io/?ts=" + Math.round(randomIntBetween(1, 2000)), { tags: { name: "http://test.k6.io/ Aggregated" } });
}
let checkRes = check(res, {
"Homepage body size is 11026 bytes": (r) => r.body.length === 11026,
"Homepage welcome header present": (r) => r.body.indexOf("Welcome to the k6.io demo site!") !== -1
});

// Record check failures
checkFailureRate.add(!checkRes);

// Record time to first byte and tag it with the URL to be able to filter the results in Insights
timeToFirstByte.add(res.timings.waiting, { ttfbURL: res.url });

// Load static assets
group("Static assets", function () {
let res = http.batch([
["GET", "http://test.k6.io/static/css/site.css", {}, { tags: { staticAsset: "yes" } }],
["GET", "http://test.k6.io/static/js/prisms.js", {}, { tags: { staticAsset: "yes" } }]
]);
checkRes = check(res[0], {
"Is stylesheet 4859 bytes?": (r) => r.body.length === 4859,
});

// Record check failures
checkFailureRate.add(!checkRes);

// Record time to first byte and tag it with the URL to be able to filter the results in Insights
timeToFirstByte.add(res[0].timings.waiting, { ttfbURL: res[0].url, staticAsset: "yes" });
timeToFirstByte.add(res[1].timings.waiting, { ttfbURL: res[1].url, staticAsset: "yes" });
});

});

sleep(10);

group("Login", function () {
let res = http.get("http://test.k6.io/my_messages.php");
let checkRes = check(res, {
"Users should not be auth'd. Is unauthorized header present?": (r) => r.body.indexOf("Unauthorized") !== -1
});

// Record check failures
checkFailureRate.add(!checkRes);

let position = Math.floor(Math.random() * loginData.users.length);
let credentials = loginData.users[position];

res = http.post("http://test.k6.io/login.php", { login: credentials.username, password: credentials.password, redir: '1' });
checkRes = check(res, {
"is logged in welcome header present": (r) => r.body.indexOf("Welcome, admin!") !== -1
});

// Record successful logins
if (checkRes) {
successfulLogins.add(1);
}

// Record check failures
checkFailureRate.add(!checkRes, { page: "login" });

// Record time to first byte and tag it with the URL to be able to filter the results in Insights
timeToFirstByte.add(res.timings.waiting, { ttfbURL: res.url });

sleep(10);
});
}
Empty file removed src/.gitkeep
Empty file.

0 comments on commit 913b2d9

Please sign in to comment.