Add some more cases to the app-id unit tests
[glib.git] / tests / timeloop-basic.c
blob2c11bc506eace7034ea67340a278ce1eab2db50b
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <sys/resource.h>
10 #include <sys/time.h>
11 #include <poll.h>
13 #define TRUE 1
14 #define FALSE 0
16 static int n_children = 3;
17 static int n_active_children;
18 static int n_iters = 10000;
20 static int write_fds[1024];
21 static struct pollfd poll_fds[1024];
23 void
24 my_pipe (int *fds)
26 if (pipe(fds) < 0)
28 fprintf (stderr, "Cannot create pipe %s\n", strerror (errno));
29 exit (1);
33 int
34 read_all (int fd, char *buf, int len)
36 size_t bytes_read = 0;
37 ssize_t count;
39 while (bytes_read < len)
41 count = read (fd, buf + bytes_read, len - bytes_read);
42 if (count < 0)
44 if (errno != EAGAIN)
45 return FALSE;
47 else if (count == 0)
48 return FALSE;
50 bytes_read += count;
53 return TRUE;
56 int
57 write_all (int fd, char *buf, int len)
59 size_t bytes_written = 0;
60 ssize_t count;
62 while (bytes_written < len)
64 count = write (fd, buf + bytes_written, len - bytes_written);
65 if (count < 0)
67 if (errno != EAGAIN)
68 return FALSE;
71 bytes_written += count;
74 return TRUE;
77 void
78 run_child (int in_fd, int out_fd)
80 int i;
81 int val = 1;
83 for (i = 0; i < n_iters; i++)
85 write_all (out_fd, (char *)&val, sizeof (val));
86 read_all (in_fd, (char *)&val, sizeof (val));
89 val = 0;
90 write_all (out_fd, (char *)&val, sizeof (val));
92 exit (0);
95 int
96 input_callback (int source, int dest)
98 int val;
100 if (!read_all (source, (char *)&val, sizeof(val)))
102 fprintf (stderr,"Unexpected EOF\n");
103 exit (1);
106 if (val)
108 write_all (dest, (char *)&val, sizeof(val));
109 return TRUE;
111 else
113 close (source);
114 close (dest);
116 n_active_children--;
117 return FALSE;
121 void
122 create_child (int pos)
124 int pid;
125 int in_fds[2];
126 int out_fds[2];
128 my_pipe (in_fds);
129 my_pipe (out_fds);
131 pid = fork ();
133 if (pid > 0) /* Parent */
135 close (in_fds[0]);
136 close (out_fds[1]);
138 write_fds[pos] = in_fds[1];
139 poll_fds[pos].fd = out_fds[0];
140 poll_fds[pos].events = POLLIN;
142 else if (pid == 0) /* Child */
144 close (in_fds[1]);
145 close (out_fds[0]);
147 setsid ();
149 run_child (in_fds[0], out_fds[1]);
151 else /* Error */
153 fprintf (stderr,"Cannot fork: %s\n", strerror (errno));
154 exit (1);
158 static double
159 difftimeval (struct timeval *old, struct timeval *new)
161 return
162 (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
165 int
166 main (int argc, char **argv)
168 int i, j;
169 struct rusage old_usage;
170 struct rusage new_usage;
172 if (argc > 1)
173 n_children = atoi(argv[1]);
175 if (argc > 2)
176 n_iters = atoi(argv[2]);
178 printf ("Children: %d Iters: %d\n", n_children, n_iters);
180 n_active_children = n_children;
181 for (i = 0; i < n_children; i++)
182 create_child (i);
184 getrusage (RUSAGE_SELF, &old_usage);
186 while (n_active_children > 0)
188 int old_n_active_children = n_active_children;
190 poll (poll_fds, n_active_children, -1);
192 for (i=0; i<n_active_children; i++)
194 if (poll_fds[i].events & (POLLIN | POLLHUP))
196 if (!input_callback (poll_fds[i].fd, write_fds[i]))
197 write_fds[i] = -1;
201 if (old_n_active_children > n_active_children)
203 j = 0;
204 for (i=0; i<old_n_active_children; i++)
206 if (write_fds[i] != -1)
208 if (j < i)
210 poll_fds[j] = poll_fds[i];
211 write_fds[j] = write_fds[i];
213 j++;
219 getrusage (RUSAGE_SELF, &new_usage);
221 printf ("Elapsed user: %g\n",
222 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
223 printf ("Elapsed system: %g\n",
224 difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
225 printf ("Elapsed total: %g\n",
226 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
227 difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
228 printf ("total / iteration: %g\n",
229 (difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
230 difftimeval (&old_usage.ru_stime, &new_usage.ru_stime)) /
231 (n_iters * n_children));
233 return 0;