Commit b0f6c69e authored by Reyhaneh Karimipour's avatar Reyhaneh Karimipour
Browse files

Add sampling interface and tests

parent 45568b1d
build
.DS_Store
\ No newline at end of file
......@@ -4,6 +4,11 @@ project(libxtrace)
include_directories( include )
link_directories(
/usr/lib/x86_64-linux-gnu
/usr/include
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 ")
set(THREADS_PREFER_PTHREAD_FLAG ON)
......@@ -67,7 +72,39 @@ target_link_libraries(luaxtrace libxtrace Threads::Threads protobuf boost_system
install(TARGETS luaxtrace DESTINATION ${CMAKE_INSTALL_LIBDIR})
# Compile the main example program
add_executable (main src/main.cc )
add_executable (main src/main.cc)
target_include_directories(main PRIVATE /usr/local/include/xtrace /usr/include)
target_link_libraries(main libxtrace Threads::Threads protobuf boost_system config++ )
find_package(Catch2 REQUIRED)
# Compile the test_nosample
add_executable(test_nosample test/test_nosample.cpp)
target_include_directories(test_nosample PRIVATE /usr/local/include/xtrace /usr/include)
target_link_libraries(test_nosample
libxtrace
config++
Catch2::Catch2
)
# Compile the test_usample
add_executable(test_usample test/test_usample.cpp)
target_include_directories(test_usample PRIVATE /usr/local/include/xtrace /usr/include)
target_link_libraries(test_usample
libxtrace
config++
Catch2::Catch2
)
# Compile the direct_test_usample
add_executable(direct_test_usample test/direct_test_usample.cpp)
target_include_directories(main PRIVATE /usr/local/include/xtrace)
target_link_libraries(main libxtrace Threads::Threads protobuf boost_system )
target_include_directories(direct_test_usample PRIVATE /usr/local/include/xtrace /usr/include)
target_link_libraries(direct_test_usample
libxtrace
config++
Catch2::Catch2
)
#ifndef _XTRACE_SAMPLER_
#define _XTRACE_SAMPLER_
#include <cstdlib>
#include <ctime>
#include <libconfig.h++>
namespace XTraceSampler {
namespace SamplingSetting {
bool sample;
double samplingRate;
void getDefaultSetting() {
libconfig::Config config;
try {
config.readFile("../resources/reference.cfg");
const libconfig::Setting &root = config.getRoot();
const libconfig::Setting &sampling = root["xtrace"]["sampling"];
sample = sampling.lookup("sample");
samplingRate = sampling.lookup("sampling_rate");
}
catch(const std::exception& e) {
std::cerr << e.what() << '\n';
}
}
bool getSample() {
return sample;
}
double getSamplingRate() {
return samplingRate;
}
}
class Sampler {
public:
Sampler() {}
~Sampler() {}
virtual bool sample() {
return true;
}
virtual void setParameter(double) {}
};
class UniformSampler: public Sampler {
private:
double p;
public:
UniformSampler() {
this -> p = SamplingSetting::getSamplingRate();
srand(static_cast<unsigned int>(clock()));
}
UniformSampler(double p, unsigned int seed) {
this -> p = p;
srand(seed);
}
~UniformSampler() {}
bool sample() {
if ((double)rand() / RAND_MAX < this -> p) {
return true;
}
else {
return false;
}
}
void setParameter(double p) {
this -> p = p;
}
};
int startedTracesNum = 0;
int samplingDecisionsNum = 0;
Sampler* sampler;
void createSampler() {
SamplingSetting::getDefaultSetting();
if (SamplingSetting::getSample()) {
sampler = new UniformSampler();
}
else {
sampler = new Sampler();
}
}
void initSampler() {
sampler = NULL;
startedTracesNum = 0;
samplingDecisionsNum = 0;
}
void incrStartedTraces() {
startedTracesNum++;
}
void incrSamplingDecisions() {
samplingDecisionsNum++;
}
bool sample() {
if (sampler == NULL) {
createSampler();
}
incrSamplingDecisions();
if (sampler -> sample()) {
incrStartedTraces();
return true;
}
else {
return false;
}
}
int getStartedTracesNumber() {
return startedTracesNum;
}
int getSamplingDecisionsNumber() {
return samplingDecisionsNum;
}
}
#endif
xtrace =
{
sampling = {
sample = true;
sampling_rate = 1.0;
};
};
\ No newline at end of file
......@@ -7,7 +7,7 @@
#include <chrono>
#include <time.h>
#include "xtrace_sampler.h"
#include "baggage.h"
#include "xtrace.h"
#include "xtrace.pb.h"
......@@ -121,11 +121,13 @@ void XTrace::Logger::log(std::string file, int line, std::string message, std::m
void XTrace::StartTrace() {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceBaggage::SetTaskID(make_event_id());
if (XTraceSampler::sample()) {
XTraceBaggage::SetTaskID(make_event_id());
XTraceReportv4 report = makeReport();
report.set_label("Start Trace");
sendReport(report);
XTraceReportv4 report = makeReport();
report.set_label("Start Trace");
sendReport(report);
}
}
void XTrace::StartTrace(std::string tag) {
......@@ -134,15 +136,17 @@ void XTrace::StartTrace(std::string tag) {
void XTrace::StartTrace(std::vector<std::string> tags) {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceBaggage::SetTaskID(make_event_id());
XTraceReportv4 report = makeReport();
report.set_agent("StartTrace");
report.set_label("Start Trace");
for (int i = 0; i < tags.size(); i++) {
report.add_tags(tags[i]);
if (XTraceSampler::sample()) {
XTraceBaggage::SetTaskID(make_event_id());
XTraceReportv4 report = makeReport();
report.set_agent("StartTrace");
report.set_label("Start Trace");
for (int i = 0; i < tags.size(); i++) {
report.add_tags(tags[i]);
}
sendReport(report);
}
sendReport(report);
}
bool XTrace::IsTracing() {
......
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include "xtrace_sampler.h"
#include "xtrace.h"
#include "pubsub.h"
#include "../src/xtrace_baggage.h"
bool hasParents() {
return XTraceBaggage::GetParentEventIDs().size() != 0;
}
TEST_CASE( "Directly test sample = true, p = 0.5", "[classic]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
double samplingRate = 0.48;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(0.5, 20);
for (int i = 0; i < tracesNumber; i++) {
XTraceSampler::sample();
}
int decisionsNum = XTraceSampler::getSamplingDecisionsNumber();
REQUIRE((double)XTraceSampler::getStartedTracesNumber() / decisionsNum == samplingRate);
REQUIRE(decisionsNum == tracesNumber);
}
TEST_CASE( "Check results for sample = true", "[classic]" ) {
XTraceSampler::initSampler();
int tracesNumber = 10;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(0.2, 20);
bool expectedDecisions[10] = {true, false, false, false, false, false, false, false, false, false};
for (int i = 0; i < tracesNumber; i++) {
REQUIRE(XTraceSampler::sample() == expectedDecisions[i]);
}
}
TEST_CASE( "Check parents for sample = true", "[classic]" ) {
XTraceSampler::initSampler();
int tracesNumber = 3;
bool expectedDecisions[3] = {false, true, false};
XTraceSampler::sampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTraceSampler::sampler -> setParameter(i % 2);
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(XTraceBaggage::HasTaskID() == expectedDecisions[i]);
REQUIRE(hasParents() == expectedDecisions[i]);
}
PubSub::shutdown();
PubSub::join();
}
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include "xtrace_sampler.h"
#include "xtrace.h"
#include "pubsub.h"
#include "../src/xtrace_baggage.h"
TEST_CASE( "Test sample = false", "[single-file]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
for (int i = 0; i < tracesNumber; i++) {
uint64_t oldTaskID = XTraceBaggage::GetTaskID();
XTrace::StartTrace("Testing trace");
REQUIRE(XTraceBaggage::HasTaskID());
REQUIRE(XTraceBaggage::GetTaskID() != oldTaskID);
XTrace::log("Ending trace");
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == tracesNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
PubSub::shutdown();
PubSub::join();
}
TEST_CASE( "Directly test sample = false", "[single-file]" ) {
XTraceSampler::initSampler();
int sampleCallsNumber = 1000;
XTraceSampler::sampler = new XTraceSampler::Sampler();
for (int i = 0; i < sampleCallsNumber; i++) {
REQUIRE(XTraceSampler::sample());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == sampleCallsNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == sampleCallsNumber);
}
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include "xtrace_sampler.h"
#include "xtrace.h"
#include "pubsub.h"
#include "../src/xtrace_baggage.h"
bool hasParents() {
return XTraceBaggage::GetParentEventIDs().size() != 0;
}
TEST_CASE( "Test sample = true, p = 0", "[single-file]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(0, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(!XTraceBaggage::HasTaskID());
REQUIRE(!hasParents());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == 0);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
PubSub::shutdown();
PubSub::join();
}
TEST_CASE( "Test sample = true, p = 1", "[single-file]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(XTraceBaggage::HasTaskID());
REQUIRE(hasParents());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == tracesNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
PubSub::shutdown();
PubSub::join();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment