sensord: move .service/.socket to packaging
[platform/core/system/sensord.git] / test / src / multi-process-performance-test.c
1 /*
2  * sensord
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <sensor_common.h>
24 #include <sensor_types.h>
25
26 #include "check-sensor.h"
27
28 void usage()
29 {
30         printf("Usage : ./performance-test <TIMEOUT> <interval>(optional)\n\n");
31
32         printf("TIMEOUT:\n");
33         printf("time for which the parallel sensor test cases should run\n");
34
35         printf("interval:\n");
36         printf("The time interval should be entered based on the sampling frequency supported by accelerometer driver on the device in ms.\n");
37         printf("If no value for sensor is entered default value by the driver will be used.\n");
38 }
39
40 int main(int argc, char** argv)
41 {
42         pid_t b = 1;
43
44         int i = 0;
45         int interval = DEFAULT_EVENT_INTERVAL;
46         int TIMEOUT = 10; //in seconds for which all the sensor tests should run
47
48         if(argc < 2) {
49                 usage();
50                 return -1;
51         }
52         else if(argc == 2){
53                 TIMEOUT = atoi(argv[1]);
54                 if (TIMEOUT == 0) {
55                         usage();
56                         return -1;
57                 }
58         }
59         else {
60                 TIMEOUT = atoi(argv[1]);
61                 interval = atoi(argv[2]);
62                 if (TIMEOUT == 0 || interval == 0) {
63                         usage();
64                         return -1;
65                 }
66         }
67
68         //make an array of size MAX and fill it with all the sensors needed to run
69         int MAX = 6;
70         pid_t pids[MAX];
71         sensor_type_t sensor[MAX];
72
73         //Update the value of MAX and add more sensors here to test more sensors in parallel
74         sensor[0] = ACCELEROMETER_SENSOR;
75         sensor[1] = GYROSCOPE_SENSOR;
76         sensor[2] = GEOMAGNETIC_SENSOR;
77         sensor[3] = PRESSURE_SENSOR;
78         sensor[4] = PROXIMITY_SENSOR;
79         sensor[MAX-1] = LIGHT_SENSOR;
80
81         while (i < MAX) {
82                 if (b > 0) {
83                         b = fork();
84                         if (b == -1) perror("Fork failed\n");
85                         else if (b == 0) {
86                                 break;
87                         }
88                         pids[i] = b;
89                         i++;
90                 }
91         }
92
93         if (i < MAX) {
94                 // call the sensord test tc-common for a sensor.
95                 int event = (sensor[i] << 16) | 0x0001;
96                 struct pthread_arguments arg;
97                 arg.sensor_type = sensor[i];
98                 arg.event = event;
99                 arg.interval = interval;
100
101                 check_sensor((void*)&arg);
102         }
103         else {
104                 // Main Parent Child. Waits for TIMEOUT and then kills all child processes.
105                 sleep (TIMEOUT);
106                 int j = 0;
107
108                 for (j = 0; j < MAX; j++) {
109                         char command[100];
110                         snprintf(command, sizeof(command), "kill %d", pids[j]);
111                         if (system(command) == -1)
112                                 return -1;
113                 }
114         }
115
116         return 0;
117 }