-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_comparison.py
More file actions
405 lines (329 loc) · 16.1 KB
/
run_comparison.py
File metadata and controls
405 lines (329 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Python과 Go 구현의 성능을 비교하는 스크립트
"""
import io
import json
import logging
import os
import re
import subprocess
import sys
from datetime import datetime
logger = None
# 로깅 설정
def setup_logger(result_dir=None):
"""로깅 설정 함수"""
logger = logging.getLogger('run_comparison')
logger.setLevel(logging.INFO)
# 기존 핸들러 제거
for handler in logger.handlers[:]:
logger.removeHandler(handler)
# 콘솔 핸들러
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# 파일 핸들러
log_file = 'run_comparison.log'
if result_dir:
log_file = os.path.join(result_dir, 'run_comparison.log')
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.INFO)
# 포맷 설정
formatter = logging.Formatter('[run_comparison.py][%(asctime)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
# 핸들러 추가
logger.addHandler(console_handler)
logger.addHandler(file_handler)
return logger
# 로거 초기화 (처음에는 기본 설정으로)
def parse_python_output(output):
"""Python/Go 출력에서 통계 파싱"""
lines = output.strip().split('\n')
results = {}
current_indicator = None
for i, line in enumerate(lines):
# 지표 이름 찾기
if '---' in line:
match = re.search(r'--- (\w+) ---', line)
if match:
current_indicator = match.group(1).lower()
logger.info(f"지표 발견: {current_indicator}")
results[current_indicator] = {
'execution_time': {},
'cpu_usage': {},
'memory_usage': {}
}
# 실행 시간 통계
if 'Execution Time Statistics:' in line and current_indicator:
logger.info(f"{current_indicator} 실행 시간 통계 파싱")
for j in range(1, 6):
if i + j < len(lines):
stat_line = lines[i + j]
if 'Min:' in stat_line:
results[current_indicator]['execution_time']['min'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Max:' in stat_line:
results[current_indicator]['execution_time']['max'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Avg:' in stat_line:
results[current_indicator]['execution_time']['avg'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'P95:' in stat_line:
results[current_indicator]['execution_time']['p95'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'P99:' in stat_line:
results[current_indicator]['execution_time']['p99'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
# CPU 사용량 - System CPU Usage 또는 단순히 CPU Usage
if ('System CPU Usage:' in line or 'CPU Usage:' in line) and current_indicator:
logger.info(f"{current_indicator} CPU 사용량 파싱")
for j in range(1, 4):
if i + j < len(lines):
stat_line = lines[i + j]
if 'Min:' in stat_line:
results[current_indicator]['cpu_usage']['min'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Max:' in stat_line:
results[current_indicator]['cpu_usage']['max'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Avg:' in stat_line:
results[current_indicator]['cpu_usage']['avg'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
# 메모리 사용량
if 'Memory Usage:' in line and current_indicator:
logger.info(f"{current_indicator} 메모리 사용량 파싱")
for j in range(1, 4):
if i + j < len(lines):
stat_line = lines[i + j]
if 'Min:' in stat_line:
results[current_indicator]['memory_usage']['min'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Max:' in stat_line:
results[current_indicator]['memory_usage']['max'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Avg:' in stat_line:
results[current_indicator]['memory_usage']['avg'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
logger.info("Python 출력 파싱 완료")
return results
def parse_go_output(output):
"""Go 출력에서 통계 파싱 (Python과 동일한 형식)"""
logger.info("Go 출력 파싱 시작")
lines = output.strip().split('\n')
results = {}
current_indicator = None
for i, line in enumerate(lines):
# 지표 이름 찾기 (--- adx ---, --- atr ---, --- bollinger ---, --- fibonacci ---)
if '---' in line:
match = re.search(r'--- (\w+) ---', line)
if match:
current_indicator = match.group(1).lower()
logger.info(f"지표 발견: {current_indicator}")
results[current_indicator] = {
'execution_time': {},
'cpu_usage': {},
'memory_usage': {}
}
# 실행 시간 통계
if 'Execution Time Statistics:' in line and current_indicator:
logger.info(f"{current_indicator} 실행 시간 통계 파싱")
for j in range(1, 6):
if i + j < len(lines):
stat_line = lines[i + j]
if 'Min:' in stat_line:
results[current_indicator]['execution_time']['min'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Max:' in stat_line:
results[current_indicator]['execution_time']['max'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Avg:' in stat_line:
results[current_indicator]['execution_time']['avg'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'P95:' in stat_line:
results[current_indicator]['execution_time']['p95'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'P99:' in stat_line:
results[current_indicator]['execution_time']['p99'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
# CPU 사용량 - Go는 System CPU Usage와 Process CPU Usage를 모두 출력함
if 'System CPU Usage:' in line and current_indicator:
logger.info(f"{current_indicator} CPU 사용량 파싱")
for j in range(1, 4):
if i + j < len(lines):
stat_line = lines[i + j]
if 'Min:' in stat_line:
results[current_indicator]['cpu_usage']['min'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Max:' in stat_line:
results[current_indicator]['cpu_usage']['max'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Avg:' in stat_line:
results[current_indicator]['cpu_usage']['avg'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
# 메모리 사용량
if 'Memory Usage:' in line and current_indicator:
logger.info(f"{current_indicator} 메모리 사용량 파싱")
for j in range(1, 4):
if i + j < len(lines):
stat_line = lines[i + j]
if 'Min:' in stat_line:
results[current_indicator]['memory_usage']['min'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Max:' in stat_line:
results[current_indicator]['memory_usage']['max'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
elif 'Avg:' in stat_line:
results[current_indicator]['memory_usage']['avg'] = float(re.search(r'(\d+\.\d+)', stat_line).group(1))
logger.info("Go 출력 파싱 완료")
return results
def run_benchmark(command):
"""벤치마크 실행"""
logger.info(f"벤치마크 실행: {' '.join(command)}")
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
if process.returncode != 0:
logger.error(f"벤치마크 실행 중 오류 발생: {stderr}")
return None
logger.info("벤치마크 실행 완료")
return stdout
def print_comparison(python_results, go_results, result_dir=None):
"""결과 비교 출력"""
logger.info("결과 비교 출력 시작")
# 출력 내용을 저장할 문자열 버퍼
output = io.StringIO()
# 출력 내용을 버퍼와 콘솔에 동시에 작성하는 함수
def print_both(text):
print(text)
output.write(text + "\n")
print_both("\n" + "=" * 80)
print_both(" " * 25 + "성능 비교 결과")
print_both("=" * 80)
indicators = ['adx', 'atr', 'bollinger', 'fibonacci']
for indicator in indicators:
if indicator not in python_results or indicator not in go_results:
logger.warning(f"{indicator} 지표가 결과에 없습니다")
continue
logger.info(f"{indicator.upper()} 지표 비교 출력")
print_both(f"\n{indicator.upper()} 지표 비교:")
print_both("-" * 60)
# 실행 시간 비교
print_both("\n실행 시간 (초):")
print_both(f"{'메트릭':<10} {'Python':>15} {'Go':>15} {'성능 향상':>15}")
print_both("-" * 60)
metrics = ['min', 'max', 'avg', 'p95', 'p99']
for metric in metrics:
py_val = python_results[indicator]['execution_time'].get(metric, 0)
go_val = go_results[indicator]['execution_time'].get(metric, 0)
if go_val > 0:
speedup = py_val / go_val
print_both(f"{metric.upper():<10} {py_val:>15.4f} {go_val:>15.4f} {speedup:>14.2f}x")
# CPU 사용량 비교
print_both("\nCPU 사용률 (%):")
print_both(f"{'메트릭':<10} {'Python':>15} {'Go':>15}")
print_both("-" * 40)
for metric in ['min', 'max', 'avg']:
py_val = python_results[indicator]['cpu_usage'].get(metric, 0)
go_val = go_results[indicator]['cpu_usage'].get(metric, 0)
print_both(f"{metric.upper():<10} {py_val:>15.2f} {go_val:>15.2f}")
# 메모리 사용량 비교
print_both("\n메모리 사용량 (MB):")
print_both(f"{'메트릭':<10} {'Python':>15} {'Go':>15}")
print_both("-" * 40)
for metric in ['min', 'max', 'avg']:
py_val = python_results[indicator]['memory_usage'].get(metric, 0)
go_val = go_results[indicator]['memory_usage'].get(metric, 0)
print_both(f"{metric.upper():<10} {py_val:>15.2f} {go_val:>15.2f}")
logger.info("결과 비교 출력 완료")
# 출력 내용을 파일로 저장
if result_dir:
comparison_file = os.path.join(result_dir, 'comparison_results.txt')
with open(comparison_file, 'w', encoding='utf-8') as f:
f.write(output.getvalue())
logger.info(f"비교 결과가 {comparison_file}에 저장되었습니다")
print(f"\n비교 결과가 {comparison_file}에 저장되었습니다")
return output.getvalue()
def save_results(python_results, go_results, result_dir):
"""결과를 JSON 파일로 저장"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(result_dir, f"benchmark_results_{timestamp}.json")
logger.info(f"결과를 {filename}에 저장 시작")
results = {
"timestamp": timestamp,
"python": python_results,
"go": go_results
}
with open(filename, 'w') as f:
json.dump(results, f, indent=2)
logger.info(f"결과가 {filename}에 저장되었습니다")
print(f"\n결과가 {filename}에 저장되었습니다.")
def save_raw_output(output, filename, result_dir):
"""원시 출력을 파일로 저장"""
file_path = os.path.join(result_dir, filename)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(output)
logger.info(f"원시 출력이 {file_path}에 저장되었습니다")
return file_path
def main(logger):
logger.info("Python vs Go 벤치마크 비교 시작")
print("Python vs Go 기술 분석 벤치마크 비교")
print("=" * 80)
# 결과 디렉토리 생성
timestamp = datetime.now().strftime("%y%m%d_%H%M%S")
result_dir = f"result_{timestamp}"
os.makedirs(result_dir, exist_ok=True)
logger.info(f"결과 디렉토리 생성: {result_dir}")
print(f"결과 디렉토리 생성: {result_dir}")
# 로거 재설정 (결과 디렉토리에 로그 저장)
logger = setup_logger(result_dir)
# 명령줄 인자 파싱
iterations = 100
workers = None
parallel = True
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if arg.startswith('--iterations='):
iterations = int(arg.split('=')[1])
logger.info(f"반복 횟수 설정: {iterations}")
elif arg.startswith('--workers='):
workers = int(arg.split('=')[1])
logger.info(f"워커 수 설정: {workers}")
elif arg == '--traditional':
parallel = False
logger.info("전통적 순차 처리 모드 설정")
# Python 벤치마크 실행
logger.info("Python 벤치마크 실행 시작")
print("\nPython 벤치마크 실행 중...")
python_cmd = ['python', 'benchmark.py', f'--iterations={iterations}']
if workers:
python_cmd.append(f'--workers={workers}')
if not parallel:
python_cmd.append('--traditional')
python_output = run_benchmark(python_cmd)
if python_output is None:
logger.error("Python 벤치마크 실행 실패")
print("Python 벤치마크 실행 실패")
return
# Python 출력 저장
python_output_file = save_raw_output(python_output, "python_output.txt", result_dir)
logger.info(f"Python 출력이 {python_output_file}에 저장되었습니다")
# Go 벤치마크 실행
logger.info("Go 벤치마크 실행 시작")
print("\nGo 벤치마크 실행 중...")
# Go 빌드 확인
if not os.path.exists('./benchmark'):
logger.info("Go 바이너리 빌드 시작")
print("Go 바이너리를 빌드합니다...")
build_result = subprocess.run(['go', 'build', 'benchmark.go'], capture_output=True)
if build_result.returncode != 0:
error_msg = build_result.stderr.decode()
logger.error(f"Go 빌드 실패: {error_msg}")
print(f"Go 빌드 실패: {error_msg}")
return
logger.info("Go 바이너리 빌드 완료")
go_cmd = ['./benchmark', f'-iterations={iterations}']
if workers:
go_cmd.append(f'-workers={workers}')
if not parallel:
go_cmd.append('-traditional')
go_output = run_benchmark(go_cmd)
if go_output is None:
logger.error("Go 벤치마크 실행 실패")
print("Go 벤치마크 실행 실패")
return
# Go 출력 저장
go_output_file = save_raw_output(go_output, "go_output.txt", result_dir)
logger.info(f"Go 출력이 {go_output_file}에 저장되었습니다")
# 결과 파싱
logger.info("벤치마크 결과 파싱 시작")
python_results = parse_python_output(python_output)
go_results = parse_go_output(go_output)
# 결과 비교 출력
comparison_output = print_comparison(python_results, go_results, result_dir)
# 결과 저장
save_results(python_results, go_results, result_dir)
logger.info("벤치마크 비교 완료")
print(f"\n모든 결과가 {result_dir} 디렉토리에 저장되었습니다.")
if __name__ == "__main__":
logger = setup_logger()
main(logger)