Skip to content

Commit 7dd5b4e

Browse files
authored
Merge pull request #90 from virtan/protocolize-tracing-structs--convert-20200715T130643
Protocolize tracing structs, convert collection to class
2 parents 91ba087 + 0d4ada4 commit 7dd5b4e

File tree

2 files changed

+140
-106
lines changed

2 files changed

+140
-106
lines changed

Sources/TSCUtility/Tracing.swift

Lines changed: 132 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,127 +10,157 @@
1010

1111
import Foundation
1212

13-
public struct Tracing {
14-
public enum EventType {
15-
case asyncBegin
16-
case asyncEnd
13+
public enum TracingEventType {
14+
case asyncBegin
15+
case asyncEnd
16+
}
17+
18+
public protocol TracingEventProtocol {
19+
/// The category of the event.
20+
var cat: String { get }
21+
22+
/// The name of the event.
23+
var name: String { get }
24+
25+
/// The free form id of the event.
26+
var id: String { get }
27+
28+
/// The phase of the event.
29+
var ph: TracingEventType { get }
30+
31+
/// The process id of the process where the event occured.
32+
var pid: Int { get }
33+
34+
/// The thread id of the process where the event occured.
35+
var tid: Int { get }
36+
37+
/// The timestamp of the event.
38+
var ts: Int { get }
39+
40+
/// The start time of the process where the event occured.
41+
var startTs: Int { get }
42+
init(
43+
cat: String,
44+
name: String,
45+
id: String,
46+
ph: TracingEventType,
47+
pid: Int,
48+
tid: Int,
49+
ts: Int,
50+
startTs: Int
51+
)
52+
}
53+
54+
public protocol TracingCollectionProtocol {
55+
var events: [TracingEventProtocol] { get set }
56+
init(_ events: [TracingEventProtocol])
57+
}
58+
59+
extension TracingCollectionProtocol {
60+
public mutating func append(_ tracingCollection: TracingCollectionProtocol) {
61+
self.events.append(contentsOf: tracingCollection.events)
1762
}
63+
}
1864

19-
public struct Event {
20-
/// The category of the event.
21-
public let cat: String
22-
23-
/// The name of the event.
24-
public let name: String
25-
26-
/// The free form id of the event.
27-
public let id: String
28-
29-
/// The phase of the event.
30-
public let ph: EventType
31-
32-
/// The process id of the process where the event occured.
33-
public let pid: Int
34-
35-
/// The thread id of the process where the event occured.
36-
public let tid: Int
37-
38-
/// The timestamp of the event.
39-
public let ts: Int
40-
41-
/// The start time of the process where the event occured.
42-
public let startTs: Int
43-
44-
#if canImport(Darwin)
45-
public init(
46-
cat: String,
47-
name: String,
48-
id: String,
49-
ph: EventType,
50-
pid: Int = Int(getpid()),
51-
tid: Int = Int(pthread_mach_thread_np(pthread_self())),
52-
ts: Int = Int(DispatchTime.now().uptimeNanoseconds),
53-
startTs: Int = 0
54-
) {
55-
self.cat = cat
56-
self.name = name
57-
self.id = id
58-
self.ph = ph
59-
self.pid = pid
60-
self.tid = tid
61-
self.ts = ts
62-
self.startTs = startTs
63-
}
64-
#elseif canImport(Glibc)
65-
public init(
66-
cat: String,
67-
name: String,
68-
id: String,
69-
ph: EventType,
70-
pid: Int = Int(getpid()),
71-
tid: Int = 1,
72-
ts: Int = Int(DispatchTime.now().uptimeNanoseconds),
73-
startTs: Int = 0
74-
) {
75-
self.cat = cat
76-
self.name = name
77-
self.id = id
78-
self.ph = ph
79-
self.pid = pid
80-
self.tid = tid
81-
self.ts = ts
82-
self.startTs = startTs
83-
}
84-
#else
85-
public init(
86-
cat: String,
87-
name: String,
88-
id: String,
89-
ph: EventType,
90-
pid: Int = 1,
91-
tid: Int = 1,
92-
ts: Int = Int(DispatchTime.now().uptimeNanoseconds),
93-
startTs: Int = 0
94-
) {
95-
self.cat = cat
96-
self.name = name
97-
self.id = id
98-
self.ph = ph
99-
self.pid = pid
100-
self.tid = tid
101-
self.ts = ts
102-
self.startTs = startTs
103-
}
104-
#endif
65+
public struct TracingEvent: TracingEventProtocol {
66+
public let cat: String
67+
public let name: String
68+
public let id: String
69+
public let ph: TracingEventType
70+
public let pid: Int
71+
public let tid: Int
72+
public let ts: Int
73+
public let startTs: Int
74+
75+
#if canImport(Darwin)
76+
public init(
77+
cat: String,
78+
name: String,
79+
id: String,
80+
ph: TracingEventType,
81+
pid: Int = Int(getpid()),
82+
tid: Int = Int(pthread_mach_thread_np(pthread_self())),
83+
ts: Int = Int(DispatchTime.now().uptimeNanoseconds),
84+
startTs: Int = 0
85+
) {
86+
self.cat = cat
87+
self.name = name
88+
self.id = id
89+
self.ph = ph
90+
self.pid = pid
91+
self.tid = tid
92+
self.ts = ts
93+
self.startTs = startTs
94+
}
95+
#elseif canImport(Glibc)
96+
public init(
97+
cat: String,
98+
name: String,
99+
id: String,
100+
ph: TracingEventType,
101+
pid: Int = Int(getpid()),
102+
tid: Int = 1,
103+
ts: Int = Int(DispatchTime.now().uptimeNanoseconds),
104+
startTs: Int = 0
105+
) {
106+
self.cat = cat
107+
self.name = name
108+
self.id = id
109+
self.ph = ph
110+
self.pid = pid
111+
self.tid = tid
112+
self.ts = ts
113+
self.startTs = startTs
114+
}
115+
#else
116+
public init(
117+
cat: String,
118+
name: String,
119+
id: String,
120+
ph: TracingEventType,
121+
pid: Int = 1,
122+
tid: Int = 1,
123+
ts: Int = Int(DispatchTime.now().uptimeNanoseconds),
124+
startTs: Int = 0
125+
) {
126+
self.cat = cat
127+
self.name = name
128+
self.id = id
129+
self.ph = ph
130+
self.pid = pid
131+
self.tid = tid
132+
self.ts = ts
133+
self.startTs = startTs
105134
}
135+
#endif
136+
}
106137

107-
public struct Collection {
108-
public var events: [Event] = []
109-
public init(_ events: [Tracing.Event] = []) {
110-
self.events = events
111-
}
138+
public class TracingCollection: TracingCollectionProtocol {
139+
public var events: [TracingEventProtocol] = []
140+
public required init(_ events: [TracingEventProtocol] = []) {
141+
self.events = events
112142
}
113143
}
114144

115145
extension Context {
116-
public static func withTracing(_ collection: Tracing.Collection) -> Context {
117-
return Context(dictionaryLiteral: (ObjectIdentifier(Tracing.Collection.self), collection as Any))
146+
public static func withTracing(_ collection: TracingCollectionProtocol) -> Context {
147+
return Context(dictionaryLiteral: (ObjectIdentifier(TracingCollectionProtocol.self), collection as Any))
118148
}
119149

120-
public mutating func enrichWithTracing(_ collection: Tracing.Collection) -> Context {
121-
self[ObjectIdentifier(Tracing.Collection.self)] = collection
150+
public mutating func enrichWithTracing(_ collection: TracingCollectionProtocol) -> Context {
151+
self[ObjectIdentifier(TracingCollectionProtocol.self)] = collection
122152
return self
123153
}
124154

125-
public var tracing: Tracing.Collection? {
155+
public var tracing: TracingCollectionProtocol? {
126156
get {
127-
guard let collection = self[ObjectIdentifier(Tracing.Collection.self)] as? Tracing.Collection else {
157+
guard let collection = self[ObjectIdentifier(TracingCollectionProtocol.self)] as? TracingCollectionProtocol else {
128158
return nil
129159
}
130160
return collection
131161
}
132162
set {
133-
self[ObjectIdentifier(Tracing.Collection.self)] = newValue
163+
self[ObjectIdentifier(TracingCollectionProtocol.self)] = newValue
134164
}
135165
}
136166
}

Tests/TSCUtilityTests/TracingTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import TSCUtility
1212

1313
class TracingTests: XCTestCase {
1414
func testBasics() {
15-
let event1 = Tracing.Event(cat: "cat", name: "name", id: "1", ph: .asyncBegin)
16-
var collection = Tracing.Collection()
15+
let event1 = TracingEvent(cat: "cat", name: "name", id: "1", ph: .asyncBegin)
16+
var collection = TracingCollection()
1717
collection.events.append(event1)
18-
let event2 = Tracing.Event(cat: "cat", name: "name", id: "1", ph: .asyncEnd)
18+
let event2 = TracingEvent(cat: "cat", name: "name", id: "1", ph: .asyncEnd)
1919
collection.events.append(event2)
2020
XCTAssertEqual(collection.events.count, 2)
2121
var ctx = Context()
2222
ctx.tracing = collection
2323
XCTAssertEqual(ctx.tracing?.events.count, 2)
24-
24+
let collection2 = TracingCollection()
25+
collection2.events.append(event2)
26+
collection.append(collection2)
27+
XCTAssertEqual(collection.events.count, 3)
28+
XCTAssertEqual(ctx.tracing?.events.count, 3)
2529
}
2630
}

0 commit comments

Comments
 (0)