2020@interface QNNTcpPingResult ()
2121
2222- (instancetype )init : (NSInteger )code
23+ ip : (NSString *)ip
2324 max : (NSTimeInterval )maxTime
2425 min : (NSTimeInterval )minTime
2526 avg : (NSTimeInterval )avgTime
26- count : (NSInteger )count ;
27+ loss : (NSInteger )loss
28+ count : (NSInteger )count
29+ totalTime : (NSTimeInterval )totalTime
30+ stddev : (NSTimeInterval )stddev ;
2731@end
2832
2933@implementation QNNTcpPingResult
@@ -36,16 +40,24 @@ - (NSString *)description {
3640}
3741
3842- (instancetype )init : (NSInteger )code
43+ ip : (NSString *)ip
3944 max : (NSTimeInterval )maxTime
4045 min : (NSTimeInterval )minTime
4146 avg : (NSTimeInterval )avgTime
42- count : (NSInteger )count {
47+ loss : (NSInteger )loss
48+ count : (NSInteger )count
49+ totalTime : (NSTimeInterval )totalTime
50+ stddev : (NSTimeInterval )stddev {
4351 if (self = [super init ]) {
4452 _code = code;
53+ _ip = ip;
4554 _minTime = minTime;
4655 _avgTime = avgTime;
4756 _maxTime = maxTime;
57+ _loss = loss;
4858 _count = count;
59+ _totalTime = totalTime;
60+ _stddev = stddev;
4961 }
5062 return self;
5163}
@@ -82,6 +94,7 @@ - (instancetype)init:(NSString *)host
8294}
8395
8496- (void )run {
97+ NSDate *begin = [NSDate date ];
8598 [self .output write: [NSString stringWithFormat: @" connect to host %@ :%lu ...\n " , _host, (unsigned long )_port]];
8699 struct sockaddr_in addr;
87100 memset (&addr, 0 , sizeof (addr));
@@ -95,18 +108,19 @@ - (void)run {
95108 [self .output write: @" Problem accessing the DNS" ];
96109 if (_complete != nil ) {
97110 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 ]);
99112 });
100113 }
101114 return ;
102115 }
103116 addr.sin_addr = *(struct in_addr *)host->h_addr ;
104117 [self .output write: [NSString stringWithFormat: @" connect to ip %s :%lu ...\n " , inet_ntoa (addr.sin_addr), (unsigned long )_port]];
105118 }
106-
119+ NSString *ip = [ NSString stringWithUTF8String: inet_ntoa (addr.sin_addr)];
107120 NSTimeInterval *intervals = (NSTimeInterval *)malloc (sizeof (NSTimeInterval ) * _count);
108121 int index = 0 ;
109122 int r = 0 ;
123+ int loss = 0 ;
110124 do {
111125 NSDate *t1 = [NSDate date ];
112126 r = [self connect: &addr];
@@ -116,6 +130,7 @@ - (void)run {
116130 [self .output write: [NSString stringWithFormat: @" connected to %s :%lu , %f ms\n " , inet_ntoa (addr.sin_addr), (unsigned long )_port, duration * 1000 ]];
117131 } else {
118132 [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++;
119134 }
120135
121136 if (index < _count && !_stopped && r == 0 ) {
@@ -128,22 +143,27 @@ - (void)run {
128143 if (_stopped) {
129144 code = kQNNRequestStoped ;
130145 }
146+ __block NSDate *startDate = begin;
131147 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 ]);
133149 });
134150 }
135151 free (intervals);
136152}
137153
138154- (QNNTcpPingResult *)buildResult : (NSInteger )code
155+ ip : (NSString *)ip
139156 durations : (NSTimeInterval *)durations
140- count : (NSInteger )count {
157+ loss : (NSInteger )loss
158+ count : (NSInteger )count
159+ totalTime : (NSTimeInterval )time {
141160 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 ];
143162 }
144163 NSTimeInterval max = 0 ;
145164 NSTimeInterval min = 10000000 ;
146165 NSTimeInterval sum = 0 ;
166+ NSTimeInterval sum2 = 0 ;
147167 for (int i = 0 ; i < count; i++) {
148168 if (durations[i] > max) {
149169 max = durations[i];
@@ -152,9 +172,12 @@ - (QNNTcpPingResult *)buildResult:(NSInteger)code
152172 min = durations[i];
153173 }
154174 sum += durations[i];
175+ sum2 += durations[i] * durations[i];
155176 }
156177 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];
158181}
159182
160183- (int )connect : (struct sockaddr_in *)addr {
0 commit comments