Best Python code snippet using locust
stats.py
Source:stats.py  
...814            self.exceptions_csv_filehandle.seek((self.exceptions_csv_data_start))815            self._exceptions_data_rows(self.exceptions_csv_writer)816            self.exceptions_csv_filehandle.truncate()817            if now - last_flush_time > CSV_STATS_FLUSH_INTERVAL_SEC:818                self.requests_flush()819                self.stats_history_flush()820                self.failures_flush()821                self.exceptions_flush()822                last_flush_time = now823            gevent.sleep(CSV_STATS_INTERVAL_SEC)824    def _stats_history_data_rows(self, csv_writer, now):825        """826        Write CSV rows with the *current* stats. By default only includes the827        Aggregated stats entry, but if self.full_history is set to True, a row for each entry will828        will be included.829        Note that this method differs from the other methods as it appends time-stamped data to the file, whereas the other methods overwrites the data.830        """831        stats = self.environment.stats832        timestamp = int(now)833        stats_entries = []834        if self.full_history:835            stats_entries = sort_stats(stats.entries)836        for stats_entry in chain(stats_entries, [stats.total]):837            csv_writer.writerow(838                chain(839                    (840                        timestamp,841                        self.environment.runner.user_count,842                        stats_entry.method or "",843                        stats_entry.name,844                        f"{stats_entry.current_rps:2f}",845                        f"{stats_entry.current_fail_per_sec:2f}",846                    ),847                    self._percentile_fields(stats_entry),848                    (849                        stats_entry.num_requests,850                        stats_entry.num_failures,851                        stats_entry.median_response_time,852                        stats_entry.avg_response_time,853                        stats_entry.min_response_time or 0,854                        stats_entry.max_response_time,855                        stats_entry.avg_content_length,856                    ),857                )858            )859    def requests_flush(self):860        self.requests_csv_filehandle.flush()861    def stats_history_flush(self):862        self.stats_history_csv_filehandle.flush()863    def failures_flush(self):864        self.failures_csv_filehandle.flush()865    def exceptions_flush(self):866        self.exceptions_csv_filehandle.flush()867    def close_files(self):868        self.requests_csv_filehandle.close()869        self.stats_history_csv_filehandle.close()870        self.failures_csv_filehandle.close()871        self.exceptions_csv_filehandle.close()872    def stats_history_file_name(self):873        return self.base_filepath + "_stats_history.csv"Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
