2

I'm trying to measure actual transfer speed during ftp download, download itself is working, streams are hooked up in run loop. Measurment is done in NSStreamEventHasBytesAvailable using CFTimeGetCurrent on event start and at the end, after data is written to file elapsed time is computed with (double)previousTimestamp-CFAbsoluteTimeGetCurrent, but the time I get is absolutely unreasonable. Tested on simulator and device, can anyone enlighten me?

code:

    switch (eventCode) {
    case NSStreamEventOpenCompleted: {            
    } break;
    case NSStreamEventHasBytesAvailable: {

        if ([[self.fileStream propertyForKey:NSStreamFileCurrentOffsetKey] intValue]==0) {

            previousTimestamp = CFAbsoluteTimeGetCurrent();

        }

        NSInteger       bytesRead;
        uint8_t         buffer[32768];

        bytesRead = [self.networkStream read:buffer maxLength:sizeof(buffer)];


        if (bytesRead == -1) 
        {
            [self _stopReceiveWithStatus:@"Err"];
        } 
        else if (bytesRead == 0) 
        {

            [self _stopReceiveWithStatus:nil];
        } 
        else 
        {
            [self completition:bytesRead];

            NSInteger   bytesWritten;
            NSInteger   bytesWrittenSoFar;

            // Write to the file.

            bytesWrittenSoFar = 0;
            do {
                bytesWritten = [self.fileStream write:&buffer[bytesWrittenSoFar] maxLength:bytesRead - bytesWrittenSoFar];
                assert(bytesWritten != 0);
                if (bytesWritten == -1) {
                    [self _stopReceiveWithStatus:@"File err"];
                    break;
                } else {
                    bytesWrittenSoFar += bytesWritten;
                }
            } while (bytesWrittenSoFar != bytesRead);

            [self downloadSpeedSave:bytesRead :previousTimestamp-CFAbsoluteTimeGetCurrent()];


            previousTimestamp = CFAbsoluteTimeGetCurrent();
  • "but the time I get is absolutely unreasonable" -- what values do you get? What did you expect? Why? – Shaggy Frog Jun 02 '12 at 00:37
  • Well, my goal is to compute transfer rate in KB/s so when I divide bytesRead by elapsed time and by 1024 (to get KB) the result is too huge like 300000KB/s and that is not possible on my internet connection... –  Jun 02 '12 at 01:20
  • So what unit does "elapsed time" have? Seconds? Milliseconds? – Shaggy Frog Jun 02 '12 at 01:29

1 Answers1

1

An alternative that I have used is to use the time.h and c routines to capture time.

http://www.cplusplus.com/reference/clibrary/ctime/time/

Another good link on SO iPhone: How to get current milliseconds?

Community
  • 1
  • 1
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • I've tried the first link with no succes, c_time seems to be cpu - dependant. Second aproach gives much more realistic values, but still the time between operations seems to be to short =>computed bandwidth to high... –  Jun 02 '12 at 11:30
  • If Im correct, in your above approach you have the new time being subtracted from the previous. This would normally result in a difference that is negative because the new timestamp should always be GREATER than the older. Do you know how your "downloadSpeedSave" routine handles negative numbers? have you tried "CFAbsoluteTimeGetCurrent() - previousTimestamp"? Also, a more correct method would be to store the value returned from "CFAbsoluteTimeGetCurrent()" into a variable then use that for both your call to "downloadSpeedSave" and setting your "previousTimestamp"! – trumpetlicks Jun 03 '12 at 18:20
  • The fact that you are making 2 calls to CFAbsoluteTimeGetCurrent() means that you SHOULD be getting one returned value that you are using for the speed calculation and another that you are setting into previousTimeStamp. You really want to pick 1 point in time that you reference. – trumpetlicks Jun 03 '12 at 18:22