mobench-sdk 0.2.0

Rust SDK for mobile benchmarking with timing harness and Android/iOS builders
Documentation
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import Foundation
import Darwin

private let defaultFunction = "{{DEFAULT_FUNCTION}}"
private let defaultIterations: UInt32 = 20
private let defaultWarmup: UInt32 = 3
private let reportSchemaV2 = "mobench.run/v2"

struct ReportIdentity {
    let runId: String
    let nonce: String
    let logicalSessionId: String
    let functionId: String
    let producer: String
}

struct BenchParams {
    let function: String
    let iterations: UInt32
    let warmup: UInt32
    let identity: ReportIdentity?

    fileprivate struct EncodedBenchSpec: Decodable {
        let function: String
        let iterations: UInt32
        let warmup: UInt32
        let schema_version: String?
        let run_id: String?
        let nonce: String?
        let logical_session_id: String?
        let function_id: String?
        let producer: String?
    }

    static func fromBundle() -> BenchParams? {
        guard let url = Bundle.main.url(forResource: "bench_spec", withExtension: "json") else {
            print("[BenchRunner] No bench_spec.json found in bundle, will use process info or defaults")
            return nil
        }
        do {
            let data = try Data(contentsOf: url)
            let decoded = try JSONDecoder().decode(EncodedBenchSpec.self, from: data)
            print("[BenchRunner] Loaded config from bench_spec.json: function=\(decoded.function), iterations=\(decoded.iterations), warmup=\(decoded.warmup)")
            return BenchParams(
                function: decoded.function,
                iterations: decoded.iterations,
                warmup: decoded.warmup,
                identity: decoded.schema_version == reportSchemaV2 ? decoded.reportIdentity() : nil
            )
        } catch {
            print("[BenchRunner] ERROR: Failed to parse bench_spec.json: \(error)")
            return nil
        }
    }

    static func fromProcessInfo() -> BenchParams {
        let info = ProcessInfo.processInfo
        var function = defaultFunction
        var iterations = defaultIterations
        var warmup = defaultWarmup

        if let envFunction = info.environment["BENCH_FUNCTION"], !envFunction.isEmpty {
            function = envFunction
        }
        if let envIterations = info.environment["BENCH_ITERATIONS"], let parsed = UInt32(envIterations) {
            iterations = parsed
        }
        if let envWarmup = info.environment["BENCH_WARMUP"], let parsed = UInt32(envWarmup) {
            warmup = parsed
        }

        for arg in info.arguments {
            if arg.hasPrefix("--bench-function="), let value = arg.split(separator: "=", maxSplits: 1).last {
                function = String(value)
            } else if arg.hasPrefix("--bench-iterations="),
                      let value = arg.split(separator: "=", maxSplits: 1).last,
                      let parsed = UInt32(value) {
                iterations = parsed
            } else if arg.hasPrefix("--bench-warmup="),
                      let value = arg.split(separator: "=", maxSplits: 1).last,
                      let parsed = UInt32(value) {
                warmup = parsed
            }
        }

        print("[BenchRunner] Resolved params: function=\(function), iterations=\(iterations), warmup=\(warmup)")
        return BenchParams(function: function, iterations: iterations, warmup: warmup, identity: nil)
    }

    static func resolved() -> BenchParams {
        if let bundled = fromBundle() {
            return bundled
        }
        return fromProcessInfo()
    }
}

private extension BenchParams.EncodedBenchSpec {
    func reportIdentity() -> ReportIdentity? {
        guard let run_id, let nonce, let logical_session_id, let function_id, let producer else {
            return nil
        }
        return ReportIdentity(
            runId: run_id,
            nonce: nonce,
            logicalSessionId: logical_session_id,
            functionId: function_id,
            producer: producer
        )
    }
}

struct BenchmarkResult {
    let displayText: String
    let jsonReport: String
}

enum {{PROJECT_NAME_PASCAL}}FFI {
    static func runCurrentBenchmark() -> BenchmarkResult {
        let params = BenchParams.resolved()
        return run(params: params)
    }

    static func run(params: BenchParams) -> BenchmarkResult {
        do {
            let processMemorySampler = ProcessMemorySampler()
            processMemorySampler.start()
            let rawReport: [String: Any]
            do {
                rawReport = try NativeBenchRunner.run(params: params)
            } catch {
                _ = processMemorySampler.stop()
                throw error
            }
            let runProcessPeakMemoryKb = processMemorySampler.stop()
            let report = generateJSONReport(
                rawReport,
                params: params,
                runProcessPeakMemoryKb: runProcessPeakMemoryKb
            )
            let displayText = formatBenchReport(report)
            let jsonReport = serializeJSON(report)
            return BenchmarkResult(displayText: displayText, jsonReport: jsonReport)
        } catch {
            print("[BenchRunner] ERROR: Benchmark failed: \(error)")
            let message = error.localizedDescription
            return BenchmarkResult(
                displayText: "Benchmark error: \(message)",
                jsonReport: serializeFailureJSON(message, params: params)
            )
        }
    }

