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 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
2 changes: 1 addition & 1 deletion bzt/resources/version/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEV_VERSION = "0.0.0dev0"
DEV_VERSION = "0.0.1-DEV"

try:
from .version import VERSION
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