samplers

Samplers are the basic test script steps.

They perform a specific action (eg, send an HTTP request) and report their time of completion, in other words, they are the subject which our test measure.

example - 1:

The most commonly used sampler is the HTTP sampler, lets take a look at a trivial example: In this example, our http sampler generates a get request to the postman echo server

from pymeter.api.samplers import HttpSampler
http_sampler = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")

example - 2:

Lets add a header to the request: Here the headers name is SomeKey and it’s value is some_value

from pymeter.api.samplers import HttpSampler
http_sampler = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1").header("SomeKey", "some_value")

example - 3:

Now lets send a post request: This post request has an application-json as a content type, and the body has a single value with var1 being the key and 1 being the value

from pymeter.api import ContentType
from pymeter.api.samplers import HttpSampler

http_sampler = (
    HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
    .header("SomeKey", "some_value")
    .post({"var1": 1}, ContentType.APPLICATION_JSON)
)

example - 4:

The Dummy Sampler in JMeter simulates requests to the server without actually running the requests, serving as a placeholder.

from pymeter.api.samplers import DummySampler
dummy_sampler = DummySampler("dummy_sampler", "hi dummy")
class pymeter.api.samplers.DummySampler(name, response_body, *children)

Bases: BaseSampler

The Dummy Sampler in JMeter simulates requests to the server without actually running the requests, serving as a placeholder.

Parameters:
  • name (str) –

  • response_body (str) –

class pymeter.api.samplers.HttpSampler(name, url, *children)

Bases: BaseSampler

Http sampler sends an http requests to a target server side By default it sends HTTP get request

Parameters:
  • name (str) –

  • url (str) –

header(key, value)

Append a header to request

Args:

key (str): Headers name value (str): Headers value

Returns:

Self: a new sampler instance

Parameters:
  • key (str) –

  • value (str) –

Return type:

Self

post(body, content_type)

Create a post request sampler

Args:

body (Union[Dict, List, str]): body of the request

content_type (ContentType): the content type of the request

Returns:

Self: a new sampler instance

Parameters:
Return type:

Self