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

Allow k6 scenario to be defined within k6 test #1797

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7a3e253
Add K6 own get_load method
asos-benpearson Nov 29, 2023
9d8904b
add vscode to gitignore
asos-benpearson Jan 8, 2024
0ca4a2c
added change file
asos-benpearson Jan 8, 2024
bc33321
Merge branch 'master' of https://github.com/benpearson84/taurus into …
asos-benpearson Jan 30, 2024
8334e75
add tests
asos-benpearson Jan 30, 2024
32085e8
fix broken test
asos-benpearson Jan 30, 2024
5c6a3ee
Merge branch 'master' of https://github.com/benpearson84/taurus into …
asos-benpearson Mar 6, 2024
35a8536
fix tests to cover new get_load method
asos-benpearson Mar 6, 2024
11f6c4b
missing self parameter
asos-benpearson Mar 6, 2024
28bcb56
fix broken test
asos-benpearson Mar 6, 2024
dd259dd
fix test_get_load_str_fail test
asos-benpearson Mar 6, 2024
1c2d174
improve code coverage
asos-benpearson Mar 6, 2024
9334acd
Update docymentation to include example yaml
asos-benpearson Mar 7, 2024
3f27103
Merge branch 'Blazemeter:master' into fix/k6-scenario-within-k6-test
asos-benpearson Apr 18, 2024
06c7c47
Update __init__.py
asos-benpearson Apr 19, 2024
cd1b215
Merge branch 'master' of https://github.com/benpearson84/taurus into …
asos-benpearson Aug 12, 2024
3889e0f
Merge branch 'fix/k6-scenario-within-k6-test' of https://github.com/b…
asos-benpearson Aug 12, 2024
53b15f1
Merge pull request #1 from Blazemeter/master
asos-benpearson Jan 21, 2025
4c13ba1
Merge branch 'master' of https://github.com/benpearson84/taurus into …
asos-benpearson Jan 21, 2025
d62ecf8
Remove --out to csv file as not required
asos-benpearson Jan 21, 2025
ef851b4
fix broken kpi test
asos-benpearson Jan 21, 2025
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ bzt/resources/NUnitRunner/*
integr-artifacts*
*.log
examples/local*
bzt/20*
bzt/20*

# vscode
.vscode
51 changes: 50 additions & 1 deletion bzt/modules/k6.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from bzt.modules import ScenarioExecutor
from bzt.modules.console import ExecutorWidget
from bzt.modules.aggregator import ResultsReader, ConsolidatingAggregator
from bzt.utils import RequiredTool, CALL_PROBLEMS, FileReader, shutdown_process
from bzt.utils import RequiredTool, CALL_PROBLEMS, FileReader, shutdown_process, dehumanize_time, numeric_types


class K6Executor(ScenarioExecutor):
Expand Down Expand Up @@ -67,6 +67,55 @@ def startup(self):
cmdline += [self.script]
self.process = self._execute(cmdline)

def get_load(self):
def eval_int(value):
try:
return int(value)
except (ValueError, TypeError):
return value

def eval_float(value):
try:
return int(value)
except (ValueError, TypeError):
return value

raw_load = self.get_raw_load()

iterations = eval_int(raw_load.iterations or 0)
ramp_up = raw_load.ramp_up

throughput = eval_float(raw_load.throughput or 0)
concurrency = eval_int(raw_load.concurrency or 0)

steps = eval_int(raw_load.steps)
hold = dehumanize_time(raw_load.hold or 0)

if ramp_up is None:
duration = hold
else:
ramp_up = dehumanize_time(raw_load.ramp_up)
duration = hold + ramp_up

msg = ''
if not isinstance(concurrency, numeric_types + (type(None),)):
msg += "Invalid concurrency value[%s]: %s " % (type(concurrency).__name__, concurrency)
if not isinstance(throughput, numeric_types + (type(None),)):
msg += "Invalid throughput value[%s]: %s " % (type(throughput).__name__, throughput)
if not isinstance(steps, numeric_types + (type(None),)):
msg += "Invalid throughput value[%s]: %s " % (type(steps).__name__, steps)
if not isinstance(iterations, numeric_types + (type(None),)):
msg += "Invalid throughput value[%s]: %s " % (type(iterations).__name__, iterations)

if msg:
raise TaurusConfigError(msg)

return self.LOAD_FMT(concurrency=concurrency, ramp_up=ramp_up, throughput=throughput, hold=hold,
iterations=iterations, duration=duration, steps=steps)




def get_widget(self):
if not self.widget:
label = "%s" % self
Expand Down
10 changes: 10 additions & 0 deletions site/dat/docs/K6.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ execution:
script: k6-test.js # has to be a valid K6 script
```

For scenarios defined within the K6 script itself you can use the example below

Example:
```yaml
execution:
- executor: k6
scenario:
script: k6-test.js # has to be a valid K6 script
```

Please keep in mind that it is necessary to provide a valid script to run tests.

## Script Example
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added own get_load method to k6.py so iterations is defaulted to 0 if not specified in the Taurus scenario.
59 changes: 59 additions & 0 deletions tests/unit/modules/test_k6.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from os.path import join

import bzt
from bzt.engine.names import EXEC

from bzt.modules.aggregator import DataPoint, KPISet
from bzt.modules.k6 import K6Executor, K6LogReader
Expand Down Expand Up @@ -103,6 +104,64 @@ def test_iterations_multiplied(self):
})
self.assertIn("--iterations 100", self.CMD_LINE)

def test_get_load(self):
self.configure({EXEC: {
"ramp-up": "1", "concurrency": "2", "throughput": "3"}})
self.assertEqual(self.obj.execution.get("ramp-up"), "1")
self.assertEqual(self.obj.execution.get("concurrency"), "2")
self.assertEqual(self.obj.execution.get("throughput"), "3")
load = self.obj.get_load()
self.assertEqual(load.ramp_up, 1)
self.assertEqual(load.concurrency, 2)
self.assertEqual(load.throughput, 3)
self.assertEqual(load.iterations, 0)

self.assertEqual(self.obj.execution.get("ramp-up"), "1")
self.assertEqual(self.obj.execution.get("concurrency"), {"local": "2"})
self.assertEqual(self.obj.execution.get("throughput"), {"local": "3"})

self.obj.engine.config.get("execution")[0]["concurrency"] = "22"
load = self.obj.get_load()
self.assertEqual(load.concurrency, 22)
self.assertEqual(self.obj.execution.get("concurrency"), {"local": "22"})

def test_get_load_defaults(self):
self.configure({EXEC: {}})
self.assertFalse(self.obj.execution.get("ramp-up"))
self.assertFalse(self.obj.execution.get("concurrency"))
self.assertFalse(self.obj.execution.get("throughput"))
self.assertFalse(self.obj.execution.get("iterations"))
load = self.obj.get_load()
self.assertEqual(load.ramp_up, None)
self.assertEqual(load.concurrency, 0)
self.assertEqual(load.throughput, 0)
self.assertEqual(load.iterations, 0)

def test_get_load_str(self):
self.configure({EXEC: {
"concurrency": "2",
"hold-for": "3",
"ramp-up": "4",
"iterations": "5",
"throughput": "6",
"steps": "7",
}})
load = self.obj.get_load()
self.assertEquals(2, load.concurrency)
self.assertEquals(3, load.hold)
self.assertEquals(4, load.ramp_up)
self.assertEquals(5, load.iterations)
self.assertEquals(6, load.throughput)
self.assertEquals(7, load.steps)

def test_get_load_str_fail(self):
self.configure({EXEC: {
"concurrency": "2VU",
"throughput": "1PS",
"steps": "3ST",
"iterations": "4IT"
}})
self.assertRaises(bzt.TaurusConfigError, self.obj.get_load)

class TestK6Reader(BZTestCase):
def test_read(self):
Expand Down