使用AFNetworking计算带宽

时间:2013-10-17 17:10:33

标签: ios bandwidth afnetworking-2

我正在尝试使用AFNetworking粗略估算上传带宽。我有NSObject的子类来观察AFNetworkingOperationDidStartNotificationAFNetworkingOperationDidFinishNotification。我得到了在AFHTTPRequestOperation进度块中写入的总预期字节数:

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        if (lastUploadSize != totalBytesExpectedToWrite)
            lastUploadSize = totalBytesExpectedToWrite;
    }];

在开始和结束时,我将当前日期保存到各自的iVar中。然后我调用一个方法,calculateBandwidth进行一些简单的计算:

-(void)calculateBandwidth {
    NSTimeInterval difference = [uploadFinishTime timeIntervalSinceDate:uploadStartTime];
    float bandwidth = (lastUploadSize * 0.0078125) / difference;

    PLog(@"Bandwidth (calculated): %f kbps", bandwidth);
    if ([_delegate respondsToSelector:@selector(networkClientDidUpdateBandwidth:time:withSize:)])
        [_delegate networkClientDidUpdateBandwidth:bandwidth time:difference withSize:lastUploadSize];
}

但带宽结果似乎已经消失。 0.0078125千比特= 1字节,如果我没有记错的话;我在数学中遗漏了什么吗?

我计划使用类似的方法制作更复杂的带宽检测器,但现在希望在每次上传的基础上获得准确的上传带宽。

0 个答案:

没有答案