config

The config module contain classes that define the overall structure of the test script. Here we will define the overall plan, the setup and teardown processes and the load shapes that the test script generate.

example - 1:

In this example we use the standard python unittest module with setup and teardown, The test case it self will rump up 10 threads in 1 second and hold the load for additional 10 seconds. Note that the setup and teardown code is executed outside of JMeter’s context in this example.

from unittest import TestCase, main
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold
from pymeter.api.samplers import HttpSampler


class TestTestPlanClass(TestCase):
    @classmethod
    def setUpClass(cls):
        print("setUpClass")

    def test_case_1(self):
        # create HTTP sampler, sends a get request to the given url
        http_sampler = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")

        # create a thread group that will rump up 10 threads in 1 second and
        # hold the load for additional 10 seconds, give it the http sampler as a child input
        thread_group_main = ThreadGroupWithRampUpAndHold(10, 1, 10, http_sampler)

        # create a test plan with the required thread group
        test_plan = TestPlan(thread_group_main)

        # run the test plan and take the results
        stats = test_plan.run()
        self.assertLess(stats.sample_time_99_percentile_milliseconds, 2000)

    @classmethod
    def tearDownClass(cls):
        print("tearDownClass")

example - 2:

If for some reason it is needed for you to run the setup and teardown code from with in the JMeter context, here’s how you can do it:
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
from pymeter.api.samplers import HttpSampler


# create HTTP sampler, sends a get request to the given url
http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")
http_sampler3 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=3")


# create a setup thread group
thread_group_setup = SetupThreadGroup(http_sampler1)

# create a thread group that will rump up 10 threads in 1 second and
# hold the load for additional 10 seconds, give it the http sampler as a child input
thread_group_main = ThreadGroupWithRampUpAndHold(10, 1,10, http_sampler2)

# create a teardown thread group
thread_group_setup = TeardownThreadGroup(http_sampler3)

# create a test plan with the required thread group
test_plan = TestPlan(thread_group_setup, thread_group_main, thread_group_setup)

# run the test plan and take the results
stats = test_plan.run()


# Assert that the 99th percentile of response time is less than 2000 milliseconds.
assert (
    stats.sample_time_99_percentile_milliseconds <= 2000
), f"99th precentile should be less than 2000 milliseconds, got {stats.sample_time_99_percentile_milliseconds}"

example - 3:

We can use a CsvDataset to append a unique dataset to our test elements, In this example, we will generate unique data for our entire test plan:

from pymeter.api.config import TestPlan, ThreadGroupSimple, CsvDataset
from pymeter.api.samplers import HttpSampler
from pymeter.api.timers import ConstantTimer


timer = ConstantTimer(2000)
csv_data_set = CsvDataset("playground/file.csv")
http_sampler1 = HttpSampler(
    "Echo_${id}", "https://postman-echo.com/get?var=${id}", timer
)
thread_group1 = ThreadGroupSimple(3, 1)
thread_group1.children(http_sampler1)


http_sampler2 = HttpSampler("Echo_${id}", "https://postman-echo.com/get?var=do", timer)
thread_group2 = ThreadGroupSimple(3, 1, http_sampler2)
test_plan = TestPlan(thread_group1, thread_group2, csv_data_set)
stats = test_plan.run()

example - 4:

We can create vars from with in JMeters context using the Vars class

from pymeter.api.config import TestPlan, ThreadGroupSimple, Vars
from pymeter.api.samplers import HttpSampler
from pymeter.api.timers import ConstantTimer
from pymeter.api.reporters import HtmlReporter

jmeter_variables = Vars(id1="value1", id2="value2")
html_reporter = HtmlReporter()
timer = ConstantTimer(2000)
http_sampler1 = HttpSampler(
    "Echo_${id1}", "https://postman-echo.com/get?var=${id1}", timer
)
thread_group1 = ThreadGroupSimple(3, 1)
thread_group1.children(http_sampler1)


http_sampler2 = HttpSampler("Echo_${id2}", "https://postman-echo.com/get?var=do", timer)
thread_group2 = ThreadGroupSimple(3, 1, http_sampler2)
test_plan = TestPlan(thread_group1, thread_group2, html_reporter, jmeter_variables)
stats = test_plan.run()
We can also set a single variable using the set method
from pymeter.api.config import Vars
jmeter_variables = Vars(id1="value1", id2="value2")
jmeter_variables.set("id1", "v2")

