Definition
Response time is the time from a stated start event, such as the arrival of a request, to a stated end event, such as the delivery of its result. Execution time is the part of that interval during which a processor actually runs the instructions that serve the request. The rest is time spent waiting (in a queue, for the processor, or for a lock or a device) and time spent delivering the result. Execution time measures the work a request needs; response time measures what the requester experiences.
The idea
One worker thread serves a queue of requests in arrival order. A job that writes a large file and occupies the worker for 200 milliseconds (ms) has just started when a status request arrives. The status request needs 1 ms of processor work. Its requester nevertheless waits about 200 ms before the worker even starts it, so the requester sees a response time of more than 200 ms for a request whose execution time is 1 ms. Making the status code twice as fast would save half a millisecond of those 200.
A supermarket checkout is a fair picture. Scanning one shopper’s items takes one minute: that is the execution time. Standing in line behind five full trolleys takes ten minutes: that is waiting. The shopper experiences eleven minutes, and a faster scanner shortens only the one-minute part. The picture stops being accurate in one respect. A cashier finishes one shopper before serving the next, but a preemptive scheduler, which may take the processor away from a running thread at any moment to run a more urgent one, can stop a request in the middle of its service. On a computer, waiting can therefore also happen between the start and the end of the execution.
How it works
First fix the two endpoints: which event starts the measurement, which event ends it, and which clock is read. Then divide the interval between them into parts that do not overlap, so that every instant belongs to exactly one part:
Queueing delay §14.38: the request waits in a queue until a worker takes it.
Scheduling delay: the thread serving the request is ready to run, but the processor runs something else, such as a higher-priority thread or an interrupt handler §6.12. Time lost to preemption falls here.
Resource wait: the thread is blocked, meaning it cannot run until something happens elsewhere, for example until another thread releases a lock or a device finishes writing to flash memory §6.6.
Execution: the processor runs the request’s own instructions. Execution time is also called processor time or CPU time §18.1.5.
Delivery: the result travels back to the requester, for example as a network message or as a callback §14.22, a function the requester registered to be called with the result.
The two quantities are measured with different instruments. Response time is the difference of two readings of a clock that runs all the time, such as a monotonic clock §9.35 or a free-running cycle counter, a hardware register that counts processor clock cycles. Execution time needs help from the operating system. Many operating systems keep per-thread runtime statistics: at every context switch, the moment the processor stops running one thread and starts another, the kernel reads a timer or cycle counter and adds the length of the interval that just ended to the total of the thread that was running. The change in a thread’s total between two points is the time that thread spent running between them. If the thread is preempted between the two points, the elapsed cycle count includes the preemption and the thread’s runtime total does not. Operating systems differ in whether time spent in interrupt handlers is charged to the thread that was interrupted.
Each part responds to a different change. A faster algorithm §14.1 reduces execution time. Another worker, or a higher priority for the request, reduces queueing delay or scheduling delay. Shorter critical sections §9.4, the stretches of code that run while holding a lock, reduce resource waits. Measure the part that the planned change is supposed to improve.
Worked example
Take a hypothetical trace of the status request from the idea above, in milliseconds: queueing delay 200, scheduling delay 3, resource wait 4, execution 1, delivery 2. The response time is the sum: , , , ms. The execution time is 1 ms, less than 0.5% of the response time, because .
Halving the execution time to 0.5 ms gives ms, a drop of , that is 0.24%. Halving the backlog instead, so that the queueing delay is 100 ms, gives ms, a drop of almost half.
The same distinction in cycles. A processor runs at 64,000,000 cycles per second. A callback starts on its thread when a cycle counter reads 1,000,000 and ends when the counter reads 1,640,000: an elapsed count of cycles, which is s, or 10 ms. Over the same interval the runtime total of that thread grows from 5,000,000 to 5,064,000 cycles: cycles, which is s, or 1 ms. The thread ran for 1 ms of the 10 ms. For the other ms it was preempted or waiting. The elapsed count of 640,000 cycles includes the preemption; the runtime count of 64,000 cycles does not.
Stated precisely
Let be the response time of one request, measured between its stated start and end events. Let be its queueing delay, its scheduling delay, its resource wait, its execution time and its delivery time, all in the same unit and defined so that every instant of the interval is counted in exactly one of them. Then and therefore : a response time is never shorter than the execution time it contains.
Queueing delay under a backlog. Suppose one worker serves requests one at a time in arrival order, never sits idle while a request waits, and starts the next request as soon as it finishes the previous one. Call a job’s service time the whole time it occupies the worker, including any waits while it holds the worker. A request arrives while the worker serves a job with remaining service time , and jobs with service times wait ahead of it in the queue. Its queueing delay is Every job added to the backlog ahead of the request adds its whole service time to and nothing to .
The parts add for one request, not for summaries over many requests. The median §11.13 of the response times is in general not the sum of the medians of the parts, because the request with the median queueing delay need not be the request with the median execution time.
Limits and common mistakes
A short execution time does not imply a short response time, and a faster execution does not imply a comparably faster response. In the example, a 50% cut in execution time gave a 0.24% cut in response time.
One long job at the front of a shared queue delays every request behind it, however short those requests are. This is head-of-line blocking §9.19.
The mean response time, a tail percentile §11.16 such as the 99th percentile, and a worst-case bound are different properties. A queue that is usually empty gives a small median response time and can still give a large worst case.
The endpoints change the number. Measured from the moment the requester sent the request, response time includes the network or radio transfer. Measured from the moment the worker took the request from the queue, it hides the queueing delay entirely. State both endpoints and the clock with every number.
The rule for overlaps must be stated. A thread that waits for a lock and, once the lock is free, waits again for the processor spends the first wait in resource wait and the second in scheduling delay. Without a stated rule, a measurement can count the same time twice or not at all.
Requests that fail or time out also have response times. Leaving them out of a summary makes the summary look better than what requesters experienced.
Execution time is not a constant either: it changes with the input, the state of the processor’s caches and the clock frequency. The worst-case execution time §9.33 entry treats bounds on it.
Reading a clock or a counter costs time, and that cost lands inside the measured interval. This instrumentation overhead §11.32 is small for one register read and larger for a call into the operating system.
Questions answered
Which component of response time grows under a shared worker backlog?
The queueing delay grows: the time a request spends in the queue before the shared worker starts serving it. When one worker serves requests one at a time in arrival order, a request’s queueing delay equals the remaining service time of the job the worker is busy with plus the full service times of all jobs queued ahead of it, so every extra job in the backlog adds its whole service time. The request’s execution time, the time the processor spends running the request’s own instructions, does not change, because the backlog does not change the work the request needs.
Two other parts of response time can grow as a side effect. If backlog jobs hold a lock or a device that the request also needs, the request’s resource wait grows. If the backlog runs on a higher-priority thread, the request’s thread waits longer for the processor, which counts as scheduling delay. Speeding up the request’s own code barely helps; more workers, a separate queue for short requests, or a shorter backlog helps.
Related entries: Head-of-line blocking (§9.19), Worst-case execution time (WCET) (§9.33), Median (§11.13), Queueing delay (§14.38), Latency distribution and tail (§11.36), Wall time, CPU time, and instruction count (§18.1.5).