Best Python code snippet using locust
stats.py
Source:stats.py
...751 ]752 )753 def exceptions_csv(self, csv_writer):754 csv_writer.writerow(self.exceptions_columns)755 self._exceptions_data_rows(csv_writer)756 def _exceptions_data_rows(self, csv_writer):757 for exc in self.environment.runner.exceptions.values():758 csv_writer.writerow([exc["count"], exc["msg"], exc["traceback"], ", ".join(exc["nodes"])])759class StatsCSVFileWriter(StatsCSV):760 """Write statistics to to CSV files"""761 def __init__(self, environment, percentiles_to_report, base_filepath, full_history=False):762 super().__init__(environment, percentiles_to_report)763 self.base_filepath = base_filepath764 self.full_history = full_history765 self.requests_csv_filehandle = open(self.base_filepath + "_stats.csv", "w")766 self.requests_csv_writer = csv.writer(self.requests_csv_filehandle)767 self.stats_history_csv_filehandle = open(self.stats_history_file_name(), "w")768 self.stats_history_csv_writer = csv.writer(self.stats_history_csv_filehandle)769 self.failures_csv_filehandle = open(self.base_filepath + "_failures.csv", "w")770 self.failures_csv_writer = csv.writer(self.failures_csv_filehandle)771 self.failures_csv_data_start = 0772 self.exceptions_csv_filehandle = open(self.base_filepath + "_exceptions.csv", "w")773 self.exceptions_csv_writer = csv.writer(self.exceptions_csv_filehandle)774 self.exceptions_csv_data_start = 0775 self.stats_history_csv_columns = [776 "Timestamp",777 "User Count",778 "Type",779 "Name",780 "Requests/s",781 "Failures/s",782 *get_readable_percentiles(self.percentiles_to_report),783 "Total Request Count",784 "Total Failure Count",785 "Total Median Response Time",786 "Total Average Response Time",787 "Total Min Response Time",788 "Total Max Response Time",789 "Total Average Content Size",790 ]791 def __call__(self):792 self.stats_writer()793 def stats_writer(self):794 """Writes all the csv files for the locust run."""795 # Write header row for all files and save position for non-append files796 self.requests_csv_writer.writerow(self.requests_csv_columns)797 requests_csv_data_start = self.requests_csv_filehandle.tell()798 self.stats_history_csv_writer.writerow(self.stats_history_csv_columns)799 self.failures_csv_writer.writerow(self.failures_columns)800 self.failures_csv_data_start = self.failures_csv_filehandle.tell()801 self.exceptions_csv_writer.writerow(self.exceptions_columns)802 self.exceptions_csv_data_start = self.exceptions_csv_filehandle.tell()803 # Continuously write date rows for all files804 last_flush_time = 0805 while True:806 now = time.time()807 self.requests_csv_filehandle.seek(requests_csv_data_start)808 self._requests_data_rows(self.requests_csv_writer)809 self.requests_csv_filehandle.truncate()810 self._stats_history_data_rows(self.stats_history_csv_writer, now)811 self.failures_csv_filehandle.seek(self.failures_csv_data_start)812 self._failures_data_rows(self.failures_csv_writer)813 self.failures_csv_filehandle.truncate()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....
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!!