    private static func generateJSONReport(
        _ rawReport: [String: Any],
        params: BenchParams,
        runProcessPeakMemoryKb: UInt64?
    ) -> [String: Any] {
        var json = rawReport
        let spec = json["spec"] as? [String: Any] ?? [:]
        let function = spec["name"] as? String ?? defaultFunction
        json["function"] = function

        let samples = json["samples"] as? [[String: Any]] ?? []
        let durations = samples.compactMap { coerceToUInt64($0["duration_ns"] ?? 0) }
        json["samples_ns"] = durations
        applyV2Envelope(to: &json, params: params, samplesNs: durations, outcome: ["status": "success"])

        if !durations.isEmpty {
            let sum = durations.reduce(0, +)
            let mean = sum / UInt64(durations.count)
            json["stats"] = [
                "min_ns": durations.min() ?? 0,
                "max_ns": durations.max() ?? 0,
                "avg_ns": Double(sum) / Double(durations.count),
                "mean_ns": mean,
                "median_ns": median(durations)
            ] as [String: Any]
            json["mean_ns"] = mean
            json["min_ns"] = durations.min() ?? 0
            json["max_ns"] = durations.max() ?? 0
        }

        let cpuSamplesMs = samples.compactMap { sample in
            sample["cpu_time_ms"].flatMap(coerceToUInt64)
        }
        let peakSamplesKb = samples.compactMap { sample in
            sample["peak_memory_kb"].flatMap(coerceToUInt64)
        }
        let processPeakSamplesKb = samples.compactMap { sample in
            sample["process_peak_memory_kb"].flatMap(coerceToUInt64)
        }
        var resources: [String: Any] = [
            "platform": "ios",
            "memory_process": "benchmark_app",
            "timestamp_ms": Int64(Date().timeIntervalSince1970 * 1000)
        ]
        if !cpuSamplesMs.isEmpty {
            let total = cpuSamplesMs.reduce(0, +)
            resources["cpu_total_ms"] = total
            resources["cpu_median_ms"] = median(cpuSamplesMs)
            resources["elapsed_cpu_ms"] = total
        }
        if let peak = peakSamplesKb.max() {
            resources["peak_memory_kb"] = peak
            resources["peak_memory_growth_kb"] = peak
        }
        if let processPeak = processPeakSamplesKb.max() ?? runProcessPeakMemoryKb {
            resources["process_peak_memory_kb"] = processPeak
        }
        json["resources"] = resources
        return json
    }

    private static func serializeFailureJSON(_ message: String, params: BenchParams) -> String {
        var json: [String: Any] = ["error": true, "message": message, "samples_ns": []]
        applyV2Envelope(
            to: &json,
            params: params,
            samplesNs: [],
            outcome: [
                "status": "failure",
                "error": ["code": "benchmark_error", "message": message]
            ]
        )
        return serializeJSON(json)
    }

    private static func applyV2Envelope(
        to json: inout [String: Any],
        params: BenchParams,
        samplesNs: [UInt64],
        outcome: [String: Any]
    ) {
        guard let identity = params.identity else { return }
        json["schema_version"] = reportSchemaV2
        json["run_id"] = identity.runId
        json["nonce"] = identity.nonce
        json["logical_session_id"] = identity.logicalSessionId
        json["function_id"] = identity.functionId
        json["producer"] = identity.producer
        json["requested"] = ["iterations": params.iterations, "warmup": params.warmup]
        json["observed"] = [
            "iterations": samplesNs.count,
            "warmup": outcome["status"] as? String == "success" ? params.warmup : 0
        ]
        json["samples_ns"] = samplesNs
        json["outcome"] = outcome
    }

    private static func formatBenchReport(_ report: [String: Any]) -> String {
        let spec = report["spec"] as? [String: Any] ?? [:]
        let function = spec["name"] as? String ?? defaultFunction
        let iterations = coerceToUInt64(spec["iterations"] ?? defaultIterations) ?? UInt64(defaultIterations)
        let warmup = coerceToUInt64(spec["warmup"] ?? defaultWarmup) ?? UInt64(defaultWarmup)
        let samples = report["samples"] as? [[String: Any]] ?? []

        var output = "=== Benchmark Results ===\n\n"
        output += "Function: \(function)\n"
        output += "Iterations: \(iterations)\n"
        output += "Warmup: \(warmup)\n\n"
        output += "Samples (\(samples.count)):\n"
        for (index, sample) in samples.enumerated() {
            let duration = coerceToUInt64(sample["duration_ns"] ?? 0) ?? 0
            output += "  \(index + 1). \(formatDuration(duration))\n"
        }

        if let stats = report["stats"] as? [String: Any] {
            output += "\nStatistics:\n"
            output += "  Min: \(formatDuration(coerceToUInt64(stats["min_ns"] ?? 0) ?? 0))\n"
            output += "  Max: \(formatDuration(coerceToUInt64(stats["max_ns"] ?? 0) ?? 0))\n"
            output += "  Avg: \(formatDuration(coerceToUInt64(stats["mean_ns"] ?? 0) ?? 0))\n"
        }

        return output
    }

