45f5caddd6ac644a37b1d8b0faad5cf3643184c7
[platform/core/system/libdbuspolicy.git] / src / stest_load_perf.cpp
1 #include "internal/include/fb_generated.h"
2 #include "internal/naive_policy_checker.hpp"
3 #include "internal/policy.hpp"
4 #include "internal/serializer.hpp"
5 #include "internal/storage_backend_serialized.hpp"
6 #include "internal/storage_backend_serialized.hpp"
7 #include "internal/tslog.hpp"
8 #include "libdbuspolicy1-private.h"
9 #include <dbuspolicy1/libdbuspolicy1.h>
10 #include <getopt.h>
11 #include <iostream>
12 #include <string>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 using namespace ldp_xml_parser;
17 using namespace ldp_serialized;
18
19 enum class Choice {
20         NONE,
21         FB,
22         XML,
23         XMLplusFB,
24         ALL,
25 };
26
27 bool measure(std::function<bool()> func, size_t count, const char *desc) {
28         bool flag = true;
29         clock_t begin = clock();
30         for (size_t i = 0; i < count; i++)
31                 flag &= func();
32         clock_t end = clock();
33
34         std::cout << desc << ": " << static_cast<double>(end - begin)/CLOCKS_PER_SEC << std::endl;
35         return flag;
36 }
37
38 bool run_xml(const char *conf_file) {
39         return policy_checker_system().initDb(conf_file);
40 }
41
42 bool run_xml_plus_fb(const char *conf_file, bool verify) {
43         Serializer serializer;
44         size_t size;
45         const uint8_t *buff = serializer.serialize(conf_file, size);
46
47         if (verify) {
48                 auto verifier = flatbuffers::Verifier(buff, size);
49                 if (!FB::VerifyFileBuffer(verifier)) {
50                         std::cout << "verification of serialized data: failed" << std::endl;
51                         return false;
52                 }
53         }
54
55         StorageBackendSerialized storage;
56         return storage.initFromData(buff);
57 }
58
59 bool run_fb(const char *conf_file, bool verify) {
60         StorageBackendSerialized sbs;
61         return sbs.init(conf_file, verify);
62 }
63
64 void run_tests(const char *conf_file, const char *conf_bin, size_t c, Choice ch, bool verify) {
65         if (ch == Choice::ALL || ch == Choice::XML) {
66                 if (!measure([&conf_file, c]() { return run_xml(conf_file); }, c, "XML")) {
67                         std::cout << "ERROR" << std::endl;
68                 }
69         }
70         if (ch == Choice::ALL || ch == Choice::FB) {
71                 if (!measure([&conf_bin, c, verify]() { return run_fb(conf_bin, verify); }, c, "FB")) {
72                         std::cout << "ERROR" << std::endl;
73                 }
74         }
75         if (ch == Choice::ALL || ch == Choice::XMLplusFB)
76                 if (!measure([&conf_file, c, verify]() { return run_xml_plus_fb(conf_file, verify); }, c, "FB after XML")) {
77                         std::cout << "ERROR" << std::endl;
78                 }
79 }
80
81 void print_help(const char *name) {
82         std::cout << std::endl;
83         std::cout << "usage: " << name << " {-f <config_bin>|-x|-d|-a <config_bin>} {--system|--session|-c <config_xml>} <count>" << std::endl;
84         std::cout << std::endl;
85         std::cout << "       -f <config_bin> - Flatbuffers" << std::endl;
86         std::cout << "       -x              - XML" << std::endl;
87         std::cout << "       -d              - FB after XML" << std::endl;
88         std::cout << "       -a <config_bin> - All tests" << std::endl;
89         std::cout << "       -v              - Verify" << std::endl;
90         std::cout << std::endl;
91 }
92
93 static const struct option options[] {
94         {"system", no_argument, 0, 0},
95         {"session", no_argument, 0, 0}
96 };
97
98 int main(int argc, char *argv[])
99 {
100         int c;
101         std::string input_filename = system_bus_conf_file_primary();
102         std::string binary_file = "";
103         size_t count = 100;
104         Choice choice = Choice::NONE;
105         bool verify = false;
106
107         while (1) {
108                 int option_index;
109                 c = getopt_long(argc, argv, "f:xda:c:v", options, &option_index);
110                 if (c == -1)
111                         break;
112                 switch(c) {
113                 case 0:
114                         if (option_index == 1)
115                                 input_filename = session_bus_conf_file_primary();
116                         break;
117                 case 'a':
118                         if (choice != Choice::NONE) {
119                                 print_help(argv[0]);
120                                 return -1;
121                         }
122                         choice = Choice::ALL;
123                         binary_file = optarg;
124                         break;
125                 case 'f':
126                         if (choice != Choice::NONE) {
127                                 print_help(argv[0]);
128                                 return -1;
129                         }
130                         choice = Choice::FB;
131                         binary_file = optarg;
132                         break;
133                 case 'x':
134                         if (choice != Choice::NONE) {
135                                 print_help(argv[0]);
136                                 return -1;
137                         }
138                         choice = Choice::XML;
139                         break;
140                 case 'd':
141                         if (choice != Choice::NONE) {
142                                 print_help(argv[0]);
143                                 return -1;
144                         }
145                         choice = Choice::XMLplusFB;
146                         break;
147                 case 'c':
148                         input_filename = optarg;
149                         break;
150                 case 'v':
151                         verify = true;
152                         break;
153                 }
154         }
155
156         if (optind < argc) {
157                 count = std::stoi(argv[optind]);
158         } else {
159                 print_help(argv[0]);
160                 return 1;
161         }
162
163         tslog::init();
164         run_tests(input_filename.c_str(), binary_file.c_str(), count, choice, verify);
165
166         return 0;
167 }