File: test-dispatcher.cpp

package info (click to toggle)
spice 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,460 kB
  • sloc: ansic: 43,305; cpp: 30,185; sh: 5,342; python: 3,040; makefile: 844
file content (148 lines) | stat: -rw-r--r-- 4,088 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2020 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library 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 library; if not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Test Dispatcher class and speed
 */

#include <config.h>

#include <cstdio>
#include <cstdlib>

#include "basic-event-loop.h"
#include "test-glib-compat.h"
#include "reds.h"
#include "dispatcher.h"
#include "win-alarm.h"

// iterations to run for each test, useful also to check the speed
static unsigned iterations = 100;

static SpiceCoreInterface *core;
static SpiceCoreInterfaceInternal core_int;
static red::shared_ptr<Dispatcher> dispatcher;
static SpiceWatch *watch;
// incremental number we use during the test, each message sent is incremented
static unsigned num;
using TestFixture = int;

static void test_dispatcher_setup(TestFixture *fixture, gconstpointer user_data)
{
    num = 0;
    dispatcher.reset();
    g_assert_null(core);
    core = basic_event_loop_init();
    g_assert_nonnull(core);
    core_int = core_interface_adapter;
    core_int.public_interface = core;
    dispatcher = red::make_shared<Dispatcher>(10);
    // TODO not create Reds, just the internal interface ??
    watch = dispatcher->create_watch(&core_int);
}

static void test_dispatcher_teardown(TestFixture *fixture, gconstpointer user_data)
{
    g_assert_nonnull(core);

    red_watch_remove(watch);
    watch = nullptr;
    dispatcher.reset();
    basic_event_loop_destroy();
    core = nullptr;
}

// test message to sent
struct Msg {
    uint64_t num;
    void *dummy;
};

// message handler to mark stop, stop the event loop
static void msg_end(void *, Msg *msg)
{
    g_assert_cmpint(num, ==, iterations);
    basic_event_loop_quit();
}

// message handler to check message number
static void msg_check(void *, Msg *msg)
{
    g_assert_cmpint(msg->num, ==, num);
    ++num;
}

static void *thread_proc(void *arg)
{
    // the argument is number of messages with NACK to send
    int n_nack = GPOINTER_TO_INT(arg);
    g_assert_cmpint(n_nack, >=, 0);
    g_assert_cmpint(n_nack, <=, 10);

    auto start = spice_get_monotonic_time_ns();

    // repeat sending messages
    for (unsigned n = 0; n < iterations; ++n) {
        Msg msg{n, nullptr};
        dispatcher->send_message_custom(msg_check, &msg, (n % 10) >= n_nack);
    }

    // one last sync to wait
    Msg msg{0, nullptr};
    dispatcher->send_message_custom(msg_end, &msg, true);

    // measure time
    auto cost = spice_get_monotonic_time_ns() - start;

    printf("With ACK/NACK %d/%d time spent %gus each over %u iterations\n",
           10 - n_nack, n_nack,
           cost / 1000.0 / iterations, iterations);
    return nullptr;
}

static void test_dispatcher(TestFixture *fixture, gconstpointer user_data)
{
    pthread_t th;

    g_assert_cmpint(pthread_create(&th, nullptr, thread_proc, (void *) user_data), ==, 0);

    // start all test
    alarm(20);
    basic_event_loop_mainloop();
    alarm(0);

    pthread_join(th, nullptr);
}

int main(int argc, char *argv[])
{
    g_test_init(&argc, &argv, NULL);

    // override number of iteration passing a parameter
    if (argc >= 2 && atoi(argv[1]) > 10) {
        iterations = atoi(argv[1]);
    }

    for (int i = 0; i <= 10; ++i) {
        char name[64];
        sprintf(name, "/server/dispatcher/%d", i);
        g_test_add(name, TestFixture, GINT_TO_POINTER(i), test_dispatcher_setup,
                   test_dispatcher, test_dispatcher_teardown);
    }

    return g_test_run();
}