Skip to content

Commit e15477b

Browse files
oschaafhtuch
authored andcommitted
Tech debt: improve coverage & move some code (#99)
cover switch cases in statIdtoFriendlyStatName() Add CpuAffinityDetectionFailure test Relocate determineCpuCoresWithAffinity to where it belongs Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
1 parent 55356e2 commit e15477b

16 files changed

Lines changed: 89 additions & 62 deletions

ci/run_coverage.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ if [ "$VALIDATE_COVERAGE" == "true" ]
3232
then
3333
COVERAGE_VALUE=$(grep -Po '.*lines[.]*: \K(\d|\.)*' "${COVERAGE_SUMMARY}")
3434
# TODO(oschaaf): The target is 97.5%, so up this whenever possible in follow ups.
35-
COVERAGE_THRESHOLD=96.9
35+
COVERAGE_THRESHOLD=97.3
3636
COVERAGE_FAILED=$(echo "${COVERAGE_VALUE}<${COVERAGE_THRESHOLD}" | bc)
3737

3838
echo "HTML coverage report is in ${COVERAGE_DIR}/coverage.html"

include/nighthawk/common/platform_util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class PlatformUtil {
2222
* @param duration duration that the calling thread should sleep.
2323
*/
2424
virtual void sleep(std::chrono::microseconds duration) const PURE;
25+
26+
/**
27+
* @return uint32_t 0 #CPUs that the current thread has affinity with. 0 on failure.
28+
*/
29+
virtual uint32_t determineCpuCoresWithAffinity() const PURE;
2530
};
2631

2732
using PlatformUtilPtr = std::unique_ptr<PlatformUtil>;

source/client/client.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ bool Main::run() {
4848
nighthawk::client::Verbosity::VerbosityOptions_Name(options_->verbosity())),
4949
"[%T.%f][%t][%L] %v", log_lock);
5050
Envoy::Event::RealTimeSystem time_system; // NO_CHECK_FORMAT(real_time)
51-
ProcessImpl process(*options_, time_system);
51+
PlatformUtilImpl platform_util;
52+
ProcessImpl process(*options_, time_system, platform_util);
5253
OutputCollectorFactoryImpl output_format_factory(time_system, *options_);
5354
auto collector = output_format_factory.create();
5455
if (process.run(*collector)) {

source/client/output_collector_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ConsoleOutputCollectorImpl::formatProtoDuration(const Envoy::Protobuf::Duration&
7878
(c % 1'000'000) / 1'000, c % 1'000);
7979
}
8080

81-
std::string ConsoleOutputCollectorImpl::statIdtoFriendlyStatName(absl::string_view stat_id) const {
81+
std::string ConsoleOutputCollectorImpl::statIdtoFriendlyStatName(absl::string_view stat_id) {
8282
if (stat_id == "benchmark_http_client.queue_to_connect") {
8383
return "Queueing and connection setup latency";
8484
} else if (stat_id == "benchmark_http_client.request_to_response") {

source/client/output_collector_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class ConsoleOutputCollectorImpl : public OutputCollectorImpl {
3232
public:
3333
ConsoleOutputCollectorImpl(Envoy::TimeSource& time_source, const Options& options);
3434
std::string toString() const override;
35+
static std::string statIdtoFriendlyStatName(absl::string_view stat_id);
3536

3637
private:
37-
std::string statIdtoFriendlyStatName(absl::string_view stat_id) const;
3838
std::string formatProtoDuration(const Envoy::Protobuf::Duration& duration) const;
3939
};
4040

source/client/process_impl.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ using namespace std::chrono_literals;
3939
namespace Nighthawk {
4040
namespace Client {
4141

42-
ProcessImpl::ProcessImpl(const Options& options, Envoy::Event::TimeSystem& time_system)
42+
ProcessImpl::ProcessImpl(const Options& options, Envoy::Event::TimeSystem& time_system,
43+
const PlatformUtil& platform_util)
4344
: time_system_(time_system), store_factory_(options), store_(store_factory_.create()),
4445
api_(thread_factory_, *store_, time_system_, file_system_),
4546
dispatcher_(api_.allocateDispatcher()), cleanup_([this] { tls_.shutdownGlobalThreading(); }),
46-
benchmark_client_factory_(options), sequencer_factory_(options), options_(options) {
47+
benchmark_client_factory_(options), sequencer_factory_(options), options_(options),
48+
platform_util_(platform_util) {
4749
configureComponentLogLevels(spdlog::level::from_str(
4850
nighthawk::client::Verbosity::VerbosityOptions_Name(options_.verbosity())));
4951
tls_.registerThread(*dispatcher_, true);
@@ -89,7 +91,7 @@ void ProcessImpl::configureComponentLogLevels(spdlog::level::level_enum level) {
8991
}
9092

9193
uint32_t ProcessImpl::determineConcurrency() const {
92-
uint32_t cpu_cores_with_affinity = PlatformUtils::determineCpuCoresWithAffinity();
94+
uint32_t cpu_cores_with_affinity = platform_util_.determineCpuCoresWithAffinity();
9395
if (cpu_cores_with_affinity == 0) {
9496
ENVOY_LOG(warn, "Failed to determine the number of cpus with affinity to our thread.");
9597
cpu_cores_with_affinity = std::thread::hardware_concurrency();

source/client/process_impl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace Client {
3636
*/
3737
class ProcessImpl : public Process, public Envoy::Logger::Loggable<Envoy::Logger::Id::main> {
3838
public:
39-
ProcessImpl(const Options& options, Envoy::Event::TimeSystem& time_system);
39+
ProcessImpl(const Options& options, Envoy::Event::TimeSystem& time_system,
40+
const PlatformUtil& platform_util);
4041

4142
uint32_t determineConcurrency() const;
4243
bool run(OutputCollector& collector) override;
@@ -67,6 +68,7 @@ class ProcessImpl : public Process, public Envoy::Logger::Loggable<Envoy::Logger
6768
const BenchmarkClientFactoryImpl benchmark_client_factory_;
6869
const SequencerFactoryImpl sequencer_factory_;
6970
const Options& options_;
71+
const PlatformUtil& platform_util_;
7072
};
7173

7274
} // namespace Client

source/client/service_impl.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ void ServiceImpl::handleExecutionRequest(const nighthawk::client::ExecutionReque
2424
// We scope here because the ProcessImpl instance must be destructed before we write the response
2525
// and set running to false.
2626
{
27-
ProcessImpl process(*options, time_system_);
27+
PlatformUtilImpl platform_util;
28+
ProcessImpl process(*options, time_system_, platform_util);
2829
OutputCollectorFactoryImpl output_format_factory(time_system_, *options);
2930
auto logging_context = std::make_unique<Envoy::Logger::Context>(
3031
spdlog::level::from_str(

source/common/platform_util_impl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ class PlatformUtilImpl : public PlatformUtil {
1616
void sleep(std::chrono::microseconds duration) const override {
1717
std::this_thread::sleep_for(duration); // NO_CHECK_FORMAT(real_time)
1818
};
19+
20+
uint32_t determineCpuCoresWithAffinity() const override {
21+
// TODO(oschaaf): mull over what to do w/regard to hyperthreading.
22+
const pthread_t thread = pthread_self();
23+
cpu_set_t cpuset;
24+
int i;
25+
26+
CPU_ZERO(&cpuset);
27+
i = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
28+
if (i == 0) {
29+
return CPU_COUNT(&cpuset);
30+
}
31+
return 0;
32+
}
1933
};
2034

2135
} // namespace Nighthawk

source/common/utility.cc

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,6 @@
1111

1212
namespace Nighthawk {
1313

14-
namespace PlatformUtils {
15-
16-
// returns 0 on failure. returns the number of HW CPU's
17-
// that the current thread has affinity with.
18-
// TODO(oschaaf): mull over what to do w/regard to hyperthreading.
19-
uint32_t determineCpuCoresWithAffinity() {
20-
const pthread_t thread = pthread_self();
21-
cpu_set_t cpuset;
22-
int i;
23-
24-
CPU_ZERO(&cpuset);
25-
i = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
26-
if (i == 0) {
27-
return CPU_COUNT(&cpuset);
28-
}
29-
return 0;
30-
}
31-
32-
} // namespace PlatformUtils
33-
3414
std::map<std::string, uint64_t>
3515
Utility::mapCountersFromStore(const Envoy::Stats::Store& store,
3616
const StoreCounterFilter& filter) const {

0 commit comments

Comments
 (0)