|
| 1 | +import itertools |
| 2 | +import os |
| 3 | +import json |
| 4 | +import statistics |
| 5 | +import subprocess |
| 6 | +import pytest |
| 7 | +import logging |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +from helpers import load_results_from_csv, log_result_csv, mean |
| 11 | + |
| 12 | +### Tests default settings ### |
| 13 | + |
| 14 | +CSV_FILE = f"/tmp/results_{datetime.now().strftime("%Y-%m-%d_%H:%M:%S")}.csv" |
| 15 | + |
| 16 | +DEFAULT_SAMPLES_NUM = 10 |
| 17 | +DEFAULT_SIZE = "1G" |
| 18 | +DEFAULT_BS = "4k" |
| 19 | +DEFAULT_IODEPTH = 1 |
| 20 | +DEFAULT_FILE = "fio-testfile" |
| 21 | + |
| 22 | +### Tests parameters |
| 23 | + |
| 24 | +system_memory = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') |
| 25 | + |
| 26 | +block_sizes = ("4k", "16k", "64k", "1M") |
| 27 | +file_sizes = ("1G", "4G", f"{(system_memory/(1024.**3))*2}G") |
| 28 | +modes = ( |
| 29 | + "read", |
| 30 | + "randread", |
| 31 | + "write", |
| 32 | + "randwrite" |
| 33 | +) |
| 34 | + |
| 35 | +test_types = { |
| 36 | + "read": "seq_read", |
| 37 | + "randread": "rand_read", |
| 38 | + "write": "seq_write", |
| 39 | + "randwrite": "rand_write" |
| 40 | +} |
| 41 | + |
| 42 | +### End of tests parameters ### |
| 43 | + |
| 44 | +def run_fio( |
| 45 | + test_name, |
| 46 | + rw_mode, |
| 47 | + temp_dir, |
| 48 | + bs=DEFAULT_BS, |
| 49 | + iodepth=DEFAULT_IODEPTH, |
| 50 | + size=DEFAULT_SIZE, |
| 51 | + file_path="", |
| 52 | +): |
| 53 | + json_output_path = os.path.join(temp_dir, f"{test_name}.json") |
| 54 | + if not file_path: |
| 55 | + file_path = os.path.join(temp_dir, DEFAULT_FILE) |
| 56 | + fio_cmd = [ |
| 57 | + "fio", |
| 58 | + f"--name={test_name}", |
| 59 | + f"--rw={rw_mode}", |
| 60 | + f"--bs={bs}", |
| 61 | + f"--iodepth={iodepth}", |
| 62 | + f"--size={size}", |
| 63 | + f"--filename={file_path}", |
| 64 | + "--direct=1", |
| 65 | + "--end_fsync=1", |
| 66 | + "--fsync_on_close=1", |
| 67 | + "--numjobs=1", |
| 68 | + "--group_reporting", |
| 69 | + "--output-format=json", |
| 70 | + f"--output={json_output_path}" |
| 71 | + ] |
| 72 | + |
| 73 | + result = subprocess.run(fio_cmd, capture_output=True, text=True) |
| 74 | + if result.returncode != 0: |
| 75 | + raise RuntimeError(f"fio failed for {test_name}:\n{result.stderr}") |
| 76 | + |
| 77 | + with open(json_output_path) as f: |
| 78 | + return json.load(f) |
| 79 | + |
| 80 | +def assert_performance_not_degraded(current, previous, threshold=10): |
| 81 | + diffs = {} |
| 82 | + for metric in ("bw_MBps", "IOPS", "latency"): |
| 83 | + try: |
| 84 | + curr = mean(current, metric) |
| 85 | + prev = mean(previous, metric) |
| 86 | + except statistics.StatisticsError: |
| 87 | + logging.info(f"Missing metric ({metric}), skipping comparison") |
| 88 | + continue |
| 89 | + diff = (curr-prev if metric == "latency" else prev-curr) / (prev * 100) |
| 90 | + assert diff <= threshold, \ |
| 91 | + f"{metric} changed by {diff:.2f}% (allowed {threshold}%)" |
| 92 | + diffs[metric] = diff |
| 93 | + |
| 94 | + logging.info("Performance difference summary:") |
| 95 | + for k, v in diffs.items(): |
| 96 | + sign = "+" if v < 0 else "-" |
| 97 | + logging.info(f"- {k}: {sign}{abs(v):.2f}%") |
| 98 | + |
| 99 | + |
| 100 | +class TestDiskPerfDestroy: ... |
| 101 | + |
| 102 | + |
| 103 | +class TestDiskPerf: |
| 104 | + test_cases = itertools.product(block_sizes, file_sizes, modes) |
| 105 | + |
| 106 | + @pytest.mark.parametrize("block_size,file_size,rw_mode", test_cases) |
| 107 | + def test_disk_benchmark( |
| 108 | + self, |
| 109 | + temp_dir, |
| 110 | + prev_results, |
| 111 | + block_size, |
| 112 | + file_size, |
| 113 | + rw_mode |
| 114 | + ): |
| 115 | + test_type = test_types[rw_mode] |
| 116 | + for i in range(DEFAULT_SAMPLES_NUM): |
| 117 | + result = run_fio(test_type, rw_mode, temp_dir) |
| 118 | + summary = log_result_csv(test_type, rw_mode, result, CSV_FILE) |
| 119 | + assert summary["IOPS"] > 0 |
| 120 | + results = load_results_from_csv(CSV_FILE) |
| 121 | + key = (test_type, rw_mode) |
| 122 | + if prev_results and key in prev_results: |
| 123 | + assert_performance_not_degraded(results[key], prev_results[key]) |
0 commit comments