tests: zlogger nominally runs the daemon
[platform/core/system/dlog.git] / tests / stdout_benchmark.c
1 /* DLOG
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <stdbool.h>
21
22 #define LOG_TAG "DLOG_BENCHMARK"
23 #include <dlog.h>
24
25 int main (int argc, char **argv)
26 {
27         if (argc != 4 && argc != 5) {
28                 fprintf(stderr, "argc = %d\n", argc);
29                 return EXIT_FAILURE;
30         }
31
32         bool buffered = atoi(argv[1]) != 0;
33         setvbuf(stdout, NULL, buffered ? _IOLBF : _IONBF, 0);
34
35         int const N = atoi(argv[2]);
36         char const *MSG = argv[3];
37         char const *where = argc == 4 ? "" : argv[4];
38
39         struct timespec start, end;
40         inline void print_diff (char const *name) {
41                 double elapsed_s = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1E9;
42                 fprintf(stderr, "%s: %lfus per log (%lfs total per %d logs)\n"
43                         , name
44                         , 1000000 * elapsed_s / N
45                         , elapsed_s
46                         , N
47                 );
48         }
49
50         if (strcmp(where, "stdout") == 0 || strcmp(where, "") == 0) {
51                 for (int i = 0; i < 10000; ++i)
52                         printf("stdout warm-up\n");
53
54                 clock_gettime(CLOCK_MONOTONIC, &start);
55                 for (int i = 0; i < N; ++i)
56                         printf("%s\n", MSG);
57                 clock_gettime(CLOCK_MONOTONIC, &end);
58                 print_diff(buffered ? "stdout (buffered)" : "stdout (not buffered)");
59         }
60
61         if (strcmp(where, "libdlog") == 0 || strcmp(where, "") == 0) {
62                 for (int i = 0; i < 10000; ++i)
63                         LOGE("libdlog warm-up\n");
64
65                 clock_gettime(CLOCK_MONOTONIC, &start);
66                 for (int i = 0; i < N; ++i)
67                         LOGE("%s\n", MSG);
68                 clock_gettime(CLOCK_MONOTONIC, &end);
69                 print_diff("libdlog");
70         }
71
72         return EXIT_SUCCESS;
73 }