Implement stdout benchmark 07/258007/6
authorMateusz Majewski <m.majewski2@samsung.com>
Fri, 7 May 2021 10:41:11 +0000 (12:41 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Tue, 25 May 2021 13:41:13 +0000 (15:41 +0200)
Change-Id: Ia0fdf1abfbe24db49f65c4ce8b24d1e7fd65279c
Co-authored-by: MichaƂ Bloch <m.bloch@samsung.com>
Makefile.am
packaging/dlog.spec
tests/stdout_benchmark.c [new file with mode: 0644]

index fb2f616..165da22 100644 (file)
@@ -313,6 +313,26 @@ perf_libdlog_LDADD = \
 perf_libdlog_SOURCES = \
        tests/performance_test.c
 
+
+usrlibexeclibdlog_PROGRAMS += dlog_stdout_benchmark
+dlog_stdout_benchmark_CFLAGS = \
+       $(AM_CFLAGS) \
+       -fPIE
+
+dlog_stdout_benchmark_LDFLAGS = \
+       $(AM_LDFLAGS) \
+       -pie
+
+dlog_stdout_benchmark_DEPENDENCIES = \
+       libdlog.la
+
+dlog_stdout_benchmark_LDADD = \
+       libdlog.la
+
+dlog_stdout_benchmark_SOURCES = \
+       tests/stdout_benchmark.c
+
+
 usrlibexeclibdlog_PROGRAMS += test_libdlogutil
 test_libdlogutil_CFLAGS = \
        $(AM_CFLAGS) \
index 248ce40..71783d6 100644 (file)
@@ -301,6 +301,7 @@ chsmack -e 'System' %{_libexecdir}/dlog-log-critical
 %{_bindir}/dlog_cpu
 /usr/share/doc/dlog/README.testsuite
 %{_libexecdir}/libdlog/perf_libdlog
+%{_libexecdir}/libdlog/dlog_stdout_benchmark
 %{_libexecdir}/libdlog/test_libdlog
 %{_libexecdir}/libdlog/test_libdlogutil
 %{_libexecdir}/libdlog/test_libredirect
diff --git a/tests/stdout_benchmark.c b/tests/stdout_benchmark.c
new file mode 100644 (file)
index 0000000..b09f78e
--- /dev/null
@@ -0,0 +1,73 @@
+/* DLOG
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <stdbool.h>
+
+#define LOG_TAG "DLOG_BENCHMARK"
+#include <dlog.h>
+
+int main (int argc, char **argv)
+{
+       if (argc != 4 && argc != 5) {
+               fprintf(stderr, "argc = %d\n", argc);
+               return EXIT_FAILURE;
+       }
+
+       bool buffered = atoi(argv[1]) != 0;
+       setvbuf(stdout, NULL, buffered ? _IOLBF : _IONBF, 0);
+
+       int const N = atoi(argv[2]);
+       char const *MSG = argv[3];
+       char const *where = argc == 4 ? "" : argv[4];
+
+       struct timespec start, end;
+       inline void print_diff (char const *name) {
+               double elapsed_s = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1E9;
+               fprintf(stderr, "%s: %lfus per log (%lfs total per %d logs)\n"
+                       , name
+                       , 1000000 * elapsed_s / N
+                       , elapsed_s
+                       , N
+               );
+       }
+
+       if (strcmp(where, "stdout") == 0 || strcmp(where, "") == 0) {
+               for (int i = 0; i < 10000; ++i)
+                       printf("stdout warm-up\n");
+
+               clock_gettime(CLOCK_MONOTONIC, &start);
+               for (int i = 0; i < N; ++i)
+                       printf("%s\n", MSG);
+               clock_gettime(CLOCK_MONOTONIC, &end);
+               print_diff(buffered ? "stdout (buffered)" : "stdout (not buffered)");
+       }
+
+       if (strcmp(where, "libdlog") == 0 || strcmp(where, "") == 0) {
+               for (int i = 0; i < 10000; ++i)
+                       LOGE("libdlog warm-up\n");
+
+               clock_gettime(CLOCK_MONOTONIC, &start);
+               for (int i = 0; i < N; ++i)
+                       LOGE("%s\n", MSG);
+               clock_gettime(CLOCK_MONOTONIC, &end);
+               print_diff("libdlog");
+       }
+
+       return EXIT_SUCCESS;
+}