Skip to content

Commit

Permalink
refactor: 💡 log class diagram generation time
Browse files Browse the repository at this point in the history
also log time how to long to fetch file information from SourceKitten
  • Loading branch information
MarcoEidinger committed Nov 3, 2021
1 parent e9b8786 commit b7de564
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions Sources/SwiftPlantUMLFramework/ClassDiagramGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ public struct ClassDiagramGenerator {
/// - presenter: outputs the PlantUMLScript / Digram e.g. `PlantUMLBrowserPresenter` or `PlantUMLConsolePresenter`
/// - sdkPath: MacOSX SDK path used to handle type inference resolution
public func generate(for paths: [String], with configuration: Configuration = .default, presentedBy presenter: PlantUMLPresenting = PlantUMLBrowserPresenter(), sdkPath: String? = nil) {
outputDiagram(for: generateScript(for: fileCollector.getFiles(for: paths), with: configuration, sdkPath: sdkPath), with: presenter)
let startDate = Date()
outputDiagram(for: generateScript(for: fileCollector.getFiles(for: paths), with: configuration, sdkPath: sdkPath), with: presenter, processingStartDate: startDate)
}

/// generate diagram from a String containing Swift code
/// - Parameters:
/// - content: representing a string containing Swift code
/// - presenter: outputs the PlantUMLScript / Digram e.g. `PlantUMLBrowserPresenter` or `PlantUMLConsolePresenter`
public func generate(from content: String, with configuration: Configuration = .default, presentedBy presenter: PlantUMLPresenting = PlantUMLBrowserPresenter()) {
outputDiagram(for: generateScript(for: content, with: configuration), with: presenter)
let startDate = Date()
outputDiagram(for: generateScript(for: content, with: configuration), with: presenter, processingStartDate: startDate)
}

func generateScript(for content: String, with configuration: Configuration = .default) -> PlantUMLScript {
Expand All @@ -46,11 +48,16 @@ public struct ClassDiagramGenerator {
return PlantUMLScript(items: allValidItems, configuration: configuration)
}

func outputDiagram(for script: PlantUMLScript, with presenter: PlantUMLPresenting) {
func outputDiagram(for script: PlantUMLScript, with presenter: PlantUMLPresenting, processingStartDate date: Date) {
logProcessingDuration(started: date)
let semaphore = DispatchSemaphore(value: 0)
presenter.present(script: script) {
semaphore.signal()
}
semaphore.wait()
}

func logProcessingDuration(started processingStartDate: Date) {
Logger.shared.info("Class diagram generated in \(Date().timeIntervalSince(processingStartDate)) seconds and will be presented now")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ internal extension SyntaxStructure {
}

private static func createStructure(from file: File) -> SyntaxStructure? {
Logger.shared.info("no type-inference")
let structure = try! Structure(file: file) // swiftlint:disable:this force_try
let jsonData = structure.description.data(using: .utf8)!
return try! JSONDecoder().decode(SyntaxStructure.self, from: jsonData) // swiftlint:disable:this force_try
}

static func create(from fileOnDisk: URL, sdkPath: String? = nil) -> SyntaxStructure? {
let methodStart = Date()
guard let file = File(path: fileOnDisk.path) else {
Logger.shared.error("not able to read contents of file \(fileOnDisk)")
return nil
}
return create(from: file, sdkPath: sdkPath)
let structure = create(from: file, sdkPath: sdkPath)
let methodFinish = Date()
let executionTime = methodFinish.timeIntervalSince(methodStart)
Logger.shared.debug("read \(fileOnDisk) \((sdkPath != nil && !sdkPath!.isEmpty) ? "parsing with SDK" : "") in \(executionTime)")
return structure
}

static func create(from contents: String) -> SyntaxStructure? {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftPlantUMLFramework/PlantUMLScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public struct PlantUMLScript {
internal init(items: [SyntaxStructure], configuration: Configuration = .default) {
context = PlantUMLContext(configuration: configuration)

let methodStart = Date()

let STR2REPLACE = "STR2REPLACE"

// let plantumlTemplate = """
Expand Down Expand Up @@ -49,6 +51,8 @@ public struct PlantUMLScript {
let neep = replacingText + "\n" + context.connections.joined(separator: "\n") + "\n" + context.extnConnections.joined(separator: "\n")

text = plantumlTemplate.replacingOccurrences(of: STR2REPLACE, with: neep)

Logger.shared.debug("PlantUML script created in \(Date().timeIntervalSince(methodStart)) seconds")
}

func encodedText(completionHandler: @escaping (Result<String, NetworkError>) -> Void) {
Expand Down

0 comments on commit b7de564

Please sign in to comment.