|
| 1 | +# Copyright (c) 2024, 2025 Bart van de Lint, Uwe Fechner |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +SIMPLE = false |
| 5 | +T_REF = 48.0 # AMD Ryzen 7840U, Julia 1.11, no sys image [s] |
| 6 | + # 37s with sys image |
| 7 | + |
| 8 | +using Pkg |
| 9 | +if ! ("Test" ∈ keys(Pkg.project().dependencies)) |
| 10 | + using TestEnv; TestEnv.activate() |
| 11 | +end |
| 12 | +using KiteModels, LinearAlgebra, Statistics, Test, Distributed |
| 13 | +include("bench_ref.jl") |
| 14 | + |
| 15 | +# Simulation parameters |
| 16 | +dt = 0.05 |
| 17 | +total_time = 10.0 # Longer simulation to see oscillations |
| 18 | +vsm_interval = 3 |
| 19 | +steps = Int(round(total_time / dt)) |
| 20 | + |
| 21 | +# Steering parameters |
| 22 | +steering_freq = 1/2 # Hz - full left-right cycle frequency |
| 23 | +steering_magnitude = 10.0 # Magnitude of steering input [Nm] |
| 24 | + |
| 25 | +# Function to run benchmark in separate Julia process |
| 26 | +function run_benchmark_subprocess() |
| 27 | + # Create a temporary script file for the benchmark |
| 28 | + benchmark_script = """ |
| 29 | + using Pkg |
| 30 | + if ! ("Test" ∈ keys(Pkg.project().dependencies)) |
| 31 | + using TestEnv; TestEnv.activate() |
| 32 | + end |
| 33 | + using KiteModels, LinearAlgebra, Statistics |
| 34 | + include("test/bench_ref.jl") |
| 35 | + |
| 36 | + SIMPLE = $SIMPLE |
| 37 | + T_REF = $T_REF |
| 38 | + |
| 39 | + # Initialize model |
| 40 | + set = load_settings("system_ram.yaml") |
| 41 | + set.segments = 3 |
| 42 | + set_values = [-50, 0.0, 0.0] # Set values of the torques of the three winches. [Nm] |
| 43 | + set.quasi_static = false |
| 44 | + set.physical_model = SIMPLE ? "simple_ram" : "ram" |
| 45 | + |
| 46 | + sam = SymbolicAWEModel(set) |
| 47 | + sam.set.abs_tol = 1e-2 |
| 48 | + sam.set.rel_tol = 1e-2 |
| 49 | + rm("data/model_1.11_ram_dynamic_3_seg.bin"; force=true) |
| 50 | + |
| 51 | + # Initialize at elevation |
| 52 | + set.l_tethers[2] += 0.2 |
| 53 | + set.l_tethers[3] += 0.2 |
| 54 | + time_ = init!(sam; remake=false, reload=true, bench=true) |
| 55 | + @info "Simplify took \$time_ seconds" |
| 56 | + rel_performance = (T_REF / rel_cpu_performance())/time_ |
| 57 | + |
| 58 | + # Write results to file for parent process to read |
| 59 | + open("benchmark_results.tmp", "w") do f |
| 60 | + println(f, time_) |
| 61 | + println(f, rel_performance) |
| 62 | + end |
| 63 | + """ |
| 64 | + |
| 65 | + # Write the script to a temporary file |
| 66 | + temp_script = "temp_benchmark.jl" |
| 67 | + open(temp_script, "w") do f |
| 68 | + write(f, benchmark_script) |
| 69 | + end |
| 70 | + |
| 71 | + try |
| 72 | + # Run the benchmark in a separate Julia process |
| 73 | + result = run(`julia --project=. $temp_script`) |
| 74 | + |
| 75 | + if result.exitcode == 0 |
| 76 | + # Read results from temporary file |
| 77 | + if isfile("benchmark_results.tmp") |
| 78 | + lines = readlines("benchmark_results.tmp") |
| 79 | + time_ = parse(Float64, lines[1]) |
| 80 | + rel_performance = parse(Float64, lines[2]) |
| 81 | + |
| 82 | + @info "Simplify took $time_ seconds" |
| 83 | + @info "Relative performance: $rel_performance" |
| 84 | + @test rel_performance > 0.8 |
| 85 | + |
| 86 | + # Clean up temporary files |
| 87 | + rm("benchmark_results.tmp", force=true) |
| 88 | + rm(temp_script, force=true) |
| 89 | + |
| 90 | + return time_, rel_performance |
| 91 | + else |
| 92 | + error("Benchmark results file not found") |
| 93 | + end |
| 94 | + else |
| 95 | + error("Benchmark process failed with exit code $(result.exitcode)") |
| 96 | + end |
| 97 | + catch e |
| 98 | + # Clean up temporary files in case of error |
| 99 | + rm("benchmark_results.tmp", force=true) |
| 100 | + rm(temp_script, force=true) |
| 101 | + rethrow(e) |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +# Run the benchmark in a separate process |
| 106 | +time_, rel_performance = run_benchmark_subprocess() |
| 107 | + |
| 108 | +# Note: sys object is not available when running in separate process |
| 109 | +# If you need sys, you would need to serialize it or run parts in the main process |
| 110 | +nothing |
| 111 | + |
| 112 | +# Desktop, AMD Ryzen 9 7950X, Julia 1.11: |
| 113 | +# - first run 34.5 seconds |
| 114 | +# - second run 21.1 seconds |
| 115 | + |
| 116 | +# Laptop, AMD Ryzen 7 7840U, Julia 1.11: |
| 117 | +# - first run 35.0 seconds |
| 118 | +# - second run 24.0 seconds |
| 119 | + |
| 120 | + |
0 commit comments