Skip to content

Commit

Permalink
test_sampler.cpp: put in code for sampler test
Browse files Browse the repository at this point in the history
  • Loading branch information
vlkale authored Apr 1, 2024
1 parent 9dde439 commit 64a9f1a
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/sampler/test_sampler.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
#include <iostream>
#include <sstream>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "Kokkos_Core.hpp"

struct Tester {
template <typename execution_space>
explicit Tester(const execution_space& space) {
//! Explicitly launch a kernel with a name, and run it 150 times with kernel
//! logger. Use a periodic sampling with skip rate 51. This should print
//! out 2 invocation, and there is a single matcher with a regular
//! expression to check this.

for (int iter = 0; iter < 150; iter++) {
Kokkos::parallel_for("named kernel",
Kokkos::RangePolicy<execution_space>(space, 0, 1),
*this);
}
}

KOKKOS_FUNCTION void operator()(const int) const {}
};

static const std::vector<std::string> matchers{
"(.*)KokkosP: sample 51 calling child-begin function...(.*)",
"(.*)KokkosP: sample 51 finished with child-begin function.(.*)",
"(.*)KokkosP: sample 51 calling child-end function...(.*)",
"(.*)KokkosP: sample 51 calling child-end function.(.*)",
"(.*)KokkosP: sample 102 calling child-begin function...(.*)",
"(.*)KokkosP: sample 102 finished with child-begin function.(.*)",
"(.*)KokkosP: sample 102 calling child-end function...(.*)",
"(.*)KokkosP: sample 102 calling child-end function.(.*)"};

/**
* @test This test checks that the tool effectively samples.
*
*/
TEST(SamplerTest, ktoEnvVarDefault) {
//! Initialize @c Kokkos.
Kokkos::initialize();

//! Redirect output for later analysis.
std::cout.flush();
std::ostringstream output;
std::streambuf* coutbuf = std::cout.rdbuf(output.rdbuf());

//! Run tests. @todo Replace this with Google Test.
Tester tester(Kokkos::DefaultExecutionSpace{});

//! Finalize @c Kokkos.
Kokkos::finalize();

//! Restore output buffer.
// std::cout.flush();
std::cout.rdbuf(coutbuf);
std::cout << output.str() << std::endl;

//! Analyze test output.
for (const auto& matcher : matchers) {
EXPECT_THAT(output.str(), ::testing::ContainsRegex(matcher));
} // end TEST
}

0 comments on commit 64a9f1a

Please sign in to comment.