Timers and think time

A timer waits before the sampler under it. Without one, a user sends requests as fast as the server answers, which is not what a person does and not what a load test should imitate. This lesson adds a pause four ways and shows, for each, the time between the first and the last sample of the run.

The constant timer

The first plan has one timer above the request, with a delay of ${__P(delay,300)} milliseconds. Four samples with three pauses of 300 ms put at least 900 ms between the first and the last sample, and the little program below reads the timestamps of the two ends of the file and prints that statement; its first argument is the floor in milliseconds and the second the file.

plan.jmx

<?xml version="1.0" encoding="UTF-8"?> <jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.3"> <hashTree> <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Timers" enabled="true"> <boolProp name="TestPlan.functional_mode">false</boolProp> <boolProp name="TestPlan.serialize_threadgroups">false</boolProp> <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"> <elementProp name="host" elementType="Argument"> <stringProp name="Argument.name">host</stringProp> <stringProp name="Argument.value">127.0.0.1</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="port" elementType="Argument"> <stringProp name="Argument.name">port</stringProp> <stringProp name="Argument.value">8099</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> </collectionProp> </elementProp> </TestPlan> <hashTree> <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true"> <stringProp name="ThreadGroup.on_sample_error">continue</stringProp> <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true"> <boolProp name="LoopController.continue_forever">false</boolProp> <stringProp name="LoopController.loops">4</stringProp> </elementProp> <stringProp name="ThreadGroup.num_threads">1</stringProp> <stringProp name="ThreadGroup.ramp_time">1</stringProp> <boolProp name="ThreadGroup.scheduler">false</boolProp> <stringProp name="ThreadGroup.duration"/> <stringProp name="ThreadGroup.delay"/> </ThreadGroup> <hashTree> <ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true"> <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="HTTPSampler.domain">${host}</stringProp> <stringProp name="HTTPSampler.port">${port}</stringProp> <stringProp name="HTTPSampler.protocol">http</stringProp> <stringProp name="HTTPSampler.contentEncoding"/> <stringProp name="HTTPSampler.path"/> </ConfigTestElement> <hashTree/> <ResultCollector guiclass="SimpleDataWriter" testclass="ResultCollector" testname="results.jtl" enabled="true"> <boolProp name="ResultCollector.error_logging">false</boolProp> <objProp> <name>saveConfig</name> <value class="SampleSaveConfiguration"> <time>true</time> <latency>true</latency> <timestamp>true</timestamp> <success>true</success> <label>true</label> <code>true</code> <message>true</message> <threadName>true</threadName> <dataType>true</dataType> <encoding>false</encoding> <assertions>true</assertions> <subresults>false</subresults> <responseData>false</responseData> <samplerData>false</samplerData> <xml>false</xml> <fieldNames>true</fieldNames> <responseHeaders>false</responseHeaders> <requestHeaders>false</requestHeaders> <responseDataOnError>false</responseDataOnError> <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage> <assertionsResultsToSave>0</assertionsResultsToSave> <bytes>true</bytes> <sentBytes>true</sentBytes> <url>true</url> <threadCounts>true</threadCounts> <idleTime>true</idleTime> <connectTime>true</connectTime> </value> </objProp> <stringProp name="filename">results.jtl</stringProp> </ResultCollector> <hashTree/> <ConstantTimer guiclass="ConstantTimerGui" testclass="ConstantTimer" testname="think time" enabled="true"> <stringProp name="ConstantTimer.delay">${__P(delay,300)}</stringProp> </ConstantTimer> <hashTree/> <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="health" enabled="true"> <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="HTTPSampler.domain">${host}</stringProp> <stringProp name="HTTPSampler.port">${port}</stringProp> <stringProp name="HTTPSampler.protocol">http</stringProp> <stringProp name="HTTPSampler.contentEncoding"/> <stringProp name="HTTPSampler.path">/health</stringProp> <stringProp name="HTTPSampler.method">GET</stringProp> <boolProp name="HTTPSampler.follow_redirects">true</boolProp> <boolProp name="HTTPSampler.auto_redirects">false</boolProp> <boolProp name="HTTPSampler.use_keepalive">true</boolProp> <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp> <stringProp name="HTTPSampler.connect_timeout"/> <stringProp name="HTTPSampler.response_timeout"/> </HTTPSamplerProxy> <hashTree/> </hashTree> </hashTree> </hashTree> </jmeterTestPlan>

span.py

import sys floor = int(sys.argv[1]) lines = open(sys.argv[2], encoding="utf-8").read().splitlines()[1:] stamps = [int(line.split(",")[0]) for line in lines if line] span = max(stamps) - min(stamps) print(f"at least {floor} ms" if span >= floor else f"shorter than {floor} ms")

The plan talks to the course server of the sixth lesson; its whole file is repeated here, so that this page runs on its own.

server.py

#!/usr/bin/env python3 """The target server of the JMeter course: a small JSON API on 127.0.0.1. python3 server.py 8099 Every endpoint is a few lines: the course tests them, it does not test the server. /slow sleeps, /fail answers 500, /login sets a cookie and returns a token, /secure wants that token in an Authorization header. """ import json import sys import time import uuid from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import parse_qs, urlparse USERS = [{"id": 1, "name": "Ada"}, {"id": 2, "name": "Grace"}, {"id": 3, "name": "Linus"}] TOKEN = "secret-token" class Handler(BaseHTTPRequestHandler): """One request, one small JSON answer.""" protocol_version = "HTTP/1.1" def log_message(self, fmt, *args): """Keep the course\'s server log quiet: every request would be a line.""" def reply(self, status, body, ctype="application/json", headers=None): """Send a body with its length, so a keep-alive client can read it.""" data = body.encode("utf-8") self.send_response(status) self.send_header("Content-Type", ctype) self.send_header("Content-Length", str(len(data))) for name, value in (headers or {}).items(): self.send_header(name, value) self.end_headers() self.wfile.write(data) def reply_json(self, status, value, headers=None): self.reply(status, json.dumps(value), headers=headers) def do_GET(self): url = urlparse(self.path) query = parse_qs(url.query) if url.path == "/": self.reply(200, "<!doctype html><title>course</title><h1>JMeter course</h1>", "text/html") elif url.path == "/health": self.reply_json(200, {"status": "ok"}) elif url.path == "/api/users": self.reply_json(200, {"users": USERS, "count": len(USERS)}) elif url.path.startswith("/api/users/"): user = next((u for u in USERS if str(u["id"]) == url.path.rsplit("/", 1)[-1]), None) self.reply_json(200 if user else 404, user or {"error": "no such user"}) elif url.path == "/items": page = int(query.get("page", ["1"])[0]) self.reply_json(200, {"page": page, "items": [f"item-{page}-{n}" for n in range(1, 4)]}) elif url.path == "/slow": ms = int(query.get("ms", ["300"])[0]) time.sleep(ms / 1000.0) self.reply_json(200, {"slept_ms": ms}) elif url.path == "/fail": self.reply_json(500, {"error": "the server was told to fail"}) elif url.path == "/login": self.reply_json(200, {"token": TOKEN}, headers={"Set-Cookie": "session=abc123; Path=/"}) elif url.path == "/secure": if self.headers.get("Authorization") == "Bearer " + TOKEN: self.reply_json(200, {"message": "welcome"}) else: self.reply_json(401, {"error": "a token is required"}) elif url.path == "/token": self.reply_json(200, {"token": uuid.uuid4().hex}) elif url.path == "/redirect": self.reply(302, "", headers={"Location": "/health"}) elif url.path == "/xml": self.reply(200, "<user><id>1</id><name>Ada</name></user>", "application/xml") elif url.path == "/echo": body = json.dumps({"method": "GET", "path": self.path, "headers": dict(self.headers)}) self.reply(200, body) else: self.reply_json(404, {"error": "not found", "path": url.path}) def do_POST(self): length = int(self.headers.get("Content-Length", "0")) raw = self.rfile.read(length).decode("utf-8") if length else "" if self.path == "/api/orders": try: order = json.loads(raw) if raw else {} except json.JSONDecodeError: self.reply_json(400, {"error": "the body is not JSON"}) return self.reply_json(201, {"id": 1001, "received": order}) else: self.reply_json(404, {"error": "not found", "path": self.path}) if __name__ == "__main__": port = int(sys.argv[1]) if len(sys.argv) > 1 else 8099 server = ThreadingHTTPServer(("127.0.0.1", port), Handler) print(f"listening on 127.0.0.1:{port}", flush=True) server.serve_forever()

python3 server.py 8099 &

jmeter -n -t plan.jmx -l results.jtl -j jmeter.log

Creating summariser <summary>
Created the tree successfully using plan.jmx
Starting standalone test @ 2026 Nov 25 05:36:22 GMT (1795584982122)
Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
summary =      4 in 00:00:02 =    2.7/s Avg:    14 Min:     2 Max:    48 Err:     0 (0.00%)
Tidying up ...    @ 2026 Nov 25 05:36:23 GMT (1795584983791)
... end of run

python3 span.py 900 results.jtl

at least 900 ms

A shorter pause, from the command line

The delay is a property, so the same file runs with another pause: -Jdelay=100 gives three pauses of 100 ms and the sentence about 300 ms. Nothing in the file changes, which is what a property is for.

jmeter -n -t plan.jmx -l short.jtl -j short.log -Jdelay=100

