-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLatencyTestTool.swift
79 lines (67 loc) · 3.12 KB
/
LatencyTestTool.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
76
77
78
79
//
// Copyright (C) 2023 Devexperts LLC. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
import Foundation
import DXFeedFramework
class LatencyTestTool: ToolsCommand {
var isTools: Bool = true
var cmd = "LatencyTest"
var shortDescription = "Connects to the specified address(es) and calculates latency."
var fullDescription: String =
"""
Connects to the specified address(es) and calculates latency.
Usage:
LatencyTest <address> <types> <symbols> [<options>]
Where:
address (pos. 0) Required. The address(es) to connect to retrieve data (see "Help address").
For Token-Based Authorization, use the following format: "<address>:<port>[login=entitle:<token>]".
types (pos. 1) Comma-separated list of dxfeed event types (e.g. Quote, TimeAndSale).
Use "all" for all available event types.
symbols (pos. 2) Comma-separated list of symbol names to get events for (e.g. "IBM, AAPL, MSFT").
Use "all" for wildcard subscription.
The "dxfeed.wildcard.enable" property must be set to true to enable wildcard subscription.
--force-stream Enforces a streaming contract for subscription. The StreamFeed role is used instead of Feed.
"""
var subscription = Subscription()
private lazy var arguments: Arguments = {
do {
let arguments = try Arguments(ProcessInfo.processInfo.arguments, requiredNumberOfArguments: 4)
return arguments
} catch {
print(fullDescription)
exit(0)
}
}()
func execute() {
let address = arguments[1]
let types = arguments.parseTypes(at: 2)
arguments.properties.forEach { key, value in
try? SystemProperty.setProperty(key, value)
}
let listener = LatencyEventListener()
let symbols = arguments.parseSymbols()
subscription.createSubscription(address: address,
symbols: symbols,
types: types,
role: arguments.isForceStream ? .streamFeed : .feed,
listeners: [listener],
properties: arguments.properties,
time: nil)
let timer = DXFTimer(timeInterval: 2)
let printers: [LatencyPrinter] = [LatencyMetricsPrinter(),
MonitoringStatsPrinter(numberOfSubscription: symbols.count * types.count,
address: address)]
timer.eventHandler = {
let metrics = listener.metrics()
listener.updateCpuUsage()
printers.forEach({ printer in
printer.update(metrics)
})
}
timer.resume()
// Calculate till input new line
_ = readLine()
}
}