automotive-dlt
dlt-test-stress.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
28 /*******************************************************************************
29 ** **
30 ** SRC-MODULE: dlt-test-stress.c **
31 ** **
32 ** TARGET : linux **
33 ** **
34 ** PROJECT : DLT **
35 ** **
36 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
37 ** Markus Klein **
38 ** **
39 ** PURPOSE : **
40 ** **
41 ** REMARKS : **
42 ** **
43 ** PLATFORM DEPENDANT [yes/no]: yes **
44 ** **
45 ** TO BE CHANGED BY USER [yes/no]: no **
46 ** **
47 *******************************************************************************/
48 
49 /*******************************************************************************
50 ** Author Identity **
51 ********************************************************************************
52 ** **
53 ** Initials Name Company **
54 ** -------- ------------------------- ---------------------------------- **
55 ** aw Alexander Wenzel BMW **
56 ** mk Markus Klein Fraunhofer ESK **
57 *******************************************************************************/
58 
59 /*******************************************************************************
60 ** Revision Control History **
61 *******************************************************************************/
62 
63 /*
64  * $LastChangedRevision: 1670 $
65  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
66  * $LastChangedBy$
67  Initials Date Comment
68  aw 13.01.2010 initial
69  */
70 #include <netdb.h>
71 #include <ctype.h> /* for isprint() */
72 #include <errno.h>
73 #include <stdio.h> /* for printf() and fprintf() */
74 #include <stdlib.h> /* for atoi() and exit() */
75 #include <string.h> /* for memset() */
76 #include <unistd.h> /* for close() */
77 #include <pthread.h> /* POSIX Threads */
78 
79 #include "dlt.h"
80 #include "dlt_common.h" /* for dlt_get_version() */
81 
83 
84 typedef struct
85 {
86  int num;
88 
89 #define STRESS1_NUM_CONTEXTS 3000
90 #define STRESS2_MAX_NUM_THREADS 64
91 #define STRESS3_MAX_NUM_MESSAGES 512
92 
93 #define MAX_TESTS 3
94 
95 void stress1(void);
96 
97 void stress2(void);
98 void thread_function(void);
99 
100 void stress3(void);
101 
105 void usage()
106 {
107  char version[255];
108 
109  dlt_get_version(version,255);
110 
111  printf("Usage: dlt-test-stress [options]\n");
112  printf("Test application executing several stress tests.\n");
113  printf("%s \n", version);
114  printf("Options:\n");
115  printf(" -v Verbose mode\n");
116  printf(" -f filename Use local log file instead of sending to daemon\n");
117  printf(" -1 Execute test 1 (register/unregister many contexts)\n");
118  printf(" -2 Execute test 2 (multiple threads logging data)\n");
119  printf(" -3 Execute test 3 (logging much data)\n");
120 }
121 
125 int main(int argc, char* argv[])
126 {
127  //int vflag = 0;
128  char *fvalue = 0;
129  int test[MAX_TESTS];
130 
131  int i,c,help;
132 
133  for (i=0;i<MAX_TESTS;i++)
134  {
135  test[i]=0;
136  }
137 
138  opterr = 0;
139 
140  while ((c = getopt (argc, argv, "vf:123")) != -1)
141  {
142  switch (c)
143  {
144  case 'v':
145  {
146  //vflag = 1;
147  break;
148  }
149  case 'f':
150  {
151  fvalue = optarg;
152  break;
153  }
154  case '1':
155  {
156  test[0] = 1;
157  break;
158  }
159  case '2':
160  {
161  test[1] = 1;
162  break;
163  }
164  case '3':
165  {
166  test[2] = 1;
167  break;
168  }
169  case '?':
170  {
171  if (optopt == 'f')
172  {
173  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
174  }
175  else if (isprint (optopt))
176  {
177  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
178  }
179  else
180  {
181  fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
182  }
183  /* unknown or wrong option used, show usage information and terminate */
184  usage();
185  return -1;
186  }
187  default:
188  {
189  abort ();
190  return -1;//for parasoft
191  }
192  }
193  }
194 
195  if (fvalue)
196  {
197  /* DLT is intialised automatically, except another output target will be used */
198  if (dlt_init_file(fvalue)<0) /* log to file */
199  {
200  return -1;
201  }
202  }
203 
204  help=0;
205  for (i=0;i<MAX_TESTS;i++)
206  {
207  if (test[i]==1)
208  {
209  help=1;
210  break;
211  }
212  }
213 
214  if (help==0)
215  {
216  usage();
217  return -1;
218  }
219 
220  DLT_REGISTER_APP("DSTS","DLT daemon stress tests");
221 
222  if (test[0])
223  {
224  stress1();
225  }
226  if (test[1])
227  {
228  stress2();
229  }
230  if (test[2])
231  {
232  stress3();
233  }
234 
236 
237  sleep(1);
238 
239  return 0;
240 }
241 
242 void stress1(void)
243 {
244  int i,c;
245  char ctid[5];
246 
247  printf("Starting stress test1... (press \"Enter\" to terminate test) \n");
248 
249  printf("* Register %d contexts...\n",STRESS1_NUM_CONTEXTS);
250 
251  for (i=0; i<STRESS1_NUM_CONTEXTS; i++)
252  {
253  /* Generate id */
254  memset(ctid,0,5);
255  snprintf(ctid,5,"%d",i);
256 
257  //printf("%i: '%s' \n",i,ctid);
258 
259  dlt_register_context(&(mycontext[i]),ctid,ctid);
260  usleep(500);
261  }
262 
263  while (1)
264  {
265  c=getchar();
266  /* if "Return" is pressed, exit loop; */
267  if (c==10)
268  {
269  break;
270  }
271  }
272 
273  printf("* Unregister %d contexts...\n",STRESS1_NUM_CONTEXTS);
274 
275  for (i=0; i<STRESS1_NUM_CONTEXTS; i++)
276  {
277  DLT_UNREGISTER_CONTEXT(mycontext[i]);
278  usleep(500);
279  }
280 
281  printf("Finished stress test1 \n\n");
282 }
283 
284 void stress2(void)
285 {
286  int ret,index;
287 
288  pthread_t thread[STRESS2_MAX_NUM_THREADS];
290 
291  printf("Starting stress test2... \n");
292 
293  srand(time(NULL));
294 
295  printf("* Creating %d Threads, each of them registers one context,\n",STRESS2_MAX_NUM_THREADS);
296  printf(" sending one log message, then unregisters the context\n");
297 
298  for (index=0;index<STRESS2_MAX_NUM_THREADS;index++)
299  {
300  thread_data[index].num = index;
301  ret=pthread_create(&(thread[index]), NULL, (void *) &thread_function, (void *) &(thread_data[index]));
302  if (ret!=0)
303  {
304  printf("Error creating thread %d: %s \n", index, strerror(errno));
305  }
306 
307  usleep(1000);
308  }
309 
310  for (index=0;index<STRESS2_MAX_NUM_THREADS;index++)
311  {
312  pthread_join(thread[index], NULL);
313  }
314 
315  printf("Finished stress test2 \n\n");
316 }
317 
318 void thread_function(void)
319 {
320  //thread_data_t *data;
321  DLT_DECLARE_CONTEXT(context_thread1);
322  char ctid[5];
323 
324  //data = (thread_data_t *) ptr;
325 
326  memset(ctid,0,5);
327 
328  /* Create random context id */
329  snprintf(ctid,5,"%.2x", rand() & 0x0000ffff);
330 
331  usleep(rand()/1000);
332 
333  DLT_REGISTER_CONTEXT(context_thread1,ctid,ctid);
334 
335  DLT_LOG(context_thread1,DLT_LOG_INFO,DLT_STRING(ctid));
336 
337  DLT_UNREGISTER_CONTEXT(context_thread1);
338 }
339 
340 void stress3(void)
341 {
342  DLT_DECLARE_CONTEXT(context_stress3);
344  int num;
345 
346  /* Performance test */
347  DLT_REGISTER_CONTEXT(context_stress3,"TST3","Stress Test 3 - Performance");
348 
349  printf("Starting stress test3... \n");
350  printf("* Logging raw data, up to a size of %d\n",STRESS3_MAX_NUM_MESSAGES);
351 
352  for (num=0;num<STRESS3_MAX_NUM_MESSAGES;num++)
353  {
354  buffer[num] = num;
355  DLT_LOG(context_stress3,DLT_LOG_INFO,DLT_INT(num),DLT_RAW(buffer,num));
356  usleep(10000);
357  }
358 
359  printf("Finished stress test3 \n\n");
360 }
#define DLT_RAW(BUF, LEN)
#define DLT_UNREGISTER_APP()
void thread_function(void)
int main(int argc, char *argv[])
#define DLT_INT(INT_VAR)
DltReturnValue dlt_register_context(DltContext *handle, const char *contextid, const char *description)
Definition: dlt_user.c:883
#define STRESS3_MAX_NUM_MESSAGES
#define MAX_TESTS
DltReturnValue dlt_init_file(const char *name)
Definition: dlt_user.c:350
void usage()
#define DLT_DECLARE_CONTEXT(CONTEXT)
#define DLT_REGISTER_APP(APPID, DESCRIPTION)
#define DLT_STRING(TEXT)
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define DLT_UNREGISTER_CONTEXT(CONTEXT)
void stress1(void)
void stress3(void)
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
void stress2(void)
DltContext mycontext[9999]
#define STRESS2_MAX_NUM_THREADS
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
#define STRESS1_NUM_CONTEXTS
#define NULL
Definition: dlt_common.h:232