Skip to content

Commit

Permalink
bpersec: fix recent speed measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
XPhyro committed Sep 18, 2023
1 parent 19a0ce7 commit c35a334
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/c/util/core/bpersec.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef struct timespec timespec_t;
enum { NS_PER_SECOND = 1000000000 };

ssize_t totread = 0;
ssize_t lastread;
ssize_t lastread = 0;
timespec_t inittime, lasttime;
unit_t optunit = UNIT_BYTE;

Expand All @@ -35,13 +35,13 @@ void clock_gettimediff(timespec_t *ti, timespec_t *tf, timespec_t *td)
}
}

void printdiff(timespec_t *ti, timespec_t *tf, char sep)
void printdiff(timespec_t *ti, timespec_t *tf, ssize_t nread, char sep)
{
static timespec_t td;

clock_gettimediff(ti, tf, &td);

double diff = (double)(optunit == UNIT_BYTE ? totread : totread * 8) /
double diff = (double)(optunit == UNIT_BYTE ? nread : nread * 8) /
((double)td.tv_sec + (double)td.tv_nsec / (double)NS_PER_SECOND);

printf("%ld.%09ld %.16e%c", td.tv_sec, td.tv_nsec, diff, sep);
Expand All @@ -53,8 +53,8 @@ void printdiffs(void)

clock_gettime(CLOCK_REALTIME, &tf);

printdiff(&lasttime, &tf, '|');
printdiff(&inittime, &tf, '\n');
printdiff(&lasttime, &tf, lastread, '|');
printdiff(&inittime, &tf, totread, '\n');
lasttime = tf;
}

Expand Down Expand Up @@ -126,8 +126,11 @@ int main(int argc, char *argv[])
lasttime = inittime;
while ((nread = read(STDIN_FILENO, buf, PIPE_BUF)) > 0) {
totread += nread;
if (!(i++ % optcycle))
lastread += nread;
if (!(i++ % optcycle)) {
printdiffs();
lastread = 0;
}
}
printdiffs();

Expand Down

0 comments on commit c35a334

Please sign in to comment.