{"record":{"id":"84841be66dd852fe","repo":"locustio/locust","slug":"statsentry-use-response-times-cache-must-be-set-to","errorCode":null,"errorMessage":"StatsEntry.use_response_times_cache must be set to True to calculate the _current_ response time percentile","messagePattern":"StatsEntry\\.use_response_times_cache must be set to True to calculate the _current_ response time percentile","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"locust/stats.py","lineNumber":620,"sourceCode":"        Get the response time that a certain number of percent of the requests\n        finished within.\n\n        Percent specified in range: 0.0 - 1.0\n        \"\"\"\n        # response_times only holds non-None samples, so None (async) requests\n        # must be excluded from the denominator, just like in avg/median\n        return calculate_response_time_percentile(\n            self.response_times, self.num_requests - self.num_none_requests, percent\n        )\n\n    def get_current_response_time_percentile(self, percent: float) -> int | None:\n        \"\"\"\n        Calculate the *current* response time for a certain percentile. We use a sliding\n        window of (approximately) the last 10 seconds (specified by CURRENT_RESPONSE_TIME_PERCENTILE_WINDOW)\n        when calculating this.\n        \"\"\"\n        if not self.use_response_times_cache:\n            raise ValueError(\n                \"StatsEntry.use_response_times_cache must be set to True to calculate the _current_ response time percentile\"\n            )\n        # First, we want to determine which of the cached response_times dicts we should\n        # use to get response_times for approximately 10 seconds ago.\n        t = int(time.time())\n        # Since we can't be sure that the cache contains an entry for every second.\n        # We'll construct a list of timestamps which we consider acceptable keys to be used\n        # when trying to fetch the cached response_times. We construct this list in such a way\n        # that it's ordered by preference by starting to add t-10, then t-11, t-9, t-12, t-8,\n        # and so on\n        acceptable_timestamps: list[int] = []\n        acceptable_timestamps.append(t - CURRENT_RESPONSE_TIME_PERCENTILE_WINDOW)\n        for i in range(1, 9):\n            acceptable_timestamps.append(t - CURRENT_RESPONSE_TIME_PERCENTILE_WINDOW - i)\n            acceptable_timestamps.append(t - CURRENT_RESPONSE_TIME_PERCENTILE_WINDOW + i)\n\n        cached: CachedResponseTimes | None = None\n        if self.response_times_cache is not None:","sourceCodeStart":602,"sourceCodeEnd":638,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/stats.py#L602-L638","documentation":"get_current_response_time_percentile() needs the sliding response-times cache to answer 'what was the p95 over the last ~10 seconds'. If StatsEntry.use_response_times_cache is False (the default for entries that only track aggregate history), the current percentile cannot be computed, so the method raises ValueError.","triggerScenarios":"Calling entry.get_current_response_time_percentile(p) on a StatsEntry whose use_response_times_cache was not set to True at creation; this happens in update_stats_history/_percentile_fields if entries are configured without the cache.","commonSituations":"Custom stats reporters or plugins reading current percentiles on entries they created manually; code building StatsEntry without passing use_response_times_cache=True; versions where custom entries previously worked with aggregate-only data.","solutions":["Create the StatsEntry with use_response_times_cache=True before calling get_current_response_time_percentile()","Use get_response_time_percentile() (aggregate, whole-run) instead if you don't need the sliding window","Enable the cache globally by configuring stats.CURRENT_RESPONSE_TIME_PERCENTILE settings in your custom entry setup"],"exampleFix":"// before\nentry = env.stats.get(name, method)\nentry.get_current_response_time_percentile(0.95)\n// after\nentry = env.stats.get(name, method, use_response_times_cache=True)\nentry.get_current_response_time_percentile(0.95)","handlingStrategy":"validation","validationCode":"if not entry.use_response_times_cache:\n    raise ValueError(\"enable use_response_times_cache before reading current percentiles\")","typeGuard":null,"tryCatchPattern":"try:\n    p = entry.get_current_response_time_percentile(0.95)\nexcept ValueError:\n    p = entry.get_response_time_percentile(0.95)  # aggregate fallback","preventionTips":["Always construct StatsEntry with use_response_times_cache=True when you need sliding-window percentiles","Document which stats entries support current percentiles","Prefer aggregate percentiles unless the 10s window is required"],"tags":["locust","stats","configuration","response-times"],"backgroundTag":"stats-cache-not-enabled","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}