Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Oct 29, 2024
1 parent df16f76 commit 9fa8433
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let package = Package(
.package(url: "https://github.com/StanfordSpezi/Spezi", from: "1.8.0"),
.package(url: "https://github.com/StanfordSpezi/SpeziViews", from: "1.7.0"),
.package(url: "https://github.com/StanfordSpezi/SpeziStorage", from: "1.1.2"),
.package(url: "https://github.com/StanfordSpezi/SpeziNotifications.git", branch: "feature/initial-version"),
.package(url: "https://github.com/StanfordSpezi/SpeziNotifications.git", from: "1.0.2"),
.package(url: "https://github.com/apple/swift-algorithms.git", from: "1.2.0"),
.package(url: "https://github.com/swiftlang/swift-syntax", from: "600.0.0-prerelease-2024-08-14"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.17.2")
Expand Down
38 changes: 0 additions & 38 deletions Sources/SpeziScheduler/EventQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Combine
import OSLog
import SwiftData
import SwiftUI

Expand Down Expand Up @@ -182,40 +181,3 @@ extension EventQuery: DynamicProperty {
}
}
}


private let logger = Logger(subsystem: "edu.stanford.spezi.scheduler", category: "EventQuery")


private func measure<T, C: Clock>(
clock: C = ContinuousClock(),
name: @autoclosure @escaping () -> StaticString,
_ action: () throws -> T
) rethrows -> T where C.Instant.Duration == Duration {
#if DEBUG || TEST
let start = clock.now
let result = try action()
let end = clock.now
logger.debug("Performing \(name()) took \(start.duration(to: end))")
return result
#else
try action()
#endif
}

func measure<T, C: Clock>(
isolation: isolated (any Actor)? = #isolation,
clock: C = ContinuousClock(),
name: @autoclosure @escaping () -> StaticString,
_ action: () async throws -> sending T
) async rethrows -> sending T where C.Instant.Duration == Duration {
#if DEBUG || TEST
let start = clock.now
let result = try await action()
let end = clock.now
logger.debug("Performing \(name()) took \(start.duration(to: end))")
return result
#else
try await action()
#endif
}
18 changes: 8 additions & 10 deletions Sources/SpeziScheduler/Scheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,18 @@ public final class Scheduler {
return // we have a container injected for testing purposes
}

let testing: Bool
let configuration: ModelConfiguration
#if targetEnvironment(simulator) || TEST
testing = true
configuration = ModelConfiguration(isStoredInMemoryOnly: true)
#elseif os(macOS)
testing = Self.isTesting
#else
testing = false
#endif

let configuration: ModelConfiguration = if testing {
ModelConfiguration(isStoredInMemoryOnly: true)
if Self.isTesting {
configuration = ModelConfiguration(isStoredInMemoryOnly: true)
} else {
ModelConfiguration(url: URL.documentsDirectory.appending(path: "edu.stanford.spezi.scheduler.storage.sqlite"))
configuration = ModelConfiguration(url: URL.documentsDirectory.appending(path: "edu.stanford.spezi.scheduler.storage.sqlite"))
}
#else
configuration = ModelConfiguration(url: URL.documentsDirectory.appending(path: "edu.stanford.spezi.scheduler.storage.sqlite"))
#endif

do {
_container = .success(try ModelContainer(for: Task.self, Outcome.self, configurations: configuration))
Expand Down
47 changes: 47 additions & 0 deletions Sources/SpeziScheduler/Utils/Measure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// This source file is part of the Stanford Spezi open-source project
//
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import OSLog


private let logger = Logger(subsystem: "edu.stanford.spezi.scheduler", category: "EventQuery")


func measure<T, C: Clock>(
clock: C = ContinuousClock(),
name: @autoclosure @escaping () -> StaticString,
_ action: () throws -> T
) rethrows -> T where C.Instant.Duration == Duration {
#if DEBUG || TEST
let start = clock.now
let result = try action()
let end = clock.now
logger.debug("Performing \(name()) took \(start.duration(to: end))")
return result
#else
try action()
#endif
}


func measure<T, C: Clock>(
isolation: isolated (any Actor)? = #isolation,
clock: C = ContinuousClock(),
name: @autoclosure @escaping () -> StaticString,
_ action: () async throws -> sending T
) async rethrows -> sending T where C.Instant.Duration == Duration {
#if DEBUG || TEST
let start = clock.now
let result = try await action()
let end = clock.now
logger.debug("Performing \(name()) took \(start.duration(to: end))")
return result
#else
try await action()
#endif
}

0 comments on commit 9fa8433

Please sign in to comment.