|
| 1 | +import json |
| 2 | +import pandas as pd |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import os |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +# --- Configuration --- |
| 8 | + |
| 9 | +if len(sys.argv) < 3: |
| 10 | + print("Usage: python generate_graphs.py <input_json_path> <output_dir>") |
| 11 | + sys.exit(1) |
| 12 | + |
| 13 | +INPUT_FILE = sys.argv[1] |
| 14 | +OUTPUT_DIR = sys.argv[2] # This will be 'benchmark_graphs' |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +# Ensure the output directory exists |
| 19 | +os.makedirs(OUTPUT_DIR, exist_ok=True) |
| 20 | + |
| 21 | +# --- 1. Data Processing Function --- |
| 22 | +def process_benchmark_data(file_path): |
| 23 | + """Loads benchmark JSON, calculates Average Time per Operation, and returns a DataFrame.""" |
| 24 | + with open(file_path, 'r') as f: |
| 25 | + data = json.load(f) |
| 26 | + |
| 27 | + benchmarks = [] |
| 28 | + |
| 29 | + for bm in data.get('benchmarks', []): |
| 30 | + name = bm['name'] |
| 31 | + cpu_time_ns = bm['cpu_time'] |
| 32 | + iterations = bm['iterations'] |
| 33 | + |
| 34 | + # Extract the key features for grouping and display |
| 35 | + if 'SystemMalloc' in name: |
| 36 | + allocator = 'System Malloc' |
| 37 | + elif 'MyTLSFAllocatorFixture' in name: |
| 38 | + allocator = 'TLSF Allocator' |
| 39 | + else: |
| 40 | + continue # Skip non-relevant benchmarks |
| 41 | + |
| 42 | + # Determine the workload type ( Mixed) |
| 43 | + if 'AllocDeallocCycle_MixedSize' in name: |
| 44 | + workload = 'AllocDeallocCycle_MixedSize' |
| 45 | + # Extract N from the end of the name (e.g., '/100') |
| 46 | + try: |
| 47 | + num_ops = int(name.split('/')[-1]) |
| 48 | + except ValueError: |
| 49 | + num_ops = 1 # Fallback |
| 50 | + # For mixed, CPU time is total time for N operations; Avg is total / N |
| 51 | + avg_time_ns = cpu_time_ns / num_ops |
| 52 | + else: |
| 53 | + continue |
| 54 | + |
| 55 | + benchmarks.append({ |
| 56 | + 'name': name, |
| 57 | + 'allocator': allocator, |
| 58 | + 'workload': workload, |
| 59 | + 'num_ops': num_ops, |
| 60 | + 'cpu_time_ns': cpu_time_ns, |
| 61 | + 'avg_time_ns': avg_time_ns # Average time for a single allocation/operation |
| 62 | + }) |
| 63 | + |
| 64 | + return pd.DataFrame(benchmarks) |
| 65 | + |
| 66 | +# --- 2. Plotting Functions --- |
| 67 | +def plot_performance(df_AllocDeallocCycle_MixedSize): |
| 68 | + """Generates a line plot showing My TLSF's performance vs. Malloc's""" |
| 69 | + |
| 70 | + # Sort for correct line plotting |
| 71 | + df_mixed_sorted = df_AllocDeallocCycle_MixedSize.sort_values(by='num_ops') |
| 72 | + |
| 73 | + plt.figure(figsize=(9, 6)) |
| 74 | + |
| 75 | + # Plotting the lines |
| 76 | + for name, group in df_mixed_sorted.groupby('allocator'): |
| 77 | + plt.plot(group['num_ops'], group['avg_time_ns'], |
| 78 | + marker='o', linestyle='-', label=name, |
| 79 | + linewidth=2.5) |
| 80 | + |
| 81 | + plt.title('Performance Graph', fontsize=14) |
| 82 | + plt.ylabel('Average Time per N Allocation-Deallocation of Mixed Sizes (nanoseconds)', fontsize=12) |
| 83 | + plt.xlabel('N Allocations per iteration ', fontsize=12) |
| 84 | + plt.xticks(group['num_ops']) # Ensure clear ticks |
| 85 | + plt.legend(title='Allocator') |
| 86 | + plt.grid(linestyle='--', alpha=0.7) |
| 87 | + |
| 88 | + plt.savefig(os.path.join(OUTPUT_DIR, 'Performance_line_plot.svg'), format='svg', bbox_inches='tight') |
| 89 | + print(f"Generated {os.path.join(OUTPUT_DIR, 'Performance_line_plot.svg')}") |
| 90 | + plt.close() |
| 91 | + |
| 92 | +# --- 3. Main Execution --- |
| 93 | +if __name__ == '__main__': |
| 94 | + try: |
| 95 | + # Load and process the data |
| 96 | + df = process_benchmark_data(INPUT_FILE) |
| 97 | + print("Data loaded successfully.") |
| 98 | + print(f"Total benchmarks processed: {len(df)}") |
| 99 | + |
| 100 | + # Filter for the mixed workload (fragmentation stress) |
| 101 | + df_AllocDeallocCycle_MixedSize = df[df['workload'] == 'AllocDeallocCycle_MixedSize'].copy() |
| 102 | + |
| 103 | + if df_AllocDeallocCycle_MixedSize.empty: |
| 104 | + print("Error: Could not find 'AllocDeallocCycle_MixedSize' data. Check JSON keys.") |
| 105 | + else: |
| 106 | + #time taken across various allocations |
| 107 | + plot_performance(df_AllocDeallocCycle_MixedSize) |
| 108 | + |
| 109 | + print("\nPlotting complete. SVGs saved to the 'output_graphs' directory.") |
| 110 | + |
| 111 | + except FileNotFoundError: |
| 112 | + print(f"Error: Input file '{INPUT_FILE}' not found. Place your benchmark JSON here.") |
| 113 | + except Exception as e: |
| 114 | + print(f"An unexpected error occurred: {e}") |
0 commit comments