aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/server/initialize_threads_calculator/initialize_threads_calculator_test.cpp
blob: fc899f3a8c78510e270a38439ee13c0d0f596451 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchcore/proton/server/initialize_threads_calculator.h>
#include <vespa/searchlib/test/directory_handler.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/threadstackexecutor.h>

using namespace proton;
using vespalib::ThreadStackExecutor;

class InitializeThreadsCalculatorTest : public search::test::DirectoryHandler, public testing::Test {
public:
    InitializeThreadsCalculatorTest() : DirectoryHandler("tmp") {}
};

void
expect_successful_init(uint32_t exp_threads)
{
    constexpr uint32_t cfg_threads = 9;
    InitializeThreadsCalculator i(HwInfo::Cpu(cfg_threads), "tmp", cfg_threads);
    EXPECT_EQ(exp_threads, i.num_threads());
    EXPECT_TRUE(i.threads().get() != nullptr);
    EXPECT_EQ(exp_threads, dynamic_cast<const ThreadStackExecutor&>(*i.threads()).getNumThreads());
    i.init_done();
    EXPECT_TRUE(i.threads().get() == nullptr);
}

void
expect_aborted_init(uint32_t exp_threads, uint32_t cfg_threads = 9)
{
    InitializeThreadsCalculator i(HwInfo::Cpu(cfg_threads), "tmp", cfg_threads);
    EXPECT_EQ(exp_threads, i.num_threads());
    EXPECT_TRUE(i.threads().get() != nullptr);
    EXPECT_EQ(exp_threads, dynamic_cast<const ThreadStackExecutor&>(*i.threads()).getNumThreads());
}

TEST_F(InitializeThreadsCalculatorTest, initialize_threads_unchanged_when_init_is_successful)
{
    expect_successful_init(9);
    // The previous init was successful,
    // so we still use the configured number of initialize threads.
    expect_successful_init(9);
}

TEST_F(InitializeThreadsCalculatorTest, initialize_threads_cut_in_half_when_init_is_aborted)
{
    expect_aborted_init(9);
    expect_aborted_init(4);
    expect_aborted_init(2);
    expect_aborted_init(1);
    expect_aborted_init(1);
}

TEST_F(InitializeThreadsCalculatorTest, zero_initialize_threads_is_special)
{
    {
        InitializeThreadsCalculator i(HwInfo::Cpu(10), "tmp", 0);
        EXPECT_EQ(0, i.num_threads());
        EXPECT_TRUE(i.threads().get() == nullptr);
    }
    expect_aborted_init(1, 0);
    expect_aborted_init(1, 0);
}

void
expect_lower(uint32_t cores, uint32_t configured) {
    InitializeThreadsCalculator i(HwInfo::Cpu(cores), "tmp", configured);
    EXPECT_EQ(std::min(cores, configured), i.num_threads());
    i.init_done();
}

TEST_F(InitializeThreadsCalculatorTest, lower_of_wanted_and_cores)
{
    expect_lower(1, 7);
    expect_lower(6, 7);
    expect_lower(7, 7);
    expect_lower(7, 6);
    expect_lower(7, 1);
}

GTEST_MAIN_RUN_ALL_TESTS()