What is the Benchmark class in CodeIgniter 4?
Why Interviewers Ask This
Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.
Answer
The Benchmark class in CI4 measures elapsed time between two points in your code, helping identify performance bottlenecks. The timer is automatically started when CI4 begins processing and stopped at the end, giving you the total execution time. Mark custom timing points: $benchmark = service("timer"). Start a timer: $benchmark->start("my_process"). Stop it: $benchmark->stop("my_process"). Get elapsed time: $benchmark->getElapsedTime("my_process"). In development mode, the Debug Toolbar automatically displays all timer data in the Timeline tab. Check memory usage: $benchmark->getMemoryUsage(). The Benchmark class is automatically shared via the Services class — the same instance is used throughout the request. Use it to profile specific database queries, external API calls, or complex computations to identify where time is being spent in your application.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong CodeIgniter candidates.