File: threadpooltester.cpp

package info (click to toggle)
opendht 3.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: cpp: 23,342; python: 2,189; ansic: 2,041; makefile: 207; sh: 72
file content (108 lines) | stat: -rw-r--r-- 2,908 bytes parent folder | download
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
/*
 *  Copyright (C) 2014-2023 Savoir-faire Linux Inc.
 *
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "threadpooltester.h"

#include "opendht/thread_pool.h"
#include <atomic>
#include <thread>

namespace test {
CPPUNIT_TEST_SUITE_REGISTRATION(ThreadPoolTester);
using clock = std::chrono::steady_clock;

void
ThreadPoolTester::setUp() {

}

void
ThreadPoolTester::testThreadPool() {
    dht::ThreadPool pool(16);

    constexpr unsigned N = 64 * 1024;
    std::atomic_uint count {0};
    for (unsigned i=0; i<N; i++)
        pool.run([&] {
            count++;
        });

    auto start = clock::now();
    while (count.load() != N && clock::now() - start < std::chrono::seconds(10))
        std::this_thread::sleep_for(std::chrono::milliseconds(10));

    pool.join();
    CPPUNIT_ASSERT_EQUAL(N, count.load());
}

void
ThreadPoolTester::testExecutor()
{
    dht::ThreadPool pool(8);
    auto executor1 = std::make_shared<dht::Executor>(pool, 1);
    auto executor4 = std::make_shared<dht::Executor>(pool, 4);
    auto executor8 = std::make_shared<dht::Executor>(pool, 8);

    constexpr unsigned N = 64 * 1024;
    unsigned count1 {0};
    std::atomic_uint count4 {0};
    std::atomic_uint count8 {0};
    for (unsigned i=0; i<N; i++) {
        executor1->run([&] { count1++; });
        executor4->run([&] { count4++; });
        executor8->run([&] { count8++; });
    }

    auto start = clock::now();
    while ((count1 != N ||
            count4.load() != N ||
            count8.load() != N) && clock::now() - start < std::chrono::seconds(20))
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    executor1.reset();
    executor4.reset();
    executor8.reset();
    CPPUNIT_ASSERT_EQUAL(N, count1);
    CPPUNIT_ASSERT_EQUAL(N, count4.load());
    CPPUNIT_ASSERT_EQUAL(N, count8.load());
}

void
ThreadPoolTester::testContext()
{
    std::atomic_uint count {0};
    constexpr unsigned N = 64 * 1024;

    {
        dht::ExecutionContext ctx(dht::ThreadPool::computation());
        for (unsigned i=0; i<N; i++) {
            ctx.run([&] { count++; });
        }
    }

    CPPUNIT_ASSERT_EQUAL(N, count.load());

}

void
ThreadPoolTester::tearDown() {
}

}  // namespace test