Creating summariser <summary>
Created the tree successfully using plan.jmx
Starting standalone test @ 2026 Nov 25 05:36:28 GMT (1795584988367)
Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
summary =      4 in 00:00:01 =    5.8/s Avg:    14 Min:     2 Max:    51 Err:     0 (0.00%)
Tidying up ...    @ 2026 Nov 25 05:36:29 GMT (1795584989206)
... end of run

python3 span.py 300 short.jtl

at least 300 ms

The uniform random timer

A fixed pause is easy to recognise from the outside: every user asks at the same rhythm, and the server sees a pattern no crowd produces. The Uniform Random Timer waits a random time between the offset and the offset plus the range, so each request of a user comes at a different distance from the one before.

uniform.jmx

<?xml version="1.0" encoding="UTF-8"?> <jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.3"> <hashTree> <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="A random pause" enabled="true"> <boolProp name="TestPlan.functional_mode">false</boolProp> <boolProp name="TestPlan.serialize_threadgroups">false</boolProp> <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"> <elementProp name="host" elementType="Argument"> <stringProp name="Argument.name">host</stringProp> <stringProp name="Argument.value">127.0.0.1</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="port" elementType="Argument"> <stringProp name="Argument.name">port</stringProp> <stringProp name="Argument.value">8099</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> </collectionProp> </elementProp> </TestPlan> <hashTree> <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true"> <stringProp name="ThreadGroup.on_sample_error">continue</stringProp> <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true"> <boolProp name="LoopController.continue_forever">false</boolProp> <stringProp name="LoopController.loops">4</stringProp> </elementProp> <stringProp name="ThreadGroup.num_threads">1</stringProp> <stringProp name="ThreadGroup.ramp_time">1</stringProp> <boolProp name="ThreadGroup.scheduler">false</boolProp> <stringProp name="ThreadGroup.duration"/> <stringProp name="ThreadGroup.delay"/> </ThreadGroup> <hashTree> <ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true"> <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="HTTPSampler.domain">${host}</stringProp> <stringProp name="HTTPSampler.port">${port}</stringProp> <stringProp name="HTTPSampler.protocol">http</stringProp> <stringProp name="HTTPSampler.contentEncoding"/> <stringProp name="HTTPSampler.path"/> </ConfigTestElement> <hashTree/> <ResultCollector guiclass="SimpleDataWriter" testclass="ResultCollector" testname="results.jtl" enabled="true"> <boolProp name="ResultCollector.error_logging">false</boolProp> <objProp> <name>saveConfig</name> <value class="SampleSaveConfiguration"> <time>true</time> <latency>true</latency> <timestamp>true</timestamp> <success>true</success> <label>true</label> <code>true</code> <message>true</message> <threadName>true</threadName> <dataType>true</dataType> <encoding>false</encoding> <assertions>true</assertions> <subresults>false</subresults> <responseData>false</responseData> <samplerData>false</samplerData> <xml>false</xml> <fieldNames>true</fieldNames> <responseHeaders>false</responseHeaders> <requestHeaders>false</requestHeaders> <responseDataOnError>false</responseDataOnError> <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage> <assertionsResultsToSave>0</assertionsResultsToSave> <bytes>true</bytes> <sentBytes>true</sentBytes> <url>true</url> <threadCounts>true</threadCounts> <idleTime>true</idleTime> <connectTime>true</connectTime> </value> </objProp> <stringProp name="filename">results.jtl</stringProp> </ResultCollector> <hashTree/> <UniformRandomTimer guiclass="UniformRandomTimerGui" testclass="UniformRandomTimer" testname="think time" enabled="true"> <stringProp name="ConstantTimer.delay">100</stringProp> <stringProp name="RandomTimer.range">200</stringProp> </UniformRandomTimer> <hashTree/> <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="health" enabled="true"> <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="HTTPSampler.domain">${host}</stringProp> <stringProp name="HTTPSampler.port">${port}</stringProp> <stringProp name="HTTPSampler.protocol">http</stringProp> <stringProp name="HTTPSampler.contentEncoding"/> <stringProp name="HTTPSampler.path">/health</stringProp> <stringProp name="HTTPSampler.method">GET</stringProp> <boolProp name="HTTPSampler.follow_redirects">true</boolProp> <boolProp name="HTTPSampler.auto_redirects">false</boolProp> <boolProp name="HTTPSampler.use_keepalive">true</boolProp> <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp> <stringProp name="HTTPSampler.connect_timeout"/> <stringProp name="HTTPSampler.response_timeout"/> </HTTPSamplerProxy> <hashTree/> </hashTree> </hashTree> </hashTree> </jmeterTestPlan>

jmeter -n -t uniform.jmx -l uniform.jtl -j uniform.log

The run prints its own summary, and the sample count and error count of that line are stable; the milliseconds are not, and no page prints them. The time between the ends of the run is the statement: each of the three pauses is at least 100 ms, so the span is at least 300 ms.

python3 span.py 300 uniform.jtl

at least 300 ms