    private static func serializeJSON(_ value: [String: Any]) -> String {
        do {
            let data = try JSONSerialization.data(withJSONObject: value, options: [.sortedKeys])
            return String(data: data, encoding: .utf8) ?? "{}"
        } catch {
            print("[BenchRunner] ERROR: Failed to serialize JSON report: \(error)")
            return "{}"
        }
    }

    private static func formatDuration(_ ns: UInt64) -> String {
        let ms = Double(ns) / 1_000_000.0
        if ms >= 1000.0 {
            return String(format: "%.3fs", ms / 1000.0)
        }
        return String(format: "%.3fms", ms)
    }
}

private enum NativeBenchRunner {
    static func run(params: BenchParams) throws -> [String: Any] {
        let spec: [String: Any] = [
            "name": params.function,
            "iterations": params.iterations,
            "warmup": params.warmup
        ]
        let specData = try JSONSerialization.data(withJSONObject: spec, options: [])
        var out = MobenchBuf()
        let status: Int32 = specData.withUnsafeBytes { bytes in
            let ptr = bytes.baseAddress?.assumingMemoryBound(to: UInt8.self)
            return mobench_run_benchmark_json(ptr, UInt(specData.count), &out)
        }

        if status != 0 {
            let message = mobench_last_error_message().map { String(cString: $0) } ?? "native benchmark failed"
            mobench_free_buf(&out)
            throw NativeBenchError.execution(message)
        }

        defer {
            mobench_free_buf(&out)
        }
        guard let ptr = out.ptr, out.len > 0 else {
            throw NativeBenchError.execution("native benchmark returned an empty report")
        }
        let data = Data(bytes: ptr, count: Int(out.len))
        let decoded = try JSONSerialization.jsonObject(with: data, options: [])
        guard let report = decoded as? [String: Any] else {
            throw NativeBenchError.execution("native benchmark returned non-object JSON")
        }
        return report
    }
}

private enum NativeBenchError: LocalizedError {
    case execution(String)

    var errorDescription: String? {
        switch self {
        case .execution(let message):
            return message
        }
    }
}

private final class ProcessMemorySampler {
    private let sampleInterval: TimeInterval
    private let queue = DispatchQueue(label: "mobench-process-memory-sampler")
    private var timer: DispatchSourceTimer?
    private var peakKb: UInt64 = 0

    init(sampleInterval: TimeInterval = 0.01) {
        self.sampleInterval = sampleInterval
    }

    func start() {
        queue.sync {
            guard timer == nil else {
                return
            }

            recordCurrentLocked()
            let source = DispatchSource.makeTimerSource(queue: queue)
            source.schedule(deadline: .now(), repeating: sampleInterval)
            source.setEventHandler { [weak self] in
                self?.recordCurrentLocked()
            }
            source.resume()
            timer = source
        }
    }

    func stop() -> UInt64? {
        queue.sync {
            timer?.cancel()
            timer = nil
            recordCurrentLocked()
            return peakKb > 0 ? peakKb : nil
        }
    }

    private func recordCurrentLocked() {
        if let currentKb = currentProcessResidentMemoryKb(), currentKb > peakKb {
            peakKb = currentKb
        }
    }
}

private func currentProcessResidentMemoryKb() -> UInt64? {
    var info = mach_task_basic_info()
    var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size) / 4
    let result = withUnsafeMutablePointer(to: &info) {
        $0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
            task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count)
        }
    }

    guard result == KERN_SUCCESS else {
        return nil
    }
    return UInt64(info.resident_size / 1024)
}

private func median(_ values: [UInt64]) -> UInt64 {
    let sorted = values.sorted()
    if sorted.isEmpty {
        return 0
    }
    if sorted.count % 2 == 0 {
        return (sorted[sorted.count / 2 - 1] + sorted[sorted.count / 2]) / 2
    }
    return sorted[sorted.count / 2]
}

private func coerceToUInt64(_ value: Any) -> UInt64? {
    if let value = value as? UInt64 {
        return value
    }
    if let value = value as? UInt32 {
        return UInt64(value)
    }
    if let value = value as? UInt {
        return UInt64(value)
    }
    if let value = value as? Int64, value >= 0 {
        return UInt64(value)
    }
    if let value = value as? Int, value >= 0 {
        return UInt64(value)
    }
    if let value = value as? NSNumber, value.int64Value >= 0 {
        return UInt64(value.uint64Value)
    }
    return nil
}

private func escapeJSON(_ value: String) -> String {
    value
        .replacingOccurrences(of: "\\", with: "\\\\")
        .replacingOccurrences(of: "\"", with: "\\\"")
}