File size: 1,588 Bytes
54c8522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Profile simulation to identify performance bottlenecks."""
import cProfile
import pstats
from pathlib import Path
from io import StringIO

from scheduler.data.case_generator import CaseGenerator
from scheduler.simulation.engine import CourtSim, CourtSimConfig


def run_simulation():
    """Run a small simulation for profiling."""
    cases = CaseGenerator.from_csv(Path("data/generated/cases_small.csv"))
    print(f"Loaded {len(cases)} cases")
    
    config = CourtSimConfig(
        start=cases[0].filed_date if cases else None,
        days=30,
        seed=42,
        courtrooms=5,
        daily_capacity=151,
        policy="readiness",
    )
    
    sim = CourtSim(config, cases)
    result = sim.run()
    
    print(f"Completed: {result.hearings_total} hearings, {result.disposals} disposals")


if __name__ == "__main__":
    # Profile the simulation
    profiler = cProfile.Profile()
    profiler.enable()
    
    run_simulation()
    
    profiler.disable()
    
    # Print stats
    s = StringIO()
    stats = pstats.Stats(profiler, stream=s)
    stats.strip_dirs()
    stats.sort_stats('cumulative')
    stats.print_stats(30)  # Top 30 functions
    
    print("\n" + "="*80)
    print("TOP 30 CUMULATIVE TIME CONSUMERS")
    print("="*80)
    print(s.getvalue())
    
    # Also sort by total time
    s2 = StringIO()
    stats2 = pstats.Stats(profiler, stream=s2)
    stats2.strip_dirs()
    stats2.sort_stats('tottime')
    stats2.print_stats(20)
    
    print("\n" + "="*80)
    print("TOP 20 TOTAL TIME CONSUMERS")
    print("="*80)
    print(s2.getvalue())