Skip to content

Commit

Permalink
Added a few flag for terminate after x seconds and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sd416 committed Jun 17, 2024
1 parent a34ee03 commit 30649d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python package

on:
Expand Down Expand Up @@ -29,12 +26,13 @@ jobs:
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Set PYTHONPATH
run: |
echo "PYTHONPATH=$(pwd)" >> $GITHUB_ENV
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
pytest -v
8 changes: 5 additions & 3 deletions log_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
rate_normal_min = 0.0001 # MB/s
rate_normal_max = 1.1000 # MB/s
rate_peak = 1.2000 # MB/s
log_line_size = 100 # Average log line size in bytes
log_line_size = 100 # Average size of a log line in bytes
base_exit_probability = 0.12 # Base 12% chance to exit early
rate_change_probability = 0.3 # 30% chance to change rate_max
rate_change_max_percentage = 0.4 # Maximum 40% change in rate_max
write_to_file = True # Flag to indicate if logs should be written to a file
log_file_path = 'logs.txt' # File path for log output
http_format_logs = True # Flag to format logs in HTTP response-like format
stop_after_seconds = -1 # Stop the script after X seconds (default -1 for continuous)

log_levels = ['DEBUG', 'INFO', 'WARNING', 'ERROR']
log_messages = [
Expand Down Expand Up @@ -133,16 +134,17 @@ def write_logs_random_segments(total_duration, segment_max_duration, rate_min, r
remaining_time -= segment_duration

# Main loop
start_time = time.time()
if write_to_file:
with open(log_file_path, 'w') as log_file:
while True:
while stop_after_seconds == -1 or time.time() - start_time < stop_after_seconds:
# Normal logging period with random segments
write_logs_random_segments(duration_normal, 5, rate_normal_min, rate_normal_max, base_exit_probability, log_file) # segments up to 5 seconds

# Peak logging period
write_logs_random_rate(duration_peak, rate_normal_max, rate_peak, log_file)
else:
while True:
while stop_after_seconds == -1 or time.time() - start_time < stop_after_seconds:
# Normal logging period with random segments
write_logs_random_segments(duration_normal, 5, rate_normal_min, rate_normal_max, base_exit_probability) # segments up to 5 seconds

Expand Down
25 changes: 21 additions & 4 deletions test_log_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import pytest
from log_generator import generate_log_line, write_logs, write_logs_random_rate, write_logs_random_segments
from log_generator import (
generate_log_line,
write_logs,
write_logs_random_rate,
write_logs_random_segments
)

# Override the configurations for testing
duration_normal = 2 # seconds
duration_peak = 1 # seconds
rate_normal_min = 0.0001 # MB/s
rate_normal_max = 0.001 # MB/s
rate_peak = 0.002 # MB/s
log_line_size = 50 # Average size of a log line in bytes
base_exit_probability = 0.1 # Base 10% chance to exit early
rate_change_probability = 0.2 # 20% chance to change rate_max
rate_change_max_percentage = 0.1 # Maximum 10% change in rate_max
stop_after_seconds = 10 # Stop the script after 2 seconds for testing

def test_generate_log_line():
log_line = generate_log_line()
Expand All @@ -9,17 +26,17 @@ def test_generate_log_line():
def test_write_logs(tmp_path):
log_file_path = tmp_path / "test_logs.txt"
with open(log_file_path, 'w') as log_file:
write_logs(0.001, 1, log_file)
write_logs(rate_normal_min, duration_normal, log_file)
assert log_file_path.read_text().strip() != ""

def test_write_logs_random_rate(tmp_path):
log_file_path = tmp_path / "test_logs_random_rate.txt"
with open(log_file_path, 'w') as log_file:
write_logs_random_rate(1, 0.0001, 0.001, log_file)
write_logs_random_rate(duration_peak, rate_normal_min, rate_normal_max, log_file)
assert log_file_path.read_text().strip() != ""

def test_write_logs_random_segments(tmp_path):
log_file_path = tmp_path / "test_logs_random_segments.txt"
with open(log_file_path, 'w') as log_file:
write_logs_random_segments(1, 5, 0.0001, 0.001, 0.1, log_file)
write_logs_random_segments(duration_normal, 1, rate_normal_min, rate_normal_max, base_exit_probability, log_file)
assert log_file_path.read_text().strip() != ""

0 comments on commit 30649d5

Please sign in to comment.