Skip to content

Commit

Permalink
Merge pull request #1821 from anyproto/ios-3180-support-debugrunprofi…
Browse files Browse the repository at this point in the history
…ler-in-debug-settings

IOS-3180 Support debug run profiler in debug menu
  • Loading branch information
ignatovv authored Jul 19, 2024
2 parents 0c3a7c4 + 4521710 commit 4b0d031
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
38 changes: 26 additions & 12 deletions Anytype/Sources/PresentationLayer/Debug/DebugMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,32 @@ struct DebugMenuView: View {

private var mainActions: some View {
VStack {
HStack {
StandardButton("Logs 🧻", style: .secondaryLarge) {
showLogs.toggle()
}
StandardButton("Logs 🧻", style: .secondaryLarge) {
showLogs.toggle()
}

StandardButton("Export localstore 📁", style: .secondaryLarge) {
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
model.getLocalStoreData()
}

if case .done(url: let url) = model.debugRunProfilerData {
StandardButton("Download Debug Run Profiler Data 💿", style: .secondaryLarge) {
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
model.shareUrlContent(url: url)
}
}
}
}

private var moreActions: some View {
VStack {
HStack {
StandardButton("Crash 🔥", style: .primaryLarge) {
let crash: [Int] = []
_ = crash[1]
}
StandardButton("Assert 🥲", style: .secondaryLarge) {
anytypeAssertionFailure("Test assert")
}
StandardButton("Crash 🔥", style: .primaryLarge) {
let crash: [Int] = []
_ = crash[1]
}
StandardButton("Assert 🥲", style: .secondaryLarge) {
anytypeAssertionFailure("Test assert")
}

StandardButton("Membership debug 💸", style: .secondaryLarge) {
Expand All @@ -119,6 +122,17 @@ struct DebugMenuView: View {
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
try await model.onSpaceDebug()
}

switch model.debugRunProfilerData {
case .empty, .done:
StandardButton("Run debug profiler 🤓", style: .secondaryLarge) {
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
model.onDebugRunProfiler()
}
case .inProgress:
Text("Profiling in progress ...")
}

StandardButton("Export full directory 🤐", style: .secondaryLarge) {
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
model.zipWorkingDirectory()
Expand Down
31 changes: 31 additions & 0 deletions Anytype/Sources/PresentationLayer/Debug/DebugMenuViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import AnytypeCore
import Services
import ZIPFoundation

enum DebugRunProfilerState: Codable {
case empty
case inProgress
case done(url: URL)
}

@MainActor
final class DebugMenuViewModel: ObservableObject {

Expand All @@ -12,6 +18,9 @@ final class DebugMenuViewModel: ObservableObject {
@Published var showZipPicker = false
@Published private(set) var flags = [FeatureFlagSection]()

@Published var debugRunProfilerData = DebugRunProfilerState.empty
@UserDefault("DebugRunProfiler", defaultValue: .empty) private var debugRunProfilerDataStore: DebugRunProfilerState

@Injected(\.debugService)
private var debugService: any DebugServiceProtocol
@Injected(\.localAuthService)
Expand All @@ -29,6 +38,7 @@ final class DebugMenuViewModel: ObservableObject {

init() {
updateFlags()
debugRunProfilerData = debugRunProfilerDataStore
}

func removeRecoveryPhraseFromDevice() {
Expand Down Expand Up @@ -93,6 +103,27 @@ final class DebugMenuViewModel: ObservableObject {
shareUrlFile = jsonFile
}

func onDebugRunProfiler() {
debugRunProfilerData = .inProgress
debugRunProfilerDataStore = .inProgress

Task.detached { [self] in
let path = try await debugService.debugRunProfiler()
let zipFile = FileManager.default.createTempDirectory().appendingPathComponent("debugRunProfiler.zip")
try FileManager.default.zipItem(at: URL(fileURLWithPath: path), to: zipFile)


Task { @MainActor in
debugRunProfilerData = .done(url: zipFile)
debugRunProfilerDataStore = .done(url: zipFile)
}
}
}

func shareUrlContent(url: URL) {
shareUrlFile = url
}

// MARK: - Private

private func updateFlags() {
Expand Down
7 changes: 7 additions & 0 deletions Modules/Services/Sources/Services/Debug/DebugService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public protocol DebugServiceProtocol: AnyObject, Sendable {
func exportLocalStore() async throws -> String
func exportStackGoroutines() async throws -> String
func exportSpaceDebug(spaceId: String) async throws -> String
func debugRunProfiler() async throws -> String
}

final class DebugService: DebugServiceProtocol {
Expand Down Expand Up @@ -37,4 +38,10 @@ final class DebugService: DebugServiceProtocol {
}).invoke()
return try result.jsonString()
}

public func debugRunProfiler() async throws -> String {
return try await ClientCommands.debugRunProfiler(.with {
$0.durationInSeconds = 60
}).invoke().path
}
}

0 comments on commit 4b0d031

Please sign in to comment.