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 $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 /*****************************************************************************
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 */
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Stress ( vlc_object_t *, char *, char * );
45 static void * Dummy ( vlc_object_t * );
47 static int Signal ( vlc_object_t *, char *, char * );
49 /*****************************************************************************
51 *****************************************************************************/
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 );
60 /*****************************************************************************
61 * Stress: perform various tests
62 *****************************************************************************/
63 static int Stress( vlc_object_t *p_this, char *psz_cmd, char *psz_arg )
65 vlc_object_t **pp_objects;
73 i_level = atoi( psz_arg );
78 else if( i_level > 200 )
80 /* It becomes quite dangerous above 150 */
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++ )
94 ppsz_name[i] = psz_blob + 20 * i;
97 pp_objects = malloc( MAXOBJ * i_level * sizeof(void*) );
102 printf( "Test #1: objects\n" );
104 printf( " - creating %i objects\n", MAXOBJ * i_level );
106 for( i = 0; i < MAXOBJ * i_level; i++ )
108 pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
111 printf( " - randomly looking up %i objects\n", MAXLOOK * i_level );
112 for( i = MAXLOOK * i_level; i--; )
114 int id = (int) (MAXOBJ * i_level * 1.0 * rand() / (RAND_MAX));
115 vlc_object_get( p_this, pp_objects[id]->i_object_id );
118 printf( " - destroying the objects (LIFO)\n" );
119 for( i = MAXOBJ * i_level; i--; )
121 vlc_object_destroy( pp_objects[i] );
124 printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
127 * Test #2: integer variables
129 printf( "Test #2: integer variables\n" );
131 printf( " - creating %i integer variables\n", MAXVAR * i_level );
133 for( i = 0; i < MAXVAR * i_level; i++ )
135 sprintf( ppsz_name[i], "foo-%04i-bar-%04x", i, i * 11 );
136 var_Create( p_this, ppsz_name[i], VLC_VAR_INTEGER );
139 printf( " - randomly assigning %i values\n", MAXSET * i_level );
140 for( i = 0; i < MAXSET * i_level; i++ )
142 int v = (int) (MAXVAR * i_level * 1.0 * rand() / (RAND_MAX));
143 var_Set( p_this, ppsz_name[v], (vlc_value_t)i );
146 printf( " - destroying the variables\n" );
147 for( i = 0; i < MAXVAR * i_level; i++ )
149 var_Destroy( p_this, ppsz_name[i] );
152 printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
155 * Test #3: string variables
157 printf( "Test #3: string variables\n" );
159 printf( " - creating %i string variables\n", MAXVAR * i_level );
161 for( i = 0; i < MAXVAR * i_level; i++ )
163 sprintf( ppsz_name[i], "foo-%04i-bar-%04x", i, i * 11 );
164 var_Create( p_this, ppsz_name[i], VLC_VAR_STRING );
167 printf( " - randomly assigning %i values\n", MAXSET * i_level );
168 for( i = 0; i < MAXSET * i_level; i++ )
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] );
174 printf( " - destroying the variables\n" );
175 for( i = 0; i < MAXVAR * i_level; i++ )
177 var_Destroy( p_this, ppsz_name[i] );
180 printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
185 printf( "Test #4: threads\n" );
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++ )
192 pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
193 vlc_thread_create( pp_objects[i], "foo", Dummy, 0, VLC_TRUE );
196 printf( " - killing the threads (LIFO)\n" );
197 for( i = MAXTH * i_level; i--; )
199 pp_objects[i]->b_die = VLC_TRUE;
200 vlc_thread_join( pp_objects[i] );
201 vlc_object_destroy( pp_objects[i] );
204 printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
206 /* Free required data */
214 /*****************************************************************************
215 * Dummy: used by test, creates objects and then do nothing.
216 *****************************************************************************/
217 static void * Dummy( vlc_object_t *p_this )
220 vlc_object_t *pp_objects[MAXOBJ/MAXTH];
222 for( i = 0; i < MAXOBJ/MAXTH; i++ )
224 pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
227 vlc_thread_ready( p_this );
229 while( !p_this->b_die )
234 for( i = MAXOBJ/MAXTH; i--; )
236 vlc_object_destroy( pp_objects[i] );
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 )
247 raise( atoi(psz_arg) );