example - 5:

We Can also generate data for each thread group:

from pymeter.api.config import TestPlan, ThreadGroupSimple, CsvDataset
from pymeter.api.samplers import HttpSampler
from pymeter.api.timers import ConstantTimer


timer = ConstantTimer(2000)
csv_data_set1 = CsvDataset("playground/file1.csv")
csv_data_set2 = CsvDataset("playground/file2.csv")
http_sampler1 = HttpSampler(
    "Echo_${id}", "https://postman-echo.com/get?var=${id}", timer
)
thread_group1 = ThreadGroupSimple(3, 1)
thread_group1.children(http_sampler1, csv_data_set1)


http_sampler2 = HttpSampler("Echo_${id}", "https://postman-echo.com/get?var=do", timer)
thread_group2 = ThreadGroupSimple(3, 1, http_sampler2, csv_data_set2)
test_plan = TestPlan(thread_group1, thread_group2)
stats = test_plan.run()

Classes

class pymeter.api.config.CsvDataset(csv_file)

Bases: TestPlanChildElement, ThreadGroupChildElement

csv data set allows you to append unique data set to samplers

Parameters:

csv_file (str) –

children(*children)

adds children to element Example 1:

    from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
    from pymeter.api.samplers import HttpSampler

    http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
    http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

    thread_group_setup = SetupThreadGroup()
    thread_group_setup.children(http_sampler1, http_sampler2)

Example 2:
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
from pymeter.api.samplers import HttpSampler

http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

thread_group_setup = SetupThreadGroup()
thread_group_setup.children(http_sampler1)
thread_group_setup.children(http_sampler2)
class pymeter.api.config.SetupThreadGroup(*children)

Bases: BaseThreadGroup

thread group for setting up test from within the context of JMeter

Parameters:

children (ThreadGroupChildElement) –

class pymeter.api.config.TeardownThreadGroup(*children)

Bases: BaseThreadGroup

thread group for tearing down test from within the context of JMeter

Parameters:

children (ThreadGroupChildElement) –

class pymeter.api.config.TestPlan(*children)

Bases: BaseConfigElement

This is the object that will call on the invocation of the test in the JMeter engine.

Parameters:

children (TestPlanChildElement) –

children(*children)

adds children to element Example 1:

    from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
    from pymeter.api.samplers import HttpSampler

    http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
    http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

    thread_group_setup = SetupThreadGroup()
    thread_group_setup.children(http_sampler1, http_sampler2)

Example 2:
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
from pymeter.api.samplers import HttpSampler

http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

thread_group_setup = SetupThreadGroup()
thread_group_setup.children(http_sampler1)
thread_group_setup.children(http_sampler2)
run()

run() will execute the test plan code and return an object with aggregated results.

This method is blocking and therefore the entire program will hang until the method is completed.

By default, run prints stats to the stdio, for other reporting options please do check the reporters page

class pymeter.api.config.ThreadGroupSimple(number_of_threads, iterations, *children, name='Thread Group')

Bases: BaseThreadGroup

Thread group defined by number of threads and number of iterations

Parameters:
  • number_of_threads (int) –

  • iterations (int) –

  • children (ThreadGroupChildElement) –

  • name (str) –

class pymeter.api.config.ThreadGroupWithRampUpAndHold(number_of_threads, rampup_time_seconds, holdup_time_seconds, *children, name='Thread Group')

Bases: BaseThreadGroup

Thread group that rumps up a number of thread in a given number of seconds and then holds the load for a given number of seconds.

Parameters:
  • number_of_threads (int) –

  • rampup_time_seconds (float) –

  • holdup_time_seconds (float) –

  • name (str) –

class pymeter.api.config.Vars(**variables)

Bases: TestPlanChildElement

Vars are key value pairs

children(*children)

adds children to element Example 1:

    from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
    from pymeter.api.samplers import HttpSampler

    http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
    http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

    thread_group_setup = SetupThreadGroup()
    thread_group_setup.children(http_sampler1, http_sampler2)

Example 2:
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
from pymeter.api.samplers import HttpSampler

http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

thread_group_setup = SetupThreadGroup()
thread_group_setup.children(http_sampler1)
thread_group_setup.children(http_sampler2)
set(key, value)

Sets a single key value pair

Parameters:
  • key (str) –

  • value (str) –