Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Tests/ORIXDSPTests/EffectBenchmarkTests.swift3// Description: Micro-benchmark: CPU cost of every effect as a share of real time (512-frame blocks at 48 kHz).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXDSP9import Testing1011@Suite(.serialized) struct EffectBenchmarkTests {12 @Test func cpuCostPerEffect() {13 let block = 512, blocks = 1875 // 20 s of audio14 let source = testSignal(seconds: Double(block * 8) / testRate)15 let l = UnsafeMutablePointer<Float>.allocate(capacity: block), r = UnsafeMutablePointer<Float>.allocate(capacity: block)16 defer { l.deallocate(); r.deallocate() }17 #if DEBUG18 let build = "debug"19 #else20 let build = "release"21 #endif22 var lines = ["ORIX DJ effects, CPU cost (\(build) build, 512 frames @ 48 kHz, one core)", "effect % real time µs / block"]23 for kind in EffectKind.allCases {24 let fx = EffectFactory.make(kind, sampleRate: testRate)25 fx.setEnabled(true)26 var best = Double.infinity27 for pass in 0 ..< 3 { // best of three passes; the first one also warms caches28 var beat = 0.029 let clock = ContinuousClock(), start = clock.now30 for b in 0 ..< blocks {31 source.withUnsafeBufferPointer { s in32 l.update(from: s.baseAddress! + (b % 8) * block, count: block); r.update(from: s.baseAddress! + (b % 8) * block, count: block)33 }34 fx.setTransport(bpm: 126, beatPosition: beat)35 fx.render(l, r, block)36 beat += Double(block) * 126 / 60 / testRate37 }38 let d = clock.now - start39 let seconds = Double(d.components.seconds) + Double(d.components.attoseconds) * 1e-1840 if pass > 0 || seconds < best { best = min(best, seconds) }41 }42 let audio = Double(block * blocks) / testRate, share = best / audio * 10043 lines.append(kind.rawValue.padding(toLength: 12, withPad: " ", startingAt: 0) + String(format: " %10.3f %12.2f", share, best / Double(blocks) * 1e6))44 #expect(share < 20, "\(kind) costs \(share) % of real time")45 #expect(l[0].isFinite)46 }47 print(lines.joined(separator: "\n"))48 }49}50