]> git.sesse.net Git - vlc/blob - modules/misc/testsuite/test4.c
* ./modules/misc/testsuite/test4.c: added a stress test module; doesn't get
[vlc] / modules / misc / testsuite / test4.c
1 /*****************************************************************************
2  * test4.c : Miscellaneous stress tests module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: test4.c,v 1.1 2002/10/14 16:34:17 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #include <stdlib.h>
30 #include <signal.h>
31
32 /*****************************************************************************
33  * Defines
34  *****************************************************************************/
35 #define MAXVAR        50                    /* Number of variables to create */
36 #define MAXSET      2000                       /* Number of variables to set */
37 #define MAXOBJ      1000                      /* Number of objects to create */
38 #define MAXLOOK    10000                      /* Number of objects to lookup */
39 #define MAXTH          4                       /* Number of threads to spawn */
40
41 /*****************************************************************************
42  * Local prototypes.
43  *****************************************************************************/
44 static int    Stress    ( vlc_object_t *, char *, char * );
45 static void * Dummy     ( vlc_object_t * );
46
47 static int    Signal    ( vlc_object_t *, char *, char * );
48
49 /*****************************************************************************
50  * Module descriptor.
51  *****************************************************************************/
52 vlc_module_begin();
53     set_description( _("Miscellaneous stress tests") );
54     var_Create( p_module->p_libvlc, "stress", VLC_VAR_COMMAND );
55     var_Set( p_module->p_libvlc, "stress", (vlc_value_t)(void*)Stress );
56     var_Create( p_module->p_libvlc, "signal", VLC_VAR_COMMAND );
57     var_Set( p_module->p_libvlc, "signal", (vlc_value_t)(void*)Signal );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * Stress: perform various tests
62  *****************************************************************************/
63 static int Stress( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
64 {
65     vlc_object_t **pp_objects;
66     mtime_t start;
67     char ** ppsz_name;
68     char *  psz_blob;
69     int     i, i_level;
70
71     if( *psz_arg )
72     {
73         i_level = atoi( psz_arg );
74         if( i_level <= 0 )
75         {
76             i_level = 1;
77         }
78         else if( i_level > 200 )
79         {
80             /* It becomes quite dangerous above 150 */
81             i_level = 200;
82         }
83     }
84     else
85     {
86         i_level = 10;
87     }
88
89     /* Allocate required data */
90     ppsz_name = malloc( MAXVAR * i_level * sizeof(char*) );
91     psz_blob = malloc( 20 * MAXVAR * i_level * sizeof(char) );
92     for( i = 0; i < MAXVAR * i_level; i++ )
93     {
94         ppsz_name[i] = psz_blob + 20 * i;
95     }
96
97     pp_objects = malloc( MAXOBJ * i_level * sizeof(void*) );
98
99     /*
100      *  Test #1: objects
101      */
102     printf( "Test #1: objects\n" );
103
104     printf( " - creating %i objects\n", MAXOBJ * i_level );
105     start = mdate();
106     for( i = 0; i < MAXOBJ * i_level; i++ )
107     {
108         pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
109     }
110
111     printf( " - randomly looking up %i objects\n", MAXLOOK * i_level );
112     for( i = MAXLOOK * i_level; i--; )
113     {
114         int id = (int) (MAXOBJ * i_level * 1.0 * rand() / (RAND_MAX));
115         vlc_object_get( p_this, pp_objects[id]->i_object_id );
116     }
117
118     printf( " - destroying the objects (LIFO)\n" );
119     for( i = MAXOBJ * i_level; i--; )
120     {
121         vlc_object_destroy( pp_objects[i] );
122     }
123
124     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
125
126     /*
127      *  Test #2: integer variables
128      */
129     printf( "Test #2: integer variables\n" );
130
131     printf( " - creating %i integer variables\n", MAXVAR * i_level );
132     start = mdate();
133     for( i = 0; i < MAXVAR * i_level; i++ )
134     {
135         sprintf( ppsz_name[i], "foo-%04i-bar-%04x", i, i * 11 );
136         var_Create( p_this, ppsz_name[i], VLC_VAR_INTEGER );
137     }
138
139     printf( " - randomly assigning %i values\n", MAXSET * i_level );
140     for( i = 0; i < MAXSET * i_level; i++ )
141     {
142         int v = (int) (MAXVAR * i_level * 1.0 * rand() / (RAND_MAX));
143         var_Set( p_this, ppsz_name[v], (vlc_value_t)i );
144     }
145
146     printf( " - destroying the variables\n" );
147     for( i = 0; i < MAXVAR * i_level; i++ )
148     {
149         var_Destroy( p_this, ppsz_name[i] );
150     }
151
152     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
153
154     /*
155      *  Test #3: string variables
156      */
157     printf( "Test #3: string variables\n" );
158
159     printf( " - creating %i string variables\n", MAXVAR * i_level );
160     start = mdate();
161     for( i = 0; i < MAXVAR * i_level; i++ )
162     {
163         sprintf( ppsz_name[i], "foo-%04i-bar-%04x", i, i * 11 );
164         var_Create( p_this, ppsz_name[i], VLC_VAR_STRING );
165     }
166
167     printf( " - randomly assigning %i values\n", MAXSET * i_level );
168     for( i = 0; i < MAXSET * i_level; i++ )
169     {
170         int v = (int) (MAXVAR * i_level * 1.0 * rand() / (RAND_MAX));
171         var_Set( p_this, ppsz_name[v], (vlc_value_t)ppsz_name[v] );
172     }
173
174     printf( " - destroying the variables\n" );
175     for( i = 0; i < MAXVAR * i_level; i++ )
176     {
177         var_Destroy( p_this, ppsz_name[i] );
178     }
179
180     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
181
182     /*
183      *  Test #4: threads
184      */
185     printf( "Test #4: threads\n" );
186     start = mdate();
187
188     printf( " - spawning %i threads that will each create %i objects\n",
189             MAXTH * i_level, MAXOBJ/MAXTH );
190     for( i = 0; i < MAXTH * i_level; i++ )
191     {
192         pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
193         vlc_thread_create( pp_objects[i], "foo", Dummy, 0, VLC_TRUE );
194     }
195
196     printf( " - killing the threads (LIFO)\n" );
197     for( i = MAXTH * i_level; i--; )
198     {
199         pp_objects[i]->b_die = VLC_TRUE;
200         vlc_thread_join( pp_objects[i] );
201         vlc_object_destroy( pp_objects[i] );
202     }
203
204     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
205
206     /* Free required data */
207     free( pp_objects );
208     free( psz_blob );
209     free( ppsz_name );
210
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  * Dummy: used by test, creates objects and then do nothing.
216  *****************************************************************************/
217 static void * Dummy( vlc_object_t *p_this )
218 {
219     int i;
220     vlc_object_t *pp_objects[MAXOBJ/MAXTH];
221
222     for( i = 0; i < MAXOBJ/MAXTH; i++ )
223     {
224         pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
225     }
226
227     vlc_thread_ready( p_this );
228
229     while( !p_this->b_die )
230     {
231         msleep( 1000 );
232     }
233
234     for( i = MAXOBJ/MAXTH; i--; )
235     {
236         vlc_object_destroy( pp_objects[i] );
237     }
238
239     return NULL;
240 }
241
242 /*****************************************************************************
243  * Signal: send a signal to the current thread.
244  *****************************************************************************/
245 static int Signal( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
246 {
247     raise( atoi(psz_arg) );
248     return VLC_SUCCESS;
249 }
250