The Gaussian random timer

The Gaussian Random Timer is the same idea with a normal distribution: most pauses are near a deviation of the offset, and a few are longer. It is the timer a plan uses when the pauses of real users are known to be bell-shaped rather than uniformly spread. The element is otherwise identical to the uniform one, with the deviation where the range sits.

The constant throughput timer

A Constant Throughput Timer does not add a pause; it paces the samples to a rate per minute. The third plan asks for 120 samples per minute, which is one every half second, and four samples then need at least a second and a half. The timer computes the wait from the time the previous sample took, so a slow server and a fast one both end up near the rate.

paced.jmx

<?xml version="1.0" encoding="UTF-8"?> <jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.3"> <hashTree> <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="A paced run" enabled="true"> <boolProp name="TestPlan.functional_mode">false</boolProp> <boolProp name="TestPlan.serialize_threadgroups">false</boolProp> <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"> <elementProp name="host" elementType="Argument"> <stringProp name="Argument.name">host</stringProp> <stringProp name="Argument.value">127.0.0.1</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="port" elementType="Argument"> <stringProp name="Argument.name">port</stringProp> <stringProp name="Argument.value">8099</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> </collectionProp> </elementProp> </TestPlan> <hashTree> <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true"> <stringProp name="ThreadGroup.on_sample_error">continue</stringProp> <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true"> <boolProp name="LoopController.continue_forever">false</boolProp> <stringProp name="LoopController.loops">4</stringProp> </elementProp> <stringProp name="ThreadGroup.num_threads">1</stringProp> <stringProp name="ThreadGroup.ramp_time">1</stringProp> <boolProp name="ThreadGroup.scheduler">false</boolProp> <stringProp name="ThreadGroup.duration"/> <stringProp name="ThreadGroup.delay"/> </ThreadGroup> <hashTree> <ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true"> <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="HTTPSampler.domain">${host}</stringProp> <stringProp name="HTTPSampler.port">${port}</stringProp> <stringProp name="HTTPSampler.protocol">http</stringProp> <stringProp name="HTTPSampler.contentEncoding"/> <stringProp name="HTTPSampler.path"/> </ConfigTestElement> <hashTree/> <ResultCollector guiclass="SimpleDataWriter" testclass="ResultCollector" testname="results.jtl" enabled="true"> <boolProp name="ResultCollector.error_logging">false</boolProp> <objProp> <name>saveConfig</name> <value class="SampleSaveConfiguration"> <time>true</time> <latency>true</latency> <timestamp>true</timestamp> <success>true</success> <label>true</label> <code>true</code> <message>true</message> <threadName>true</threadName> <dataType>true</dataType> <encoding>false</encoding> <assertions>true</assertions> <subresults>false</subresults> <responseData>false</responseData> <samplerData>false</samplerData> <xml>false</xml> <fieldNames>true</fieldNames> <responseHeaders>false</responseHeaders> <requestHeaders>false</requestHeaders> <responseDataOnError>false</responseDataOnError> <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage> <assertionsResultsToSave>0</assertionsResultsToSave> <bytes>true</bytes> <sentBytes>true</sentBytes> <url>true</url> <threadCounts>true</threadCounts> <idleTime>true</idleTime> <connectTime>true</connectTime> </value> </objProp> <stringProp name="filename">results.jtl</stringProp> </ResultCollector> <hashTree/> <ConstantThroughputTimer guiclass="TestBeanGUI" testclass="ConstantThroughputTimer" testname="two samples per second" enabled="true"> <stringProp name="calcMode">1</stringProp> <stringProp name="throughput">120</stringProp> </ConstantThroughputTimer> <hashTree/> <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="health" enabled="true"> <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="HTTPSampler.domain">${host}</stringProp> <stringProp name="HTTPSampler.port">${port}</stringProp> <stringProp name="HTTPSampler.protocol">http</stringProp> <stringProp name="HTTPSampler.contentEncoding"/> <stringProp name="HTTPSampler.path">/health</stringProp> <stringProp name="HTTPSampler.method">GET</stringProp> <boolProp name="HTTPSampler.follow_redirects">true</boolProp> <boolProp name="HTTPSampler.auto_redirects">false</boolProp> <boolProp name="HTTPSampler.use_keepalive">true</boolProp> <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp> <stringProp name="HTTPSampler.connect_timeout"/> <stringProp name="HTTPSampler.response_timeout"/> </HTTPSamplerProxy> <hashTree/> </hashTree> </hashTree> </hashTree> </jmeterTestPlan>

jmeter -n -t paced.jmx -l paced.jtl -j paced.log

python3 span.py 900 paced.jtl

at least 900 ms

The timer is the one element that changes a load test from "as fast as possible" into a stated rate. The percentile lesson reads what that rate did to the answers.

Related

Автор статьи: Arthur Isaev

Изображение баннера