Add test to check performance of loading config files 56/200956/4
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Wed, 6 Mar 2019 10:10:23 +0000 (11:10 +0100)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Thu, 7 Mar 2019 14:22:41 +0000 (15:22 +0100)
Change-Id: I6354bc17befa330f1b63efc792ab951a9c0ebe39

Makefile.am
src/stest_load_perf.cpp [new file with mode: 0644]

index 0ac5c8b..be78f32 100644 (file)
@@ -190,7 +190,7 @@ libdbuspolicy_tests_SOURCES = src/test_runner.c
 runnerdir = ${libdir}/dbus-tests/runner/
 
 alonetestdir = ${libdir}/dbus-tests/test-suites/libdbuspolicy-tests/
-alonetest_PROGRAMS = dbus_daemon stest_ownership stest_method_call stest_signal stest_cynara stest_memory stest_performance
+alonetest_PROGRAMS = dbus_daemon stest_ownership stest_method_call stest_signal stest_cynara stest_memory stest_performance stest_load_perf
 
 dbus_daemon_SOURCES = src/dbus_daemon.c
 stest_ownership_SOURCES = src/stest_ownership.c src/stest_common.c
@@ -199,6 +199,7 @@ stest_signal_SOURCES = src/stest_signal.c src/stest_common.c
 stest_cynara_SOURCES = src/stest_cynara.c src/stest_common.c
 stest_memory_SOURCES = src/stest_memory.c
 stest_performance_SOURCES = src/stest_performance.cpp
+stest_load_perf_SOURCES = src/stest_load_perf.cpp
 
 stest_ownership_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS) $(DLOG_LIBS)
 stest_method_call_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS) $(DLOG_LIBS)
@@ -206,6 +207,8 @@ stest_signal_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS) $
 stest_cynara_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS) $(DLOG_LIBS)
 stest_memory_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS) $(DLOG_LIBS)
 stest_performance_LDADD = src/libinternal.a -lexpat -lstdc++ $(CYNARA_LIBS) $(DLOG_LIBS)
+stest_load_perf_LDADD = src/libinternal.a -lexpat -lstdc++ $(CYNARA_LIBS) $(DLOG_LIBS)
+stest_load_perf_CPPFLAGS = -ggdb ${AM_CPPFLAGS} -O0
 
 all-tests:: $(alonetest_PROGRAMS) $(runner_PROGRAMS)
 endif
diff --git a/src/stest_load_perf.cpp b/src/stest_load_perf.cpp
new file mode 100644 (file)
index 0000000..70208ca
--- /dev/null
@@ -0,0 +1,157 @@
+#include <unistd.h>
+#include <getopt.h>
+
+#include <iostream>
+#include <string>
+#include <sys/types.h>
+#include <dbuspolicy1/libdbuspolicy1.h>
+#include "internal/internal.h"
+#include "internal/policy.hpp"
+#include "internal/naive_policy_checker.hpp"
+#include "internal/serializer.hpp"
+#include "internal/include/fb_generated.h"
+#include "internal/storage_backend_serialized.hpp"
+#include "internal/storage_backend_serialized.hpp"
+#include "libdbuspolicy1-private.h"
+
+using namespace ldp_xml_parser;
+using namespace ldp_serialized;
+
+enum class Choice {
+       NONE,
+       FB,
+       XML,
+       XMLplusFB,
+       ALL,
+};
+
+bool measure(std::function<bool()> func, size_t count, const char *desc) {
+       bool flag = true;
+       clock_t begin = clock();
+       for (size_t i = 0; i < count; i++)
+               flag &= func();
+       clock_t end = clock();
+
+       std::cout << desc << ": " << static_cast<double>(end - begin)/CLOCKS_PER_SEC << std::endl;
+       return flag;
+}
+
+bool run_xml(const char *conf_file) {
+       return __internal_init(SYSTEM_BUS, conf_file) == 0;
+}
+
+bool run_xml_plus_fb(const char *conf_file) {
+       Serializer serializer;
+       size_t size;
+       uint8_t *buff = serializer.serialize(conf_file, size);
+
+       const FB::File *file = FB::GetFile(buff);
+
+       StorageBackendSerialized storage;
+       return storage.init(file);
+}
+
+bool run_fb(const char *conf_file) {
+       StorageBackendSerialized sbs;
+       return sbs.init(conf_file, true);
+}
+
+void run_tests(const char *conf_file, const char *conf_bin, size_t c, Choice ch) {
+       if (ch == Choice::ALL || ch == Choice::XML) {
+               if (!measure([&conf_file, c]() { return run_xml(conf_file); }, c, "XML")) {
+                       cout << "ERROR" << endl;
+               }
+       }
+       if (ch == Choice::ALL || ch == Choice::FB) {
+               if (!measure([&conf_bin, c]() { return run_fb(conf_bin); }, c, "FB")) {
+                       cout << "ERROR" << endl;
+               }
+       }
+       if (ch == Choice::ALL || ch == Choice::XMLplusFB)
+               if (!measure([&conf_file, c]() { return run_xml_plus_fb(conf_file); }, c, "FB after XML")) {
+                       cout << "ERROR" << endl;
+               }
+}
+
+void print_help(const char *name) {
+       cout << endl;
+       cout << "usage: " << name << " {-f <config_bin>|-x|-d|-a <config_bin>} {--system|--session|-c <config_xml>} <count>" << endl;
+       cout << endl;
+       cout << "       -f <config_bin> - Flatbuffers" << endl;
+       cout << "       -x              - XML" << endl;
+       cout << "       -d              - FB after XML" << endl;
+       cout << "       -a <config_bin> - All tests" << endl;
+       cout << endl;
+}
+
+static const struct option options[] {
+       {"system", no_argument, 0, 0},
+       {"session", no_argument, 0, 0}
+};
+
+int main(int argc, char *argv[])
+{
+       int c;
+       std::string input_filename = system_bus_conf_file_primary();
+       std::string binary_file = "";
+       size_t count = 100;
+       Choice choice = Choice::NONE;
+
+       while (1) {
+               int option_index;
+               c = getopt_long(argc, argv, "f:xda:c:", options, &option_index);
+               if (c == -1)
+                       break;
+               switch(c) {
+               case 0:
+                       if (option_index == 1)
+                               input_filename = session_bus_conf_file_primary();
+                       break;
+               case 'a':
+                       if (choice != Choice::NONE) {
+                               print_help(argv[0]);
+                               return -1;
+                       }
+                       choice = Choice::ALL;
+                       binary_file = optarg;
+                       break;
+               case 'f':
+                       if (choice != Choice::NONE) {
+                               print_help(argv[0]);
+                               return -1;
+                       }
+                       choice = Choice::FB;
+                       binary_file = optarg;
+                       break;
+               case 'x':
+                       if (choice != Choice::NONE) {
+                               print_help(argv[0]);
+                               return -1;
+                       }
+                       choice = Choice::XML;
+                       break;
+               case 'd':
+                       if (choice != Choice::NONE) {
+                               print_help(argv[0]);
+                               return -1;
+                       }
+                       choice = Choice::XMLplusFB;
+                       break;
+               case 'c':
+                       input_filename = optarg;
+                       break;
+               }
+       }
+
+       if (optind < argc) {
+               count = stoi(argv[optind]);
+       } else {
+               print_help(argv[0]);
+               return 1;
+       }
+
+       __internal_init_once();
+       run_tests(input_filename.c_str(), binary_file.c_str(), count, choice);
+
+       return 0;
+}