Commit 683355d4 authored by Reyhaneh Karimipour's avatar Reyhaneh Karimipour
Browse files

Add xtrace_sampler.cpp and modify tests

parent f09e050b
build
.DS_Store
\ No newline at end of file
.DS_Store
.vscode/
\ No newline at end of file
......@@ -2,7 +2,10 @@ cmake_minimum_required(VERSION 3.4.1)
project(libxtrace)
include_directories( include )
include_directories(
include
src
)
link_directories(
/usr/lib/x86_64-linux-gnu
......@@ -27,6 +30,7 @@ add_library (libxtrace SHARED
src/xtrace.cpp
src/xtrace_baggage.cpp
src/lexvarint.cpp
src/xtrace_sampler.cpp
${PROTO_SRCS}
)
set_target_properties(libxtrace
......@@ -79,31 +83,17 @@ target_link_libraries(main libxtrace Threads::Threads protobuf boost_system conf
find_package(Catch2 REQUIRED)
# Compile the test_nosample
add_executable(test_nosample test/test_nosample.cpp)
include_directories(test)
target_include_directories(test_nosample PRIVATE /usr/local/include/xtrace /usr/include)
target_link_libraries(test_nosample
libxtrace
config++
Catch2::Catch2
# Compile the tests
add_executable(tests
test/tests.cpp
test/xtrace-cpp/test/test_sampler.cpp
test/xtrace-cpp/test/direct_test_sampler.cpp
)
# 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(direct_test_usample PRIVATE /usr/local/include/xtrace /usr/include)
target_link_libraries(direct_test_usample
target_include_directories(tests PRIVATE /usr/local/include/xtrace /usr/include)
target_link_libraries(tests
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;
}
extern bool sample;
extern double samplingRate;
double getSamplingRate() {
return samplingRate;
}
void getDefaultSetting();
bool getSample();
double getSamplingRate();
}
class Sampler {
public:
Sampler() {}
~Sampler() {}
virtual bool sample() {
return true;
}
virtual void setParameter(double) {}
Sampler();
virtual bool sample();
virtual void setParameter(double);
};
......@@ -52,83 +26,24 @@ namespace XTraceSampler {
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;
}
UniformSampler();
UniformSampler(double p, unsigned int seed);
bool sample();
void setParameter(double 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();
}
extern int startedTracesNum;
extern int samplingDecisionsNum;
extern Sampler* sampler;
incrSamplingDecisions();
if (sampler -> sample()) {
incrStartedTraces();
return true;
}
else {
return false;
}
}
int getStartedTracesNumber() {
return startedTracesNum;
}
int getSamplingDecisionsNumber() {
return samplingDecisionsNum;
}
void createSampler();
void initSampler();
void incrStartedTraces();
void incrSamplingDecisions();
bool sample();
int getStartedTracesNumber();
int getSamplingDecisionsNumber();
}
#endif
......@@ -96,3 +96,6 @@ void XTraceBaggage::SetParentEventID(uint64_t parentEventId) {
ThreadLocalBaggage::Set(b);
}
bool XTraceBaggage::HasParentEventIDs() {
return GetParentEventIDs().size() != 0;
}
......@@ -13,6 +13,7 @@ void SetTaskID(uint64_t taskId);
std::vector<uint64_t> GetParentEventIDs();
void SetParentEventID(uint64_t eventId);
bool HasParentEventIDs();
}
......
#include <cstdlib>
#include <ctime>
#include <libconfig.h++>
#include <iostream>
#include "xtrace_sampler.h"
bool XTraceSampler::SamplingSetting::sample = true;
double XTraceSampler::SamplingSetting::samplingRate = 1.0;
void XTraceSampler::SamplingSetting::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 XTraceSampler::SamplingSetting::getSample() {
return sample;
}
double XTraceSampler::SamplingSetting::getSamplingRate() {
return samplingRate;
}
XTraceSampler::Sampler::Sampler() {}
bool XTraceSampler::Sampler::sample() {
return true;
}
void XTraceSampler::Sampler::setParameter(double p) {}
XTraceSampler::UniformSampler::UniformSampler() {
this -> p = SamplingSetting::getSamplingRate();
srand(static_cast<unsigned int>(clock()));
}
XTraceSampler::UniformSampler::UniformSampler(double p, unsigned int seed) {
this -> p = p;
srand(seed);
}
bool XTraceSampler::UniformSampler::sample() {
if ((double)rand() / RAND_MAX < this -> p) {
return true;
}
else {
return false;
}
}
void XTraceSampler::UniformSampler::setParameter(double p) {
this -> p = p;
}
int XTraceSampler::startedTracesNum = 0;
int XTraceSampler::samplingDecisionsNum = 0;
XTraceSampler::Sampler* XTraceSampler::sampler = NULL;
void XTraceSampler::createSampler() {
initSampler();
SamplingSetting::getDefaultSetting();
if (SamplingSetting::getSample()) {
sampler = new UniformSampler();
}
else {
sampler = new Sampler();
}
}
void XTraceSampler::initSampler() {
sampler = NULL;
startedTracesNum = 0;
samplingDecisionsNum = 0;
}
void XTraceSampler::incrStartedTraces() {
startedTracesNum++;
}
void XTraceSampler::incrSamplingDecisions() {
samplingDecisionsNum++;
}
bool XTraceSampler::sample() {
if (sampler == NULL) {
createSampler();
}
incrSamplingDecisions();
if (sampler -> sample()) {
incrStartedTraces();
return true;
}
else {
return false;
}
}
int XTraceSampler::getStartedTracesNumber() {
return startedTracesNum;
}
int XTraceSampler::getSamplingDecisionsNumber() {
return samplingDecisionsNum;
}
\ No newline at end of file
#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"
TEST_CASE("All test cases reside in other .cpp files.", "[multi-file]") {}
\ No newline at end of file
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include "xtrace_sampler.h"
#include "xtrace.h"
#include "pubsub.h"
#include "../src/xtrace_baggage.h"
#include "xtrace_baggage.h"
TEST_CASE( "Directly test sample = false", "[no-sample]" ) {
XTraceSampler::initSampler();
int sampleCallsNumber = 1000;
XTraceSampler::sampler = new XTraceSampler::Sampler();
bool hasParents() {
return XTraceBaggage::GetParentEventIDs().size() != 0;
for (int i = 0; i < sampleCallsNumber; i++) {
REQUIRE(XTraceSampler::sample());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == sampleCallsNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == sampleCallsNumber);
}
TEST_CASE( "Directly test sample = true, p = 0.5", "[classic]" ) {
TEST_CASE( "Directly test sample = true, p = 0.5", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
......@@ -27,7 +35,7 @@ TEST_CASE( "Directly test sample = true, p = 0.5", "[classic]" ) {
REQUIRE(decisionsNum == tracesNumber);
}
TEST_CASE( "Check results for sample = true", "[classic]" ) {
TEST_CASE( "Check results for sample = true", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 10;
......@@ -39,22 +47,3 @@ TEST_CASE( "Check results for sample = true", "[classic]" ) {
}
}
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"
#include "xtrace_baggage.h"
TEST_CASE( "Test sample = false", "[no-sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
bool hasParents() {
return XTraceBaggage::GetParentEventIDs().size() != 0;
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);
}
TEST_CASE( "Test sample = true, p = 0", "[single-file]" ) {
TEST_CASE( "Test sample = true, p = 0", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
......@@ -21,17 +32,14 @@ TEST_CASE( "Test sample = true, p = 0", "[single-file]" ) {
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(!XTraceBaggage::HasTaskID());
REQUIRE(!hasParents());
REQUIRE(!XTraceBaggage::HasParentEventIDs());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == 0);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
PubSub::shutdown();
PubSub::join();
}
TEST_CASE( "Test sample = true, p = 1", "[single-file]" ) {
TEST_CASE( "Test sample = true, p = 1", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 1000;
......@@ -41,11 +49,45 @@ TEST_CASE( "Test sample = true, p = 1", "[single-file]" ) {
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(XTraceBaggage::HasTaskID());
REQUIRE(hasParents());
REQUIRE(XTraceBaggage::HasParentEventIDs());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == tracesNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
}
TEST_CASE( "Check has parents for sample = true", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 2;
bool expectedDecisions[2] = {true, false};
XTraceSampler::sampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTraceSampler::sampler -> setParameter(1 - i);
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(XTraceBaggage::HasTaskID() == expectedDecisions[i]);
REQUIRE(XTraceBaggage::HasParentEventIDs() == expectedDecisions[i]);
}
}
TEST_CASE( "Check no parents for sample = true", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 2;
bool expectedDecisions[2] = {false, true};
XTraceSampler::sampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTraceSampler::sampler -> setParameter(i);
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(XTraceBaggage::HasTaskID() == expectedDecisions[i]);
REQUIRE(XTraceBaggage::HasParentEventIDs() == expectedDecisions[i]);
}
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