SPB Git

Repository Public License

BenchmarkTests.swift 6.9 KB 80 lines swift

1// ORIX Convert — Packages/EngineMedia/Tests/EngineMediaTests/BenchmarkTests.swift2// Description: Opt-in benchmark (ORIX_MEDIA_BENCH=1): runs the engine on 4K / 1080p video and a 5-minute signal, and3//              records the wall time plus the exact ffmpeg commands, which Scripts/benchmark.sh replays under4//              /usr/bin/time -l for peak memory and CPU time.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import Foundation7import Testing8import ConvertCore9@testable import EngineMedia1011@Suite(.serialized, .enabled(if: ProcessInfo.processInfo.environment["ORIX_MEDIA_BENCH"] == "1"))12struct Benchmarks {13    struct Record: Codable {14        var name: String15        var mediaSeconds: Double16        var engineWallSeconds: Double17        var outputBytes: Int18        var encoder: String19        var commands: [[String]]20    }2122    @Test func run() async throws {23        let dir = URL(fileURLWithPath: ProcessInfo.processInfo.environment["ORIX_MEDIA_BENCH_DIR"] ?? "/tmp/orix-media-bench", isDirectory: true)24        try? FileManager.default.removeItem(at: dir)25        try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)26        let uhd = dir.appendingPathComponent("uhd.mp4"), hd = dir.appendingPathComponent("hd.mp4"), wav = dir.appendingPathComponent("signal.wav")27        try await T.ffmpeg(["-filter_complex", "testsrc2=size=3840x2160:rate=30:duration=10[v];sine=frequency=440:sample_rate=48000:duration=10,aformat=channel_layouts=stereo[a]",28                            "-map", "[v]", "-map", "[a]", "-c:v", "h264_videotoolbox", "-b:v", "45M", "-pix_fmt", "yuv420p", "-c:a", "aac_at", "-b:a", "192k", "-f", "mp4", "file:" + uhd.path], timeout: 900)29        try await T.ffmpeg(["-filter_complex", "testsrc2=size=1920x1080:rate=30:duration=10[v];sine=frequency=440:sample_rate=48000:duration=10,aformat=channel_layouts=stereo[a]",30                            "-map", "[v]", "-map", "[a]", "-c:v", "h264_videotoolbox", "-b:v", "12M", "-pix_fmt", "yuv420p", "-c:a", "aac_at", "-b:a", "192k", "-f", "mp4", "file:" + hd.path], timeout: 900)31        // 5 minutes: sweeping tone + pink noise, stereo 44.1 kHz 16-bit.32        try await T.ffmpeg(["-filter_complex", "aevalsrc='0.4*sin(2*PI*(200+3*t)*t)|0.4*sin(2*PI*(300+2*t)*t)':s=44100:d=300[t];anoisesrc=color=pink:sample_rate=44100:duration=300:amplitude=0.2:seed=3,aformat=channel_layouts=stereo[n];[t][n]amix=inputs=2:normalize=0[o]",33                            "-map", "[o]", "-c:a", "pcm_s16le", "-f", "wav", "file:" + wav.path], timeout: 900)3435        struct Case { let name: String; let source: URL; let path: ConversionPath; let options: [String: OptionValue]; let seconds: Double }36        let cases: [Case] = [37            .init(name: "4K H.264 → HEVC (hardware, MP4)", source: uhd, path: try T.videoPath(.mp4, .mp4), options: ["video.codec": .string("hevc"), "video.encoder": .string("hardware")], seconds: 10),38            .init(name: "4K H.264 → HEVC (Apple software, MP4)", source: uhd, path: try T.videoPath(.mp4, .mp4), options: ["video.codec": .string("hevc"), "video.encoder": .string("software")], seconds: 10),39            .init(name: "4K H.264 → AV1 (SVT-AV1, MKV)", source: uhd, path: try T.videoPath(.mp4, .mkv), options: ["video.codec": .string("av1")], seconds: 10),40            .init(name: "4K H.264 → ProRes 422 (hardware, MOV)", source: uhd, path: try T.videoPath(.mp4, .mov), options: ["video.codec": .string("prores"), "video.encoder": .string("hardware")], seconds: 10),41            .init(name: "4K H.264 → ProRes 422 (prores_ks, MOV)", source: uhd, path: try T.videoPath(.mp4, .mov), options: ["video.codec": .string("prores"), "video.encoder": .string("software")], seconds: 10),42            .init(name: "4K H.264 → H.264 (hardware, MP4)", source: uhd, path: try T.videoPath(.mp4, .mp4), options: ["video.encoder": .string("hardware")], seconds: 10),43            .init(name: "1080p H.264 → VP9 (libvpx, WebM)", source: hd, path: try T.videoPath(.mp4, .webm), options: [:], seconds: 10),44            .init(name: "WAV → MP3 192 kb/s", source: wav, path: try T.audioPath(.wav, .mp3), options: [:], seconds: 300),45            .init(name: "WAV → AAC 160 kb/s (M4A, AudioToolbox)", source: wav, path: try T.audioPath(.wav, .m4a), options: [:], seconds: 300),46            .init(name: "WAV → FLAC level 5", source: wav, path: try T.audioPath(.wav, .flac), options: [:], seconds: 300),47            .init(name: "WAV → Opus 128 kb/s", source: wav, path: try T.audioPath(.wav, .opus), options: [:], seconds: 300),48            .init(name: "WAV → MP3, loudness-normalised to -16 LUFS (measure + encode + verify)", source: wav, path: try T.audioPath(.wav, .mp3), options: ["loudness.normalize": .bool(true)], seconds: 300),49        ]50        var records: [Record] = []51        for c in cases {52            let engine = T.engine(for: c.path)53            let work = dir.appendingPathComponent("out-\(records.count)", isDirectory: true)54            try FileManager.default.createDirectory(at: work, withIntermediateDirectories: true)55            let log = Collector<String>()56            let request = ConversionRequest(source: c.source, sourceFormat: c.path.source, destinationFormat: c.path.destination, operation: c.path.operation, options: ConversionOptions(c.options))57            let context = ConversionContext(workDirectory: work, log: { _, m in log.add(m) })58            let start = Date()59            let output = try await engine.convert(request, context: context)60            let wall = Date().timeIntervalSince(start)61            let inspection = try await engine.inspect(c.source, format: c.path.source)62            let checks = try await engine.validate(output, request: request, source: inspection)63            for check in checks where !check.passed { Issue.record("\(c.name): \(check.name) — \(check.detail)") }64            let size = try output.files[0].resourceValues(forKeys: [.fileSizeKey]).fileSize ?? 065            // The logged commands, with the output redirected to a scratch file so the replay does not need the work directory.66            let commands = log.all.filter { $0.hasPrefix("ffmpeg ") }.map { line -> [String] in67                let args = Array(line.split(separator: " ", omittingEmptySubsequences: false).map(String.init).dropFirst())68                let prefix = "file:" + work.path + "/"69                return args.map { $0.hasPrefix(prefix) ? "file:" + dir.appendingPathComponent("replay-" + String($0.dropFirst(prefix.count))).path : $0 }70            }71            records.append(Record(name: c.name, mediaSeconds: c.seconds, engineWallSeconds: wall, outputBytes: size,72                                  encoder: output.facts["video.encoder"] ?? output.facts["audio.encoder"] ?? "", commands: commands))73            try? FileManager.default.removeItem(at: work)74        }75        let encoder = JSONEncoder()76        encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]77        try encoder.encode(records).write(to: dir.appendingPathComponent("records.json"))78    }79}80
Files main
Menu

Theme