libdbus-p2p: add a dual forked test 39/270039/2
authorMichal Bloch <m.bloch@samsung.com>
Fri, 21 Jan 2022 15:31:53 +0000 (16:31 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 24 Jan 2022 18:00:20 +0000 (19:00 +0100)
Instead of a separate client and server binaries,
the common test combines both and forks, similar to
how the other tests implement this.

Change-Id: I7124861bb21330df4aed6ffd02c5b2492408342e
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
Makefile
benchmark/libdbus-p2p-client.cpp
benchmark/libdbus-p2p-common.cpp [new file with mode: 0644]
benchmark/libdbus-p2p-server.cpp
packaging/dbus-tools.spec

index 42d09a57381e51f70e84c5aee05a3cc798f3a9fe..5788af631a01674ee0612b367c92fa4de108bd7d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ DFTSRC=benchmark/common.c
 CXXFLAGS = -std=c++2a -Wall -g `pkg-config --cflags dbus-1`
 LDLIBS = -pthread `pkg-config --libs dbus-1`
 
-all: libdbus-p2p-client libdbus-p2p-server
+all: libdbus-p2p-client libdbus-p2p-server libdbus-p2p-common
        gcc $(CFLAGS) -pg -o benchmark/pipe $(DFTSRC) benchmark/pipe.c $(LDFLAGS)
        gcc $(CFLAGS) -o benchmark/socket $(DFTSRC) benchmark/socket.c $(LDFLAGS)
        gcc $(CFLAGS) -o benchmark/sharedmem $(DFTSRC) benchmark/sharedmem.c $(LDFLAGS)
@@ -22,6 +22,9 @@ libdbus-p2p-client: benchmark/libdbus-p2p-client.cpp
 libdbus-p2p-server: benchmark/libdbus-p2p-server.cpp
        $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o benchmark/$@
 
+libdbus-p2p-common: benchmark/libdbus-p2p-common.cpp benchmark/libdbus-p2p-client.cpp benchmark/libdbus-p2p-server.cpp
+       $(CXX) $(CXXFLAGS) -DLIBDBUS_P2P_COMMON $^ $(LDLIBS) -o benchmark/$@
+
 install:
        make -C policychecker install
        mkdir -p $(DESTDIR)/usr/bin
@@ -32,5 +35,6 @@ install:
        cp benchmark/p2p-gdbus $(DESTDIR)/usr/bin/p2p-gdbus
        cp benchmark/libdbus-p2p-server $(DESTDIR)/usr/bin/libdbus-p2p-server
        cp benchmark/libdbus-p2p-client $(DESTDIR)/usr/bin/libdbus-p2p-client
+       cp benchmark/libdbus-p2p-common $(DESTDIR)/usr/bin/libdbus-p2p-common
        cp benchmark/libdbus $(DESTDIR)/usr/bin/libdbus
        cp benchmark/dbus-tools-batch-tests.sh $(DESTDIR)/usr/bin/dbus-tools-batch-tests.sh
index 97882656ad581a99402a4875976b7221fa3fd04c..12a7b76fe997cbf9c9282672ccc95aee3bedb598 100644 (file)
@@ -34,7 +34,7 @@ static constexpr const char * TEST_PATH = "/org/test";
 static constexpr const char * TEST_NAME = "org.test";
 static constexpr const char * TEST_METHOD = "Perf";
 
-struct run_opts {
+struct client_run_opts {
        bool show_help = false;
 
        /* The value is mostly conforming to other test's default,
@@ -63,9 +63,9 @@ struct run_opts {
        std::string sock_path = "/tmp/test";
 };
 
-run_opts get_opt (int argc, char ** argv)
+static client_run_opts get_opt (int argc, char ** argv)
 {
-       run_opts ret;
+       client_run_opts ret;
 
        int opt;
        while ((opt = getopt(argc, argv, "d:m:t:p:h")) != -1)
@@ -92,7 +92,7 @@ run_opts get_opt (int argc, char ** argv)
        return ret;
 }
 
-void show_help (const char * argv0)
+static void show_help (const char * argv0)
 {
        std::cout << "Usage: " << argv0
                << " [-m message_size] "
@@ -103,9 +103,9 @@ void show_help (const char * argv0)
        ;
 }
 
-int main (int argc, char ** argv)
+int main_client (int argc, char ** argv)
 {
-       run_opts opt = get_opt (argc, argv);
+       client_run_opts opt = get_opt (argc, argv);
 
        if (opt.show_help) {
                show_help (argv[0]);
@@ -161,3 +161,7 @@ int main (int argc, char ** argv)
 
        return EXIT_SUCCESS;
 }
+
+#ifndef LIBDBUS_P2P_COMMON
+int main(int argc, char ** argv) { return main_client(argc, argv); }
+#endif
diff --git a/benchmark/libdbus-p2p-common.cpp b/benchmark/libdbus-p2p-common.cpp
new file mode 100644 (file)
index 0000000..1c01371
--- /dev/null
@@ -0,0 +1,72 @@
+/* MIT License
+ *
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#include <unistd.h>
+
+#include <chrono>
+#include <cstring>
+#include <iostream>
+#include <thread>
+
+using namespace std::chrono_literals;
+
+extern int main_server(int, char **);
+extern int main_client(int, char **);
+
+int main(int argc, char ** argv)
+{
+       /* Runs the server and the client from the same binary via fork.
+        * This is in an attempt to make the test behave more like the
+        * other tests which work that way. See the server and the client
+        * for their argument format and other implementation details. */
+
+       int separator = 0;
+       while (strcmp(argv[separator], "--") && separator < argc)
+               ++separator;
+       if (separator == argc) {
+               std::cerr << "Usage: " << argv[0] << " serverargs -- clientargs" << std::endl;
+               return EXIT_FAILURE;
+       }
+
+       int child_pid = fork ();
+       if (child_pid == -1) {
+               std::cerr << "fork failed! " << errno << std::endl;
+               return EXIT_FAILURE;
+       }
+
+       if (child_pid == 0) {
+               argc = separator;
+               argv[separator] = nullptr;
+
+               return main_server (argc, argv);
+       } else {
+               /* Let the server set up,
+                * even under heavy load. */
+               std::this_thread::sleep_for (10s);
+
+               /* NB: argv[0] stays (program name), while
+                * argv[argc] is still part of the array. */
+               for (int i = 1; i <= argc - separator; ++i)
+                       argv[i] = argv[i + separator];
+               return main_client (argc - separator, argv);
+       }
+}
index f231e5a7e8062924e473dd887da86ce79a03291c..86628d7d917dcd88b18fa5d9f0072307183cd446 100644 (file)
@@ -40,7 +40,7 @@
 
 static constexpr const char * TEST_PATH = "/org/test";
 
-struct run_opts {
+struct server_run_opts {
        bool show_help = false;
 
        /* With the default client settings, this is 5 seconds' worth of
@@ -73,9 +73,9 @@ struct run_opts {
        std::optional <std::string> raw_data_path = std::nullopt;
 };
 
-run_opts get_opt (int argc, char ** argv)
+static server_run_opts get_opt (int argc, char ** argv)
 {
-       run_opts ret;
+       server_run_opts ret;
 
        int opt;
        while ((opt = getopt(argc, argv, ":ihp:r:t:w")) != -1)
@@ -137,7 +137,7 @@ struct conn_with_metadata {
 
        static DBusObjectPathVTable vtable;
 
-       conn_with_metadata (const run_opts & ro, DBusConnection * c)
+       conn_with_metadata (const server_run_opts & ro, DBusConnection * c)
                : total_time (0)
                , total_count (0)
                , min_time (std::numeric_limits <uint64_t> :: max ())
@@ -263,7 +263,7 @@ DBusObjectPathVTable conn_with_metadata::vtable =
        , nullptr
 };
 
-void show_help (const char * argv0)
+static void show_help (const char * argv0)
 {
        std::cout << "Usage: " << argv0
                << " [-t warmup_tries]"
@@ -275,7 +275,7 @@ void show_help (const char * argv0)
        ;
 }
 
-void dispatch_connection (conn_with_metadata & cwm, bool busy_wait)
+static void dispatch_connection (conn_with_metadata & cwm, bool busy_wait)
 {
        if (dbus_connection_read_write_dispatch (cwm.conn.get(), busy_wait ? 0 : -1))
                return;
@@ -305,7 +305,7 @@ struct DBusServer_deleter {
        }
 };
 
-void read_from_watch (DBusWatch * watch, bool wait_infinitely)
+static void read_from_watch (DBusWatch * watch, bool wait_infinitely)
 {
        struct pollfd pfd;
        pfd.events = POLLIN;
@@ -329,12 +329,12 @@ extern "C" void sig_handler (int sig)
        std::exit (EXIT_SUCCESS);
 }
 
-int main (int argc, char ** argv)
+int main_server (int argc, char ** argv)
 {
        std::signal (SIGINT , sig_handler);
        std::signal (SIGTERM, sig_handler);
 
-       run_opts opt = get_opt (argc, argv);
+       server_run_opts opt = get_opt (argc, argv);
 
        if (opt.show_help) {
                show_help (argv[0]);
@@ -412,3 +412,6 @@ int main (int argc, char ** argv)
        return EXIT_SUCCESS;
 }
 
+#ifndef LIBDBUS_P2P_COMMON
+int main(int argc, char ** argv) { return main_server(argc, argv); }
+#endif
index 1822d1150ef81c7b657f8482c946f2c35d3fe64e..85265ba83a868d2259411d2470b9b56feb842df8 100644 (file)
@@ -58,5 +58,6 @@ make
 %{_bindir}/libdbus
 %{_bindir}/libdbus-p2p-server
 %{_bindir}/libdbus-p2p-client
+%{_bindir}/libdbus-p2p-common
 %{_bindir}/p2p-gdbus
 %{_bindir}/dbus-tools-batch-tests.sh