File: main.cpp

package info (click to toggle)
dbus-cpp 5.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,216 kB
  • sloc: cpp: 9,433; ansic: 2,012; xml: 1,156; makefile: 14; sh: 2
file content (186 lines) | stat: -rw-r--r-- 5,583 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include "benchmark_service.h"

#include <core/dbus/announcer.h>
#include <core/dbus/resolver.h>
#include <core/dbus/asio/executor.h>
#include <core/dbus/types/stl/vector.h>

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/moment.hpp>
#include <boost/accumulators/statistics/variance.hpp>

#include <cstdio>
#include <fstream>

#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

namespace acc = boost::accumulators;
namespace dbus = core::dbus;

namespace
{
dbus::Bus::Ptr the_session_bus()
{
    dbus::Bus::Ptr session_bus = std::make_shared<dbus::Bus>(dbus::WellKnownBus::session);
    return session_bus;
}

struct CrossProcessSync
{
    static const int read_fd = 0;
    static const int write_fd = 1;

    CrossProcessSync()
    {
        if (pipe(fds) < 0)
            throw std::runtime_error(strerror(errno));
    }

    ~CrossProcessSync() noexcept
    {
        ::close(fds[0]);
        ::close(fds[1]);
    }

    void signal_ready()
    {
        int value = 42;
        if (!write(fds[write_fd], std::addressof(value), sizeof(value)))
            throw std::runtime_error(::strerror(errno));
    }

    void wait_for_signal_ready()
    {
        int value;
        if (!read(fds[read_fd], std::addressof(value), sizeof(value)))
            throw std::runtime_error(::strerror(errno));
    }

    int fds[2];
};

bool is_child(pid_t pid)
{
    return pid == 0;
}

int fork_and_run(int argc, char** argv, std::function<int(int, char**)> child, std::function<int(int, char**, pid_t)> parent)
{
    auto pid = fork();

    if (pid < 0)
    {
        throw std::runtime_error(std::string("Could not fork child: ") + std::strerror(errno));
    }

    try
    {
        if (is_child(pid))
        {
            return child(argc, argv);
        }

        return parent(argc, argv, pid);
    }
    catch (...)
    {
        kill(pid, SIGKILL);
    }

    return EXIT_FAILURE;
}
}

int main(int argc, char** argv)
{
    CrossProcessSync cross_process_sync;

    auto server = [&cross_process_sync](int, char**)
    {
        auto bus = the_session_bus();
        bus->install_executor(core::dbus::asio::make_executor(bus));
        std::thread t1
        {
            [&]()
            {
                bus->run();
            }
        };
        test::BenchmarkService::Ptr benchmark_service = dbus::announce_service_on_bus<test::IBenchmarkService, test::BenchmarkService>(bus);
        cross_process_sync.signal_ready();
        if (t1.joinable())
            t1.join();
        return EXIT_SUCCESS;
    };

    auto client = [&cross_process_sync](int, char**, pid_t pid)
    {
        auto bus = the_session_bus();

        cross_process_sync.wait_for_signal_ready();

        auto stub = dbus::resolve_service_on_bus<test::IBenchmarkService, test::BenchmarkServiceStub>(bus);

        std::ofstream out("dbus_benchmark_int64_t.txt");
        acc::accumulator_set<double, acc::stats<acc::tag::mean, acc::tag::lazy_variance > > as;
        std::chrono::high_resolution_clock::time_point before;
        const int32_t default_value = 42;
        const unsigned int iteration_count = 10000;
        for (unsigned int i = 0; i < iteration_count; i++)
        {
            before = std::chrono::high_resolution_clock::now();
            auto value = stub->method_int64(default_value);
            if (value != default_value)
                return EXIT_FAILURE;
            auto duration = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - before);
            out << duration.count() << std::endl;
            as(duration.count());
        }

        std::cout << "MethodInt64 -> Mean: " << acc::mean(as) << " [µs], std. dev.: " << std::sqrt(acc::lazy_variance(as)) << " [µs]" << std::endl;

        out.close();
        out.open("dbus_benchmark_vector_int32_t.txt");
        as = acc::accumulator_set<double, acc::stats<acc::tag::mean, acc::tag::lazy_variance > >();
        const size_t element_count = 100;
        std::vector<int32_t> value(element_count, default_value);
        for (unsigned int i = 0; i < iteration_count; i++)
        {
            before = std::chrono::high_resolution_clock::now();
            stub->method_vector_int32(value);
            auto duration = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - before);
            out << duration.count() << std::endl;
            as(duration.count());
        }

        std::cout << "MethodVectorInt32 -> Mean: " << acc::mean(as) << " [µs], std. dev.: " << std::sqrt(acc::lazy_variance(as)) << " [µs]" << std::endl;

        kill(pid, SIGKILL);

        return EXIT_SUCCESS;
    };

    return fork_and_run(argc, argv, server, client);
}