tests/benchmarks/: Add poll stress test.
[platform/upstream/gstreamer.git] / tests / benchmarks / gstpollstress.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <stdlib.h>
22 #include <gst/gst.h>
23
24 static GstPoll *set;
25 static GList *fds = NULL;
26 static GMutex *fdlock;
27 static GTimer *timer;
28
29 #define MAX_THREADS  100
30
31 static void
32 mess_some_more ()
33 {
34   GList *walk;
35   gint random;
36   gint removed = 0;
37
38   g_mutex_lock (fdlock);
39
40   for (walk = fds; walk;) {
41     GstPollFD *fd = (GstPollFD *) walk->data;
42
43     walk = g_list_next (walk);
44
45     random = (gint) (10.0 * rand () / (RAND_MAX + 1.0));
46     switch (random) {
47       case 0:
48       {
49         /*
50            GstPollFD *newfd = g_new0 (GstPollFD, 1);
51
52            gst_poll_add_fd (set, newfd);
53            fds = g_list_prepend (fds, newfd);
54          */
55         break;
56       }
57       case 1:
58         if ((gint) (10.0 * rand () / (RAND_MAX + 1.0)) < 2) {
59           gst_poll_remove_fd (set, fd);
60           fds = g_list_remove (fds, fd);
61           g_free (fd);
62           removed++;
63         }
64         break;
65
66       case 2:
67         gst_poll_fd_ctl_write (set, fd, TRUE);
68         break;
69       case 3:
70         gst_poll_fd_ctl_write (set, fd, FALSE);
71         break;
72       case 4:
73         gst_poll_fd_ctl_read (set, fd, TRUE);
74         break;
75       case 5:
76         gst_poll_fd_ctl_read (set, fd, FALSE);
77         break;
78
79       case 6:
80         gst_poll_fd_has_closed (set, fd);
81         break;
82       case 7:
83         gst_poll_fd_has_error (set, fd);
84         break;
85       case 8:
86         gst_poll_fd_can_read (set, fd);
87         break;
88       case 9:
89         gst_poll_fd_can_write (set, fd);
90         break;
91       default:
92         g_assert_not_reached ();
93         break;
94     }
95   }
96   if (g_list_length (fds) < 900) {
97     random = removed + (gint) (2.0 * rand () / (RAND_MAX + 1.0));
98     while (random) {
99       GstPollFD *newfd = g_new0 (GstPollFD, 1);
100
101       gst_poll_add_fd (set, newfd);
102       fds = g_list_prepend (fds, newfd);
103       random--;
104     }
105   }
106
107   g_mutex_unlock (fdlock);
108 }
109
110 void *
111 run_test (void *threadid)
112 {
113   gint id = GPOINTER_TO_INT (threadid);
114
115   while (TRUE) {
116     if (id == 0) {
117       gint res = gst_poll_wait (set, 10);
118
119       if (res < 0) {
120         g_print ("error %d %s\n", errno, g_strerror (errno));
121       }
122     } else {
123       mess_some_more ();
124       if (g_timer_elapsed (timer, NULL) > 0.5) {
125         g_mutex_lock (fdlock);
126         g_print ("active fds :%d\n", g_list_length (fds));
127         g_timer_start (timer);
128         g_mutex_unlock (fdlock);
129       }
130       g_usleep (1);
131     }
132   }
133
134   g_thread_exit (NULL);
135   return NULL;
136 }
137
138 gint
139 main (gint argc, gchar * argv[])
140 {
141   GThread *threads[MAX_THREADS];
142   gint num_threads;
143   gint t;
144
145   gst_init (&argc, &argv);
146
147   fdlock = g_mutex_new ();
148   timer = g_timer_new ();
149
150   if (argc != 2) {
151     g_print ("usage: %s <num_threads>\n", argv[0]);
152     exit (-1);
153   }
154
155   num_threads = atoi (argv[1]);
156
157   set = gst_poll_new (GST_POLL_MODE_POLL, TRUE);
158
159   for (t = 0; t < num_threads; t++) {
160     GError *error = NULL;
161
162     threads[t] = g_thread_create (run_test, GINT_TO_POINTER (t), TRUE, &error);
163     if (error) {
164       printf ("ERROR: g_thread_create() %s\n", error->message);
165       exit (-1);
166     }
167   }
168   printf ("main(): Created %d threads.\n", t);
169
170   for (t = 0; t < num_threads; t++) {
171     g_thread_join (threads[t]);
172   }
173
174   gst_poll_free (set);
175
176   return 0;
177 }