9 static struct timeval tp1,tp2;
11 static void start_timer()
13 gettimeofday(&tp1,NULL);
16 static double end_timer()
18 gettimeofday(&tp2,NULL);
19 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
20 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
24 static pid_t piped_child(const char *command[], int *f_in, int *f_out)
28 int from_child_pipe[2];
30 if (pipe(to_child_pipe) < 0 || pipe(from_child_pipe) < 0) {
42 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
43 close(to_child_pipe[1]) < 0 ||
44 close(from_child_pipe[0]) < 0 ||
45 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
46 printf("Failed to dup/close");
49 if (to_child_pipe[0] != 0)
50 close(to_child_pipe[0]);
51 if (from_child_pipe[1] != 1)
52 close(from_child_pipe[1]);
53 execvp(command[0], command);
54 printf("Failed to exec %s", command[0]);
58 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
59 printf("Failed to close");
63 *f_in = from_child_pipe[0];
64 *f_out = to_child_pipe[1];
70 int main(int argc, const char *argv[])
74 double max_latency=0.0, min_latency=0.0, total_latency=0.0;
75 unsigned num_calls = 0;
76 char buf[] = "testing0\n";
78 int len = strlen(buf);
81 pid = piped_child(argv+1, &fd_in, &fd_out);
83 /* ignore first round trip */
84 if (write(fd_out, buf, len) != len) {
85 printf("write error\n");
88 if (read(fd_in, buf2, len) != len) {
89 printf("read error\n");
95 buf[len-2] = num_calls%10;
97 if (write(fd_out, buf, len) != len) {
98 printf("write error\n");
101 if (read(fd_in, buf2, len) != len) {
102 printf("read error\n");
106 if (memcmp(buf, buf2, len) != 0) {
107 printf("Wrong data: %s\n", buf2);
109 if (lat < min_latency || min_latency == 0.0) {
112 if (lat > max_latency) {
115 total_latency += lat;
117 if (total_latency > print_count+1) {
119 printf("Calls: %6u Latency: min=%.6f max=%.6f avg=%.6f\n",
121 min_latency, max_latency, total_latency/num_calls);