• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #846

05 Dec 2022 11:16PM UTC coverage: 86.482% (+0.8%) from 85.664%
#846

push

StellarBot
Merge #6093

6093: Replace boost::string_ref with std::string_view r=hkaiser a=hkaiser

Working towards #5497 and #3440

Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

14 of 14 new or added lines in 6 files covered. (100.0%)

172759 of 199764 relevant lines covered (86.48%)

1842530.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.3
/libs/core/resiliency/tests/performance/replicate/pure_async_for_replicate.cpp
1
//  Copyright (c) 2019 National Technology & Engineering Solutions of Sandia,
2
//                     LLC (NTESS).
3
//  Copyright (c) 2019 Nikunj Gupta
4
//
5
//  SPDX-License-Identifier: BSL-1.0
6
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
7
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8

9
#include <hpx/local/future.hpp>
10
#include <hpx/local/init.hpp>
11
#include <hpx/modules/resiliency.hpp>
12
#include <hpx/modules/timing.hpp>
13

14
#include <atomic>
15
#include <cstdint>
16
#include <ctime>
17
#include <exception>
18
#include <iostream>
19
#include <random>
20
#include <stdexcept>
21
#include <utility>
22
#include <vector>
23

24
constexpr int num_iterations = 1000;
25

26
std::random_device rd;
1✔
27
std::mt19937 gen(rd());
1✔
28

29
struct vogon_exception : std::exception
266✔
30
{
31
};
32

33
std::atomic<int> counter(0);
34

35
bool validate(int result)
×
36
{
37
    return result == 42;
×
38
}
39

40
int vote(std::vector<int>&& vect)
×
41
{
42
    return std::move(vect.at(0));
×
43
}
44

45
int universal_ans(std::uint64_t delay_ns, double error)
1,000✔
46
{
47
    std::exponential_distribution<> dist(error);
1,000✔
48

49
    if (delay_ns == 0)
1,000✔
50
        return 42;
×
51

52
    double num = dist(gen);
1,000✔
53
    bool error_flag = false;
1,000✔
54

55
    // Probability of error occurrence is proportional to exp(-error_rate)
56
    if (num > 1.0)
1,000✔
57
    {
58
        error_flag = true;
133✔
59
        ++counter;
133✔
60
    }
133✔
61

62
    std::uint64_t start = hpx::chrono::high_resolution_clock::now();
1,000✔
63

64
    while (true)
30,051,468✔
65
    {
66
        // Check if we've reached the specified delay.
67
        if ((hpx::chrono::high_resolution_clock::now() - start) >= delay_ns)
30,051,468✔
68
        {
69
            // Re-run the thread if the thread was meant to re-run
70
            if (error_flag)
1,000✔
71
                throw vogon_exception();
×
72
            // No error has to occur with this thread, simply break the loop after
73
            // execution is done for the desired time
74
            else
75
                break;
867✔
76
        }
77
    }
78

79
    return 42;
867✔
80
}
867✔
81

82
int hpx_main(hpx::program_options::variables_map& vm)
1✔
83
{
84
    double error = vm["error-rate"].as<double>();
1✔
85
    std::uint64_t delay = vm["exec-time"].as<std::uint64_t>();
1✔
86

87
    {
88
        std::cout << "Starting async" << std::endl;
1✔
89

90
        std::vector<hpx::future<int>> vect;
1✔
91
        vect.reserve(num_iterations);
1✔
92

93
        hpx::chrono::high_resolution_timer t;
1✔
94

95
        for (int i = 0; i < num_iterations; ++i)
1,001✔
96
        {
97
            hpx::future<int> f =
98
                hpx::async(&universal_ans, delay * 1000, error);
1,000✔
99
            vect.push_back(std::move(f));
1,000✔
100
        }
1,000✔
101

102
        try
103
        {
104
            for (int i = 0; i < num_iterations; ++i)
1✔
105
            {
106
                vect[i].get();
1✔
107
            }
×
108
        }
1✔
109
        catch (vogon_exception const&)
110
        {
111
            std::cout << "Errors thrown" << std::endl;
1✔
112
        }
1✔
113
        catch (std::exception& e)
114
        {
115
            std::cout << e.what() << std::endl;
×
116
        }
1✔
117

118
        double elapsed = t.elapsed();
1✔
119
        hpx::util::format_to(std::cout,
1✔
120
            "Async execution time = {1}\n"
1✔
121
            "Number of exceptions = {2}\n",
122
            elapsed, counter);
123
    }
1✔
124

125
    return hpx::local::finalize();
1✔
126
}
1✔
127

128
int main(int argc, char* argv[])
1✔
129
{
130
    using hpx::program_options::options_description;
131
    using hpx::program_options::value;
132

133
    // Configure application-specific options
134
    options_description desc_commandline(
1✔
135
        "Usage: " HPX_APPLICATION_STRING " [options]");
1✔
136

137
    desc_commandline.add_options()("n-value",
2✔
138
        value<std::uint64_t>()->default_value(10),
1✔
139
        "Number of asynchronous launches for async replicate");
140

141
    desc_commandline.add_options()("error-rate",
2✔
142
        value<double>()->default_value(2),
1✔
143
        "Average rate at which error is likely to occur");
144

145
    desc_commandline.add_options()("exec-time",
2✔
146
        value<std::uint64_t>()->default_value(1000),
1✔
147
        "Time in us taken by a thread to execute before it terminates");
148

149
    // Initialize and run HPX
150
    hpx::local::init_params init_args;
1✔
151
    init_args.desc_cmdline = desc_commandline;
1✔
152

153
    return hpx::local::init(hpx_main, argc, argv, init_args);
1✔
154
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc