forked from johnno1962/SwiftTrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftStats.swift
75 lines (66 loc) · 2.33 KB
/
SwiftStats.swift
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
//
// SwiftStats.swift
// SwiftTrace
//
// Created by John Holdsworth on 23/09/2020.
// Copyright © 2020 John Holdsworth. All rights reserved.
//
// Obtaining invocation statistics
// ===============================
//
// $Id: //depot/SwiftTrace/SwiftTrace/SwiftStats.swift#6 $
//
import Foundation
extension SwiftTrace {
func populate(elapsedTimes: inout [String: Double]) {
previousSwiftTrace?.populate(elapsedTimes: &elapsedTimes)
for (_, swizzle) in activeSwizzles {
elapsedTimes[swizzle.signature] = swizzle.totalElapsed
}
}
/**
Accumulated amount of time spent in each swizzled method.
*/
public static func elapsedTimes() -> [String: Double] {
var elapsedTimes = [String: Double]()
lastSwiftTrace.populate(elapsedTimes: &elapsedTimes)
return elapsedTimes
}
/**
Sorted descending accumulated amount of time spent in each swizzled method.
*/
public static func sortedElapsedTimes(onlyFirst: Int? = nil) -> [(key: String, value: TimeInterval)] {
let sorted = elapsedTimes().sorted { $1.value < $0.value }
return onlyFirst != nil ? Array(sorted.prefix(onlyFirst!)) : sorted
}
func populate(invocationCounts: inout [String: Int]) {
previousSwiftTrace?.populate(invocationCounts: &invocationCounts)
for (_, swizzle) in activeSwizzles {
invocationCounts[swizzle.signature] = swizzle.invocationCount
}
}
/**
Numbers of times each swizzled method has been invoked.
*/
public static func invocationCounts() -> [String: Int] {
var invocationCounts = [String: Int]()
lastSwiftTrace.populate(invocationCounts: &invocationCounts)
return invocationCounts
}
/**
Sorted descending numbers of times each swizzled method has been invoked.
*/
public static func sortedInvocationCounts(onlyFirst: Int? = nil) -> [(key: String, value: Int)] {
let sorted = invocationCounts().sorted { $1.value < $0.value }
return onlyFirst != nil ? Array(sorted.prefix(onlyFirst!)) : sorted
}
public static func callOrder() -> [Swizzle] {
var calls = [Swizzle]()
var call = firstCalled
while call != nil {
calls.append(call!)
call = call!.nextCalled
}
return calls
}
}