progress/trspeed: use a local convenient pointer to beautify code

The function becomes easier to read and understand with less repetition.
This commit is contained in:
Daniel Stenberg 2021-05-08 13:14:42 +02:00
parent 8a75224a69
commit 1e19eceb50
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -382,76 +382,69 @@ static curl_off_t trspeed(curl_off_t size, /* number of bytes */
static bool progress_calc(struct Curl_easy *data, struct curltime now)
{
bool timetoshow = FALSE;
struct Progress * const p = &data->progress;
/* The time spent so far (from the start) in microseconds */
data->progress.timespent = Curl_timediff_us(now, data->progress.start);
data->progress.dlspeed = trspeed(data->progress.downloaded,
data->progress.timespent);
data->progress.ulspeed = trspeed(data->progress.uploaded,
data->progress.timespent);
p->timespent = Curl_timediff_us(now, p->start);
p->dlspeed = trspeed(p->downloaded, p->timespent);
p->ulspeed = trspeed(p->uploaded, p->timespent);
/* Calculations done at most once a second, unless end is reached */
if(data->progress.lastshow != now.tv_sec) {
if(p->lastshow != now.tv_sec) {
int countindex; /* amount of seconds stored in the speeder array */
int nowindex = data->progress.speeder_c% CURR_TIME;
data->progress.lastshow = now.tv_sec;
int nowindex = p->speeder_c% CURR_TIME;
p->lastshow = now.tv_sec;
timetoshow = TRUE;
/* Let's do the "current speed" thing, with the dl + ul speeds
combined. Store the speed at entry 'nowindex'. */
data->progress.speeder[ nowindex ] =
data->progress.downloaded + data->progress.uploaded;
p->speeder[ nowindex ] = p->downloaded + p->uploaded;
/* remember the exact time for this moment */
data->progress.speeder_time [ nowindex ] = now;
p->speeder_time [ nowindex ] = now;
/* advance our speeder_c counter, which is increased every time we get
here and we expect it to never wrap as 2^32 is a lot of seconds! */
data->progress.speeder_c++;
p->speeder_c++;
/* figure out how many index entries of data we have stored in our speeder
array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
transfer. Imagine, after one second we have filled in two entries,
after two seconds we've filled in three entries etc. */
countindex = ((data->progress.speeder_c >= CURR_TIME)?
CURR_TIME:data->progress.speeder_c) - 1;
countindex = ((p->speeder_c >= CURR_TIME)? CURR_TIME:p->speeder_c) - 1;
/* first of all, we don't do this if there's no counted seconds yet */
if(countindex) {
int checkindex;
timediff_t span_ms;
curl_off_t amount;
/* Get the index position to compare with the 'nowindex' position.
Get the oldest entry possible. While we have less than CURR_TIME
entries, the first entry will remain the oldest. */
checkindex = (data->progress.speeder_c >= CURR_TIME)?
data->progress.speeder_c%CURR_TIME:0;
checkindex = (p->speeder_c >= CURR_TIME)? p->speeder_c%CURR_TIME:0;
/* Figure out the exact time for the time span */
span_ms = Curl_timediff(now, data->progress.speeder_time[checkindex]);
span_ms = Curl_timediff(now, p->speeder_time[checkindex]);
if(0 == span_ms)
span_ms = 1; /* at least one millisecond MUST have passed */
/* Calculate the average speed the last 'span_ms' milliseconds */
{
curl_off_t amount = data->progress.speeder[nowindex]-
data->progress.speeder[checkindex];
amount = p->speeder[nowindex]- p->speeder[checkindex];
if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
/* the 'amount' value is bigger than would fit in 32 bits if
multiplied with 1000, so we use the double math for this */
data->progress.current_speed = (curl_off_t)
((double)amount/((double)span_ms/1000.0));
else
/* the 'amount' value is small enough to fit within 32 bits even
when multiplied with 1000 */
data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
}
if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
/* the 'amount' value is bigger than would fit in 32 bits if
multiplied with 1000, so we use the double math for this */
p->current_speed = (curl_off_t)
((double)amount/((double)span_ms/1000.0));
else
/* the 'amount' value is small enough to fit within 32 bits even
when multiplied with 1000 */
p->current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
}
else
/* the first second we use the average */
data->progress.current_speed =
data->progress.ulspeed + data->progress.dlspeed;
p->current_speed = p->ulspeed + p->dlspeed;
} /* Calculations end */
return timetoshow;