Skip to content

Commit

Permalink
add performance test
Browse files Browse the repository at this point in the history
  • Loading branch information
guxi committed Mar 27, 2017
1 parent 2a0c0a2 commit 45aff16
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
from rqalpha import run
from rqalpha.utils.logger import system_log


TEST_DIR = os.path.abspath("./tests/")
TEST_OUT = os.path.abspath("./tests/outs/")


pd.set_option("display.width", 160)


Expand Down Expand Up @@ -160,7 +158,7 @@ def test_api():

base_api_config = {
"base": {
"securities": "stock",
"strategy_type": "stock",
"start_date": "2016-12-01",
"end_date": "2016-12-31",
"frequency": "1d",
Expand All @@ -180,7 +178,7 @@ def test_api():

stock_api_config = {
"base": {
"securities": "stock",
"strategy_type": "stock",
"start_date": "2016-03-07",
"end_date": "2016-03-08",
"frequency": "1d",
Expand All @@ -200,7 +198,7 @@ def test_api():

future_api_config = {
"base": {
"securities": "future",
"strategy_type": "future",
"start_date": "2016-03-07",
"end_date": "2016-03-08",
"frequency": "1d",
Expand Down Expand Up @@ -257,13 +255,48 @@ def test_strategy():
get_test_files()


def write_csv(path, fields):
old_test_times = []
if not os.path.exists(path):
with open(path, 'w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fields)
writer.writeheader()
with open(path) as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
old_test_times.append(row)

if performance_path is None:
if len(old_test_times) != 0 and time_spend > float(old_test_times[-1]["time_spend"]) * 1.1:
system_log.error("代码咋写的,太慢了!")
system_log.error("上次测试用例执行的总时长为:" + old_test_times[-1]["time_spend"])
system_log.error("本次测试用例执行的总时长增长为: " + str(time_spend))
else:
with open(path, 'a') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fields)
writer.writerow({'date_time': end_time, 'time_spend': time_spend})
else:
if 0 < len(old_test_times) < 5 and time_spend > float(sum(float(i['time_spend']) for i in old_test_times))/len(old_test_times) * 1.1:
raise RuntimeError('Performance regresses!')
elif len(old_test_times) >= 5 and time_spend > float(sum(float(i['time_spend']) for i in old_test_times[-5:]))/5 * 1.1:
raise RuntimeError('Performance regresses!')
else:
with open(path, 'a') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fields)
writer.writerow({'date_time': end_time, 'time_spend': time_spend})


if __name__ == '__main__':
if is_enable_coverage():
print("enable coverage")
cov = coverage.Coverage()
cov.start()

performance_path = None
field_names = ['date_time', 'time_spend']

start_time = datetime.now()

if len(sys.argv) >= 2:
if sys.argv[1] == 'mod':
test_api()
Expand All @@ -273,6 +306,14 @@ def test_strategy():
test_strategy()
end_time = datetime.now()

elif sys.argv[1] == 'performance':
test_api()
test_strategy()
end_time = datetime.now()
performance_path = sys.argv[2]
time_spend = (end_time - start_time).total_seconds()
write_csv(performance_path, field_names)

else:
target_file = sys.argv[1]
get_test_files(target_file)
Expand All @@ -284,25 +325,8 @@ def test_strategy():
end_time = datetime.now()
if error_count == 0:
time_csv_file_path = os.path.join(TEST_OUT, "time.csv")
field_names = ['date_time', 'time_spend']
old_test_times = []
time_spend = (end_time - start_time).total_seconds()
if not os.path.exists(time_csv_file_path):
with open(time_csv_file_path, 'w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=field_names)
writer.writeheader()
with open(time_csv_file_path) as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
old_test_times.append(row)
if len(old_test_times) != 0 and time_spend > float(old_test_times[-1]["time_spend"]) * 1.1:
system_log.error(u"代码咋写的,太慢了!")
system_log.error(u"上次测试用例执行的总时长为:" + old_test_times[-1]["time_spend"])
system_log.error(u"本次测试用例执行的总时长增长为: " + str(time_spend))
else:
with open(time_csv_file_path, 'a') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=field_names)
writer.writerow({'date_time': end_time, 'time_spend': time_spend})
write_csv(time_csv_file_path, field_names)

else:
print('Failed!')
Expand Down

0 comments on commit 45aff16

Please sign in to comment.