posted by 구로공돌이 2025. 4. 23. 09:18

import requests
import csv
import time
from datetime import datetime

# API 설정
url = 'https://your-api-endpoint.com/path'  # 실제 API 주소로 변경
params = {
    'param1': 'value1',
    'param2': 'value2'
}
headers = {
    'Authorization': 'Bearer your-token',
    'X-Custom-Header1': 'value1',
    'X-Custom-Header2': 'value2'
}

# CSV 파일명
csv_file = 'api_response_log.csv'

# CSV 파일에 헤더 작성 (파일 없을 경우)
try:
    with open(csv_file, 'x', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(['timestamp', 'status_code', 'elapsed_time_sec', 'error'])
except FileExistsError:
    pass

# 무한 루프 - 3분 간격으로 체크
while True:
    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    try:
        start_time = time.time()
        response = requests.get(url, params=params, headers=headers, timeout=10)
        elapsed_time = round(time.time() - start_time, 3)
        status_code = response.status_code
        error = ''
    except Exception as e:
        elapsed_time = -1
        status_code = 'ERROR'
        error = str(e)

    # CSV에 기록
    with open(csv_file, 'a', newline='') as f:
        writer = csv.writer(f)
        writer.writerow([timestamp, status_code, elapsed_time, error])

    print(f"[{timestamp}] status: {status_code}, time: {elapsed_time}s")
    
    time.sleep(180)  # 3분 대기

posted by 구로공돌이 2025. 4. 8. 14:38

import requests
import time
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

URL = "https://example.com"  # 측정할 대상 URL
LOG_FILE = "full_response_log.txt"  # 로그 파일

def measure_backend_response_time(url):
    try:
        start = time.time()
        response = requests.get(url, timeout=10)
        end = time.time()
        duration_ms = round((end - start) * 1000, 2)
        return response.status_code, duration_ms
    except requests.exceptions.RequestException as e:
        return "ERROR", str(e)

def measure_rendering_time(url):
    try:
        options = Options()
        options.add_argument("--headless")
        options.add_argument("--no-sandbox")
        options.add_argument("--disable-dev-shm-usage")
        
        driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)

        start = time.time()
        driver.get(url)
        end = time.time()

        driver.quit()

        duration_ms = round((end - start) * 1000, 2)
        return duration_ms
    except Exception as e:
        return str(e)

if __name__ == "__main__":
    while True:
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        
        # 1. requests.get()로 백엔드 응답 시간 측정
        status_code, backend_time = measure_backend_response_time(URL)
        
        # 2. Selenium으로 렌더링 시간 측정
        rendering_time = measure_rendering_time(URL)

        # 3. 로그 포맷 만들기
        log_line = f"{timestamp} | {URL} | status: {status_code} | backend: {backend_time}ms | render: {rendering_time}ms"
        
        # 4. 콘솔 + 파일 저장
        print(log_line)
        with open(LOG_FILE, "a") as f:
            f.write(log_line + "\n")
        
        # 5. 1분 대기
        time.sleep(60)

posted by 구로공돌이 2025. 4. 8. 09:38

import requests
import time
from datetime import datetime

URL = "https://example.com"  # 측정할 대상 URL
LOG_FILE = "response_time_log.txt"  # 로그 파일 경로

def measure_response_time(url):
    try:
        start = time.time()
        response = requests.get(url, timeout=10)
        end = time.time()

        duration_ms = round((end - start) * 1000, 2)
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        log_line = f"{timestamp} | {url} | {response.status_code} | {duration_ms}ms"
        print(log_line)

        with open(LOG_FILE, "a") as f:
            f.write(log_line + "\n")

    except requests.exceptions.RequestException as e:
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        log_line = f"{timestamp} | {url} | ERROR | {str(e)}"
        print(log_line)
        with open(LOG_FILE, "a") as f:
            f.write(log_line + "\n")

if __name__ == "__main__":
    while True:
        measure_response_time(URL)
        time.sleep(60)  # 1분 대기