# Perf Tools & Testing - K6
## Deploy on CF
Open the IDE environment given to you. If URL not working,get one from the mediator.
Replace with your Inumber or Cnumber in the below URL.
URL: `https://<inumber>-theia.dev.perfsizer.sapcloud.io/`
Example : `https://i513578-theia.dev.perfsizer.sapcloud.io/`

To open Terminal : Menu –> Terminal –> New Terminal

1. Open the command prompt/terminal in your theia IDE and set the CF API using the below command
> cf api api.cf.scale.perfteam.sapcloud.io
2. Login to cf with the following command
> cf auth devx_user devx2021
3. Set the orgranization using the below command
> cf target -o devx
4. Go to `devx-perf` folder.
> cd devx-perf/
5. Open the manifest.yml file and edit the name of the applications with your `I-number` and save it(File -> save) ,such that it is unique.

5. Once you changed the manifest.yml, you have to deploy the application using the following command.
> cf push
Note: CF Push will use the manifest.yml file to deploy the application in Cloud.For Further information regarding manifest file,please refer the below link.
https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html
### Test Your application
> curl https://<inumber>-nginx.cfapps.scale.perfteam.sapcloud.io
You will get the below response(as in image)

`
Hurray!!! You have deployed a application in SAP Cloud Platorm.
## Performance Tools
There are lot of load generator tools available in the market. Each has it's own unqiue features and way of load generation. Based on the application use cases , toolset may vary. Here will see tool called `K6` which we use widely for our performance tests.
### Other Notable Tools :
- nGrinder
- Apache Jmeter
- HP LoadRunner
- Locust
- Hey
- Fortio
- Tsung
- Gatling
- Wrk
Official Page: https://k6.io/docs/
### About K6
k6 works with the concept of virtual users (VUs), which run scripts - they're essentially glorified, parallel while(true) loops. Scripts are written using JavaScript, as ES6 modules, which allows you to break larger tests into smaller pieces, or make reusable pieces as you like.
Scripts must contain, at the very least, a `default` function - this defines the entry point for your VUs, similar to the `main()` function in many other languages:
```
export default function () {
// vu code: do things here...
}
```
#### Using options
* Options variable is used to specify the parameters like number of user, duration of test , rampup period etc.
```
import http from 'k6/http';
export let options = {
vus: 10,
duration: '30s',
};
export default function () {
http.get(URL);
}
```
#### Checks in K6
Checks are like assertions, but differ in that they don't halt the execution. Instead, they store the result of the check, pass or fail, and let the script execution continue. Take a look at thresholds for a way to halt the execution of a test based on checks.
```
import { check } from 'k6';
import http from 'k6/http';
export default function () {
let res = http.get(URL);
check(res, {
'is status 200': (r) => r.status === 200,
});
}
```
## Configure the K6 Script.
* We have created the template script for k6 in the devx-perf folder->`script.js`.
* In the k6 script, replace the `Inumber` with your Inumber.

In this script, we have used
* Number of Users - 35
* Test Duration - 3mins
* Protocol - GET
## Trigger the test
Trigger the test using the following K6 command.
> k6 run script.js --out influxdb=http://influxdb:8086/devx --system-tags=method,name,status,tag
- run - Will run the test
- --out - Will store the results(here we are using influxdb to store results)
InfluxDb is open source timeseries database.
official docs - https://www.influxdata.com/

## View Real Time Results in Grafana
Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources.

Official docs - https://grafana.com/
1. In the local browser, give the grafana URL to view the results. If Grafana URL is not working, get it from the mediator.
Replace with your Inumber or Cnumber in the below URL.
URL : `https://<i-number>.dev.perfsizer.sapcloud.io/d/devx`
Example url : `https://i513578.dev.perfsizer.sapcloud.io/d/devx`
Username/Password : admin/admin
2. Configure the test time, to see the results in graph.

you can see the following panel,
- Number of Virtual users
- Request Per second
- Erros per second
Similarly you can see the request duration(average,minimum,maximum).

## Result Summary
Once the test is finished, in the theia IDE, you can see the summary report from k6(as below), which gives the average TPS,RT,Requestes failed, etc.
Important Metrics to view in summary,
* `iterations` - total number of requests, Average TPS
* `iteration_duration` - Response Time(avg,max,min..)
* `vus` - Number of Virtual Users
* `http_req_failed` - Number of requests failed

## Scale Test
1. So far,you tested the application with only one instance.Now you will scale the application to two instances and test the application.
> cf scale <inumber>-nginx -i 2

2. Now when you execute cf app, you will see two instance running.
> cf app <inumber>-nginx

### Repeat the test
Once the application is scaled,repeat the same test with above K6 commands, you will see average TPS increased.