locust-demo/Library/LocustBasic.py

26 lines
803 B
Python
Raw Normal View History

2023-03-21 01:18:45 +08:00
from locust import task, FastHttpUser, constant, between
from locust.main import main
import csv
import sys
import os
class CsvDataReader:
__point = 0
def __init__(self, filename: str, encoding=None):
filename = '%s%s%s/%s' % (os.path.dirname(os.path.dirname(__file__)), os.sep, 'Data', filename)
try:
open(filename).read(4096)
except UnicodeDecodeError:
encoding = 'utf-8'
self.csv_data = [row for row in csv.reader(open(filename, encoding=encoding))]
self.csv_data_length = len(self.csv_data)
if self.csv_data_length == 0:
raise Exception('No data in file %s.' % filename)
def read(self):
data = self.csv_data[self.__point % self.csv_data_length]
self.__point += 1
return data