Skip to content

Commit

Permalink
Fix long startup problem without CPU thrashing (#79)
Browse files Browse the repository at this point in the history
* Fix 4 second pause on startup

* Remove debug

* Fix long startup problem

* Rename queue

* Avoid asyncAfter (CPU thrashing)

* merge

* merge
  • Loading branch information
sjanuary authored and mattcolegate committed Mar 16, 2017
1 parent 090c8f5 commit 02b65a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Sources/SwiftMetrics/SwiftMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@ open class SwiftMetrics {
private func testLatency() {
if(latencyEnabled) {
// Run every two seconds
jobsQueue.asyncAfter(deadline: .now() + .seconds(2), execute: {
jobsQueue.async {
sleep(2)
let preDispatchTime = Date().timeIntervalSince1970 * 1000;
DispatchQueue.global().async {
let timeNow = Date().timeIntervalSince1970 * 1000
let latencyTime = timeNow - preDispatchTime
self.emitData(LatencyData(timeOfSample: Int(preDispatchTime), duration:latencyTime))
self.testLatency()
}
})
}
}
}

Expand Down
23 changes: 9 additions & 14 deletions Sources/SwiftMetricsDash/SwiftMetricsDash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class SwiftMetricsService: WebSocketService {
monitor.on(sendMEM)
monitor.on(storeHTTP)
sendhttpData()
sendhttpURLData()
}


Expand Down Expand Up @@ -264,20 +263,14 @@ class SwiftMetricsService: WebSocketService {
"average":"\(localCopy.average)",
"total":"\(localCopy.total)"]])

for (_,connection) in self.connections {
if let messageToSend = httpLine.rawString() {
connection.send(message: messageToSend)
}
for (_,connection) in self.connections {
if let messageToSend = httpLine.rawString() {
connection.send(message: messageToSend)
}
}
self.httpAggregateData = HTTPAggregateData()
}
jobsQueue.asyncAfter(deadline: .now() + .seconds(2), execute: {
self.sendhttpData()
})
}
}

func sendhttpURLData() {
httpURLsQueue.sync {
var responseData:[JSON] = []
let localCopy = self.httpURLData
Expand All @@ -301,9 +294,11 @@ class SwiftMetricsService: WebSocketService {
connection.send(message: messageToSend2)
}
}
jobsQueue.asyncAfter(deadline: .now() + .seconds(2), execute: {
self.sendhttpURLData()
})
jobsQueue.async {
// re-run this function after 2 seconds
sleep(2)
self.sendhttpData()
}
}
}

Expand Down

0 comments on commit 02b65a1

Please sign in to comment.