20
20
@interface QNNTcpPingResult ()
21
21
22
22
- (instancetype )init : (NSInteger )code
23
+ ip : (NSString *)ip
23
24
max : (NSTimeInterval )maxTime
24
25
min : (NSTimeInterval )minTime
25
26
avg : (NSTimeInterval )avgTime
26
- count : (NSInteger )count ;
27
+ loss : (NSInteger )loss
28
+ count : (NSInteger )count
29
+ totalTime : (NSTimeInterval )totalTime
30
+ stddev : (NSTimeInterval )stddev ;
27
31
@end
28
32
29
33
@implementation QNNTcpPingResult
@@ -36,16 +40,24 @@ - (NSString *)description {
36
40
}
37
41
38
42
- (instancetype )init : (NSInteger )code
43
+ ip : (NSString *)ip
39
44
max : (NSTimeInterval )maxTime
40
45
min : (NSTimeInterval )minTime
41
46
avg : (NSTimeInterval )avgTime
42
- count : (NSInteger )count {
47
+ loss : (NSInteger )loss
48
+ count : (NSInteger )count
49
+ totalTime : (NSTimeInterval )totalTime
50
+ stddev : (NSTimeInterval )stddev {
43
51
if (self = [super init ]) {
44
52
_code = code;
53
+ _ip = ip;
45
54
_minTime = minTime;
46
55
_avgTime = avgTime;
47
56
_maxTime = maxTime;
57
+ _loss = loss;
48
58
_count = count;
59
+ _totalTime = totalTime;
60
+ _stddev = stddev;
49
61
}
50
62
return self;
51
63
}
@@ -82,6 +94,7 @@ - (instancetype)init:(NSString *)host
82
94
}
83
95
84
96
- (void )run {
97
+ NSDate *begin = [NSDate date ];
85
98
[self .output write :[NSString stringWithFormat: @" connect to host %@ :%lu ...\n " , _host, (unsigned long )_port]];
86
99
struct sockaddr_in addr;
87
100
memset (&addr, 0 , sizeof (addr));
@@ -95,18 +108,19 @@ - (void)run {
95
108
[self .output write :@" Problem accessing the DNS" ];
96
109
if (_complete != nil ) {
97
110
dispatch_async (dispatch_get_main_queue (), ^(void ) {
98
- _complete ([self buildResult: -1006 durations: nil count: 0 ]);
111
+ _complete ([self buildResult: -1006 ip: nil durations: nil loss: 0 count: 0 totalTime :0 ]);
99
112
});
100
113
}
101
114
return ;
102
115
}
103
116
addr.sin_addr = *(struct in_addr *)host->h_addr ;
104
117
[self .output write :[NSString stringWithFormat: @" connect to ip %s :%lu ...\n " , inet_ntoa (addr.sin_addr), (unsigned long )_port]];
105
118
}
106
-
119
+ NSString *ip = [ NSString stringWithUTF8String: inet_ntoa (addr.sin_addr)];
107
120
NSTimeInterval *intervals = (NSTimeInterval *)malloc (sizeof (NSTimeInterval ) * _count);
108
121
int index = 0 ;
109
122
int r = 0 ;
123
+ int loss = 0 ;
110
124
do {
111
125
NSDate *t1 = [NSDate date ];
112
126
r = [self connect: &addr];
@@ -116,6 +130,7 @@ - (void)run {
116
130
[self .output write :[NSString stringWithFormat: @" connected to %s :%lu , %f ms\n " , inet_ntoa (addr.sin_addr), (unsigned long )_port, duration * 1000 ]];
117
131
} else {
118
132
[self .output write :[NSString stringWithFormat: @" connect failed to %s :%lu , %f ms, error %d \n " , inet_ntoa (addr.sin_addr), (unsigned long )_port, duration * 1000 , r]];
133
+ loss++;
119
134
}
120
135
121
136
if (index < _count && !_stopped && r == 0 ) {
@@ -128,22 +143,27 @@ - (void)run {
128
143
if (_stopped) {
129
144
code = kQNNRequestStoped ;
130
145
}
146
+ __block NSDate *startDate = begin;
131
147
dispatch_async (dispatch_get_main_queue (), ^(void ) {
132
- _complete ([self buildResult: code durations: intervals count: index ]);
148
+ _complete ([self buildResult: code ip: ip durations: intervals loss: loss count: index totalTime: [[ NSDate date ] timeIntervalSinceDate: startDate] * 1000 ]);
133
149
});
134
150
}
135
151
free (intervals);
136
152
}
137
153
138
154
- (QNNTcpPingResult *)buildResult : (NSInteger )code
155
+ ip : (NSString *)ip
139
156
durations : (NSTimeInterval *)durations
140
- count : (NSInteger )count {
157
+ loss : (NSInteger )loss
158
+ count : (NSInteger )count
159
+ totalTime : (NSTimeInterval )time {
141
160
if (code != 0 && code != kQNNRequestStoped ) {
142
- return [[QNNTcpPingResult alloc ] init: code max: 0 min: 0 avg: 0 count: 1 ];
161
+ return [[QNNTcpPingResult alloc ] init: code ip: ip max: 0 min: 0 avg: 0 loss: 1 count: 1 totalTime: time stddev: 0 ];
143
162
}
144
163
NSTimeInterval max = 0 ;
145
164
NSTimeInterval min = 10000000 ;
146
165
NSTimeInterval sum = 0 ;
166
+ NSTimeInterval sum2 = 0 ;
147
167
for (int i = 0 ; i < count; i++) {
148
168
if (durations[i] > max) {
149
169
max = durations[i];
@@ -152,9 +172,12 @@ - (QNNTcpPingResult *)buildResult:(NSInteger)code
152
172
min = durations[i];
153
173
}
154
174
sum += durations[i];
175
+ sum2 += durations[i] * durations[i];
155
176
}
156
177
NSTimeInterval avg = sum / count;
157
- return [[QNNTcpPingResult alloc ] init: code max: max min: min avg: avg count: count];
178
+ NSTimeInterval avg2 = sum2 / count;
179
+ NSTimeInterval stddev = sqrt (avg2 - avg * avg);
180
+ return [[QNNTcpPingResult alloc ] init: code ip: ip max: max min: min avg: avg loss: loss count: count totalTime: time stddev: stddev];
158
181
}
159
182
160
183
- (int )connect : (struct sockaddr_in *)addr {
0 commit comments