oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1#if DEBUG
2import Foundation
3import Darwin
4
5/// Fixture instrumentation contains only process memory numbers, never inputs.
6@MainActor public final class ShareMemory {
7 private var peak: UInt64 = 0
8 private var sampling: Task<Void, Never>?
9 public init() {
10 sampling = Task { [weak self] in
11 while !Task.isCancelled {
12 self?.sample()
13 try? await Task.sleep(for: .milliseconds(10))
14 }
15 }
16 }
17 private func sample() {
18 var info = task_vm_info_data_t()
19 var count = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<integer_t>.size)
20 let status = withUnsafeMutablePointer(to: &info) { pointer in
21 pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count) }
22 }
23 if status == KERN_SUCCESS { peak = max(peak, info.phys_footprint) }
24 }
25 public func save(to url: URL) {
26 sample(); sampling?.cancel()
27 var usage = rusage(); getrusage(RUSAGE_SELF, &usage)
28 let data = try? JSONSerialization.data(withJSONObject: ["peak_physical_footprint_bytes": peak, "peak_rss_bytes": UInt64(usage.ru_maxrss)], options: .sortedKeys)
29 try? data?.write(to: url, options: .atomic)
30 }
31 deinit { sampling?.cancel() }
32}
33#endif
34