]> git.sesse.net Git - vlc/blob - modules/misc/testsuite/test4.c
Include vlc_plugin.h as needed
[vlc] / modules / misc / testsuite / test4.c
1 /*****************************************************************************
2  * test4.c : Miscellaneous stress tests module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include <vlc_plugin.h>
33
34 #include <signal.h>
35
36 /*****************************************************************************
37  * Defines
38  *****************************************************************************/
39 #define MAXVAR        50                    /* Number of variables to create */
40 #define MAXSET      2000                       /* Number of variables to set */
41 #define MAXOBJ      1000                      /* Number of objects to create */
42 #define MAXLOOK    10000                      /* Number of objects to lookup */
43 #define MAXTH          4                       /* Number of threads to spawn */
44
45 /*****************************************************************************
46  * Local prototypes.
47  *****************************************************************************/
48 static int    Foo       ( vlc_object_t *, char const *,
49                           vlc_value_t, vlc_value_t, void * );
50 static int    Callback  ( vlc_object_t *, char const *,
51                           vlc_value_t, vlc_value_t, void * );
52 static int    MyCallback( vlc_object_t *, char const *,
53                           vlc_value_t, vlc_value_t, void * );
54 static void * MyThread  ( vlc_object_t * );
55
56 static int    Stress    ( vlc_object_t *, char const *,
57                           vlc_value_t, vlc_value_t, void * );
58 static void * Dummy     ( vlc_object_t * );
59
60 static int    Signal    ( vlc_object_t *, char const *,
61                           vlc_value_t, vlc_value_t, void * );
62
63 /*****************************************************************************
64  * Module descriptor.
65  *****************************************************************************/
66 vlc_module_begin();
67     set_description( _("Miscellaneous stress tests") );
68     var_Create( p_module->p_libvlc, "foo-test",
69                 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
70     var_AddCallback( p_module->p_libvlc, "foo-test", Foo, NULL );
71     var_Create( p_module->p_libvlc, "callback-test",
72                 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
73     var_AddCallback( p_module->p_libvlc, "callback-test", Callback, NULL );
74     var_Create( p_module->p_libvlc, "stress-test",
75                 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
76     var_AddCallback( p_module->p_libvlc, "stress-test", Stress, NULL );
77     var_Create( p_module->p_libvlc, "signal",
78                 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
79     var_AddCallback( p_module->p_libvlc, "signal", Signal, NULL );
80 vlc_module_end();
81
82 /*****************************************************************************
83  * Foo: put anything here
84  *****************************************************************************/
85 static int Foo( vlc_object_t *p_this, char const *psz_cmd,
86                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
87 {
88     vlc_value_t val;
89     int i;
90
91     var_Create( p_this, "honk", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
92
93     val.psz_string = "foo";
94     var_Change( p_this, "honk", VLC_VAR_ADDCHOICE, &val, NULL );
95     val.psz_string = "bar";
96     var_Change( p_this, "honk", VLC_VAR_ADDCHOICE, &val, NULL );
97     val.psz_string = "baz";
98     var_Change( p_this, "honk", VLC_VAR_ADDCHOICE, &val, NULL );
99     var_Change( p_this, "honk", VLC_VAR_SETDEFAULT, &val, NULL );
100
101     var_Get( p_this, "honk", &val ); printf( "value: %s\n", val.psz_string );
102
103     val.psz_string = "foo";
104     var_Set( p_this, "honk", val );
105
106     var_Get( p_this, "honk", &val ); printf( "value: %s\n", val.psz_string );
107
108     val.psz_string = "blork";
109     var_Set( p_this, "honk", val );
110
111     var_Get( p_this, "honk", &val ); printf( "value: %s\n", val.psz_string );
112
113     val.psz_string = "baz";
114     var_Change( p_this, "honk", VLC_VAR_DELCHOICE, &val, NULL );
115
116     var_Get( p_this, "honk", &val ); printf( "value: %s\n", val.psz_string );
117
118     var_Change( p_this, "honk", VLC_VAR_GETLIST, &val, NULL );
119     for( i = 0 ; i < val.p_list->i_count ; i++ )
120     {
121         printf( "value %i: %s\n", i, val.p_list->p_values[i].psz_string );
122     }
123     var_Change( p_this, "honk", VLC_VAR_FREELIST, &val, NULL );
124
125     var_Destroy( p_this, "honk" );
126
127     return VLC_SUCCESS;
128 }
129
130 /*****************************************************************************
131  * Callback: test callback functions
132  *****************************************************************************/
133 static int Callback( vlc_object_t *p_this, char const *psz_cmd,
134                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
135 {
136     int i;
137     char psz_var[20];
138     vlc_object_t *pp_objects[10];
139     vlc_value_t val;
140
141     /* Allocate a few variables */
142     for( i = 0; i < 1000; i++ )
143     {
144         sprintf( psz_var, "blork-%i", i );
145         var_Create( p_this, psz_var, VLC_VAR_INTEGER );
146         var_AddCallback( p_this, psz_var, MyCallback, NULL );
147     }
148
149     /*
150      *  Test #1: callback loop detection
151      */
152     printf( "Test #1: callback loop detection\n" );
153
154     printf( " - without boundary check (vlc should print an error)\n" );
155     val.i_int = 1234567;
156     var_Set( p_this, "blork-12", val );
157
158     printf( " - with boundary check\n" );
159     val.i_int = 12;
160     var_Set( p_this, "blork-12", val );
161
162     /*
163      *  Test #2: concurrent access
164      */
165     printf( "Test #2: concurrent access\n" );
166
167     printf( " - launch worker threads\n" );
168
169     for( i = 0; i < 10; i++ )
170     {
171         pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
172         vlc_object_attach( pp_objects[i], p_this );
173         vlc_thread_create( pp_objects[i], "foo", MyThread, 0, true );
174     }
175
176     msleep( 3000000 );
177
178     printf( " - kill worker threads\n" );
179
180     for( i = 0; i < 10; i++ )
181     {
182         vlc_object_kill( pp_objects[i] );
183         vlc_thread_join( pp_objects[i] );
184         vlc_object_detach( pp_objects[i] );
185         vlc_object_release( pp_objects[i] );
186     }
187
188     /* Clean our mess */
189     for( i = 0; i < 1000; i++ )
190     {
191         sprintf( psz_var, "blork-%i", i );
192         var_DelCallback( p_this, psz_var, MyCallback, NULL );
193         var_Destroy( p_this, psz_var );
194     }
195
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * MyCallback: used by callback-test
201  *****************************************************************************/
202 static int MyCallback( vlc_object_t *p_this, char const *psz_var,
203                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
204 {
205     int i_var = 1 + atoi( psz_var + strlen("blork-") );
206     char psz_newvar[20];
207
208     if( i_var == 1000 )
209     {
210         i_var = 0;
211     }
212
213     /* If we are requested to stop, then stop. */
214     if( i_var == newval.i_int )
215     {
216         return VLC_SUCCESS;
217     }
218
219     /* If we're the blork-3 callback, set blork-4, and so on. */
220     sprintf( psz_newvar, "blork-%i", i_var );
221     var_Set( p_this, psz_newvar, newval );
222
223     return VLC_SUCCESS;
224 }
225
226 /*****************************************************************************
227  * MyThread: used by callback-test, creates objects and then do nothing.
228  *****************************************************************************/
229 static void * MyThread( vlc_object_t *p_this )
230 {
231     char psz_var[20];
232     vlc_value_t val;
233     vlc_object_t *p_parent = p_this->p_parent;
234
235     vlc_thread_ready( p_this );
236
237     val.i_int = 42;
238
239     while( !p_this->b_die )
240     {
241         int i = (int) (100.0 * rand() / (RAND_MAX));
242
243         sprintf( psz_var, "blork-%i", i );
244         val.i_int = i + 200;
245         var_Set( p_parent, psz_var, val );
246
247         /* This is quite heavy, but we only have 10 threads. Keep cool. */
248         msleep( 1000 );
249     }
250
251     return NULL;
252 }
253
254 /*****************************************************************************
255  * Stress: perform various stress tests
256  *****************************************************************************/
257 static int Stress( vlc_object_t *p_this, char const *psz_cmd,
258                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
259 {
260     vlc_object_t **pp_objects;
261     mtime_t start;
262     char ** ppsz_name;
263     char *  psz_blob;
264     int     i, i_level;
265
266     if( *newval.psz_string )
267     {
268         i_level = atoi( newval.psz_string );
269         if( i_level <= 0 )
270         {
271             i_level = 1;
272         }
273         else if( i_level > 200 )
274         {
275             /* It becomes quite dangerous above 150 */
276             i_level = 200;
277         }
278     }
279     else
280     {
281         i_level = 10;
282     }
283
284     /* Allocate required data */
285     ppsz_name = malloc( MAXVAR * i_level * sizeof(char*) );
286     psz_blob = malloc( 20 * MAXVAR * i_level * sizeof(char) );
287     for( i = 0; i < MAXVAR * i_level; i++ )
288     {
289         ppsz_name[i] = psz_blob + 20 * i;
290     }
291
292     pp_objects = malloc( MAXOBJ * i_level * sizeof(void*) );
293
294     /*
295      *  Test #1: objects
296      */
297     printf( "Test #1: objects\n" );
298
299     printf( " - creating %i objects\n", MAXOBJ * i_level );
300     start = mdate();
301     for( i = 0; i < MAXOBJ * i_level; i++ )
302     {
303         pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
304     }
305
306     printf( " - randomly looking up %i objects\n", MAXLOOK * i_level );
307     for( i = MAXLOOK * i_level; i--; )
308     {
309         int id = (int) (MAXOBJ * i_level * 1.0 * rand() / (RAND_MAX));
310         vlc_object_get( pp_objects[id]->i_object_id );
311         vlc_object_release( p_this );
312     }
313
314     printf( " - destroying the objects (LIFO)\n" );
315     for( i = MAXOBJ * i_level; i--; )
316     {
317         vlc_object_release( pp_objects[i] );
318     }
319
320     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
321
322     /*
323      *  Test #2: integer variables
324      */
325     printf( "Test #2: integer variables\n" );
326
327     printf( " - creating %i integer variables\n", MAXVAR * i_level );
328     start = mdate();
329     for( i = 0; i < MAXVAR * i_level; i++ )
330     {
331         sprintf( ppsz_name[i], "foo-%04i-bar-%04x", i, i * 11 );
332         var_Create( p_this, ppsz_name[i], VLC_VAR_INTEGER );
333     }
334
335     printf( " - randomly assigning %i values\n", MAXSET * i_level );
336     for( i = 0; i < MAXSET * i_level; i++ )
337     {
338         int v = (int) (MAXVAR * i_level * 1.0 * rand() / (RAND_MAX));
339         var_Set( p_this, ppsz_name[v], (vlc_value_t)i );
340     }
341
342     printf( " - destroying the variables\n" );
343     for( i = 0; i < MAXVAR * i_level; i++ )
344     {
345         var_Destroy( p_this, ppsz_name[i] );
346     }
347
348     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
349
350     /*
351      *  Test #3: string variables
352      */
353     printf( "Test #3: string variables\n" );
354
355     printf( " - creating %i string variables\n", MAXVAR * i_level );
356     start = mdate();
357     for( i = 0; i < MAXVAR * i_level; i++ )
358     {
359         sprintf( ppsz_name[i], "foo-%04i-bar-%04x", i, i * 11 );
360         var_Create( p_this, ppsz_name[i], VLC_VAR_STRING );
361     }
362
363     printf( " - randomly assigning %i values\n", MAXSET * i_level );
364     for( i = 0; i < MAXSET * i_level; i++ )
365     {
366         int v = (int) (MAXVAR * i_level * 1.0 * rand() / (RAND_MAX));
367         var_Set( p_this, ppsz_name[v], (vlc_value_t)ppsz_name[v] );
368     }
369
370     printf( " - destroying the variables\n" );
371     for( i = 0; i < MAXVAR * i_level; i++ )
372     {
373         var_Destroy( p_this, ppsz_name[i] );
374     }
375
376     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
377
378     /*
379      *  Test #4: threads
380      */
381     printf( "Test #4: threads\n" );
382     start = mdate();
383
384     printf( " - spawning %i threads that will each create %i objects\n",
385             MAXTH * i_level, MAXOBJ/MAXTH );
386     for( i = 0; i < MAXTH * i_level; i++ )
387     {
388         pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
389         vlc_thread_create( pp_objects[i], "foo", Dummy, 0, true );
390     }
391
392     printf( " - killing the threads (LIFO)\n" );
393     for( i = MAXTH * i_level; i--; )
394     {
395         pp_objects[i]->b_die = true;
396         vlc_thread_join( pp_objects[i] );
397         vlc_object_release( pp_objects[i] );
398     }
399
400     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
401
402     /* Free required data */
403     free( pp_objects );
404     free( psz_blob );
405     free( ppsz_name );
406
407     return VLC_SUCCESS;
408 }
409
410 /*****************************************************************************
411  * Dummy: used by stress-test, creates objects and then do nothing.
412  *****************************************************************************/
413 static void * Dummy( vlc_object_t *p_this )
414 {
415     int i;
416     vlc_object_t *pp_objects[MAXOBJ/MAXTH];
417
418     for( i = 0; i < MAXOBJ/MAXTH; i++ )
419     {
420         pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
421     }
422
423     vlc_thread_ready( p_this );
424
425     while( !p_this->b_die )
426     {
427         msleep( 10000 );
428     }
429
430     for( i = MAXOBJ/MAXTH; i--; )
431     {
432         vlc_object_release( pp_objects[i] );
433     }
434
435     return NULL;
436 }
437
438 /*****************************************************************************
439  * Signal: send a signal to the current thread.
440  *****************************************************************************/
441 static int Signal( vlc_object_t *p_this, char const *psz_cmd,
442                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
443 {
444     raise( atoi(newval.psz_string) );
445     return VLC_SUCCESS;
446 }