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

Add sampling in logs and some tests

parent 683355d4
......@@ -4,19 +4,29 @@
namespace XTraceSampler {
namespace SamplingSetting {
extern bool sample;
extern double samplingRate;
extern bool taskSample;
extern double taskSamplingRate;
extern bool logSample;
extern double logSamplingRate;
void getDefaultSetting();
bool getSample();
double getSamplingRate();
bool getTaskSample();
double getTaskSamplingRate();
bool getLogSample();
double getLogSamplingRate();
}
class Sampler {
private:
int count;
public:
Sampler();
void incrCount();
int getCount();
virtual bool sample();
virtual double getParameter();
virtual void setParameter(double);
};
......@@ -26,23 +36,23 @@ namespace XTraceSampler {
double p;
public:
UniformSampler();
UniformSampler(double p, unsigned int seed);
bool sample();
void setParameter(double p);
};
extern int startedTracesNum;
enum SamplingMode {TASK, LOG};
extern int samplingDecisionsNum;
extern Sampler* sampler;
extern Sampler* taskSampler;
extern Sampler* logSampler;
void createSampler();
void createSampler(SamplingMode mode);
Sampler* getNewSampler(bool sample, double samplingRate);
void initSampler();
void incrStartedTraces();
void incrSamplingDecisions();
bool sample();
int getStartedTracesNumber();
Sampler** getSamplerPointer(SamplingMode mode);
bool sample(SamplingMode mode);
int getSamplingDecisionsNumber();
}
......
xtrace =
{
sampling = {
sample = true;
sampling_rate = 1.0;
task = {
sample = true;
sampling_rate = 1.0;
};
log = {
sample = true;
sampling_rate = 1.0;
};
};
};
\ No newline at end of file
......@@ -71,7 +71,12 @@ void sendReport(XTraceReportv4 &report) {
void XTrace::Logger::log(std::string message) {
if (!XTraceBaggage::HasTaskID()) {
return;
if (XTraceSampler::sample(XTraceSampler::LOG)) {
XTraceBaggage::SetTaskID(make_event_id());
}
else {
return;
}
}
XTraceReportv4 report = makeReport();
......@@ -82,7 +87,12 @@ void XTrace::Logger::log(std::string message) {
void XTrace::Logger::log(std::string file, int line, std::string message) {
if (!XTraceBaggage::HasTaskID()) {
return;
if (XTraceSampler::sample(XTraceSampler::LOG)) {
XTraceBaggage::SetTaskID(make_event_id());
}
else {
return;
}
}
XTraceReportv4 report = makeReport();
......@@ -121,7 +131,7 @@ 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
if (XTraceSampler::sample()) {
if (XTraceSampler::sample(XTraceSampler::TASK)) {
XTraceBaggage::SetTaskID(make_event_id());
XTraceReportv4 report = makeReport();
......@@ -136,7 +146,7 @@ void XTrace::StartTrace(std::string tag) {
void XTrace::StartTrace(std::vector<std::string> tags) {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
if (XTraceSampler::sample()) {
if (XTraceSampler::sample(XTraceSampler::TASK)) {
XTraceBaggage::SetTaskID(make_event_id());
XTraceReportv4 report = makeReport();
......
......@@ -4,8 +4,10 @@
#include <iostream>
#include "xtrace_sampler.h"
bool XTraceSampler::SamplingSetting::sample = true;
double XTraceSampler::SamplingSetting::samplingRate = 1.0;
bool XTraceSampler::SamplingSetting::taskSample = true;
double XTraceSampler::SamplingSetting::taskSamplingRate = 1.0;
bool XTraceSampler::SamplingSetting::logSample = true;
double XTraceSampler::SamplingSetting::logSamplingRate = 1.0;
void XTraceSampler::SamplingSetting::getDefaultSetting() {
libconfig::Config config;
......@@ -13,36 +15,56 @@ void XTraceSampler::SamplingSetting::getDefaultSetting() {
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");
taskSample = sampling["task"].lookup("sample");
taskSamplingRate = sampling["task"].lookup("sampling_rate");
logSample = sampling["log"].lookup("sample");
logSamplingRate = sampling["log"].lookup("sampling_rate");
}
catch(const std::exception& e) {
std::cerr << e.what() << '\n';
}
}
bool XTraceSampler::SamplingSetting::getSample() {
return sample;
bool XTraceSampler::SamplingSetting::getTaskSample() {
return taskSample;
}
double XTraceSampler::SamplingSetting::getSamplingRate() {
return samplingRate;
double XTraceSampler::SamplingSetting::getTaskSamplingRate() {
return taskSamplingRate;
}
XTraceSampler::Sampler::Sampler() {}
bool XTraceSampler::SamplingSetting::getLogSample() {
return logSample;
}
double XTraceSampler::SamplingSetting::getLogSamplingRate() {
return logSamplingRate;
}
XTraceSampler::Sampler::Sampler() {
this -> count = 0;
}
void XTraceSampler::Sampler::incrCount() {
this -> count ++;
}
int XTraceSampler::Sampler::getCount() {
return this -> count;
}
bool XTraceSampler::Sampler::sample() {
return true;
}
double XTraceSampler::Sampler::getParameter() {
return 1.0;
}
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) {
XTraceSampler::UniformSampler::UniformSampler(double p, unsigned int seed):Sampler() {
this -> p = p;
srand(seed);
}
......@@ -60,43 +82,65 @@ void XTraceSampler::UniformSampler::setParameter(double p) {
this -> p = p;
}
int XTraceSampler::startedTracesNum = 0;
int XTraceSampler::samplingDecisionsNum = 0;
XTraceSampler::Sampler* XTraceSampler::sampler = NULL;
XTraceSampler::Sampler* XTraceSampler::taskSampler = NULL;
XTraceSampler::Sampler* XTraceSampler::logSampler = NULL;
void XTraceSampler::createSampler() {
initSampler();
void XTraceSampler::createSampler(SamplingMode mode) {
SamplingSetting::getDefaultSetting();
if (SamplingSetting::getSample()) {
sampler = new UniformSampler();
switch (mode)
{
case XTraceSampler::TASK:
taskSampler = getNewSampler(SamplingSetting::getTaskSample(), SamplingSetting::getTaskSamplingRate());
break;
case XTraceSampler::LOG:
logSampler = getNewSampler(SamplingSetting::getLogSample(), SamplingSetting::getLogSamplingRate());
break;
}
}
XTraceSampler::Sampler* XTraceSampler::getNewSampler(bool sample, double samplingRate) {
if (sample) {
return new UniformSampler(samplingRate, static_cast<unsigned int>(clock()));
}
else {
sampler = new Sampler();
return new Sampler();
}
}
void XTraceSampler::initSampler() {
sampler = NULL;
startedTracesNum = 0;
taskSampler = NULL;
logSampler = NULL;
samplingDecisionsNum = 0;
}
void XTraceSampler::incrStartedTraces() {
startedTracesNum++;
}
void XTraceSampler::incrSamplingDecisions() {
samplingDecisionsNum++;
}
bool XTraceSampler::sample() {
if (sampler == NULL) {
createSampler();
}
XTraceSampler::Sampler** XTraceSampler::getSamplerPointer(SamplingMode mode) {
switch (mode)
{
case XTraceSampler::TASK:
return &taskSampler;
case XTraceSampler::LOG:
return &logSampler;
}
}
bool XTraceSampler::sample(SamplingMode mode) {
Sampler** sampler = getSamplerPointer(mode);
if ((*sampler) == NULL) {
createSampler(mode);
}
incrSamplingDecisions();
if (sampler -> sample()) {
incrStartedTraces();
if ((*sampler) -> sample()) {
(*sampler) -> incrCount();
return true;
}
else {
......@@ -104,10 +148,6 @@ bool XTraceSampler::sample() {
}
}
int XTraceSampler::getStartedTracesNumber() {
return startedTracesNum;
}
int XTraceSampler::getSamplingDecisionsNumber() {
return samplingDecisionsNum;
}
\ No newline at end of file
......@@ -5,17 +5,31 @@
#include "pubsub.h"
#include "xtrace_baggage.h"
TEST_CASE( "Directly test sample = false", "[no-sample]" ) {
TEST_CASE( "Directly test sample tasks = false", "[no-sample]" ) {
XTraceSampler::initSampler();
int sampleCallsNumber = 1000;
XTraceSampler::sampler = new XTraceSampler::Sampler();
XTraceSampler::taskSampler = new XTraceSampler::Sampler();
for (int i = 0; i < sampleCallsNumber; i++) {
REQUIRE(XTraceSampler::sample());
REQUIRE(XTraceSampler::sample(XTraceSampler::TASK));
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == sampleCallsNumber);
REQUIRE(XTraceSampler::taskSampler -> getCount() == sampleCallsNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == sampleCallsNumber);
}
TEST_CASE( "Directly test sample logs = false", "[no-sample]" ) {
XTraceSampler::initSampler();
int sampleCallsNumber = 1000;
XTraceSampler::logSampler = new XTraceSampler::Sampler();
for (int i = 0; i < sampleCallsNumber; i++) {
REQUIRE(XTraceSampler::sample(XTraceSampler::LOG));
}
REQUIRE(XTraceSampler::logSampler -> getCount() == sampleCallsNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == sampleCallsNumber);
}
......@@ -24,14 +38,14 @@ TEST_CASE( "Directly test sample = true, p = 0.5", "[sample]" ) {
int tracesNumber = 1000;
double samplingRate = 0.48;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(0.5, 20);
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(0.5, 20);
for (int i = 0; i < tracesNumber; i++) {
XTraceSampler::sample();
XTraceSampler::sample(XTraceSampler::TASK);
}
int decisionsNum = XTraceSampler::getSamplingDecisionsNumber();
REQUIRE((double)XTraceSampler::getStartedTracesNumber() / decisionsNum == samplingRate);
REQUIRE((double)XTraceSampler::taskSampler -> getCount() / decisionsNum == samplingRate);
REQUIRE(decisionsNum == tracesNumber);
}
......@@ -39,11 +53,31 @@ TEST_CASE( "Check results for sample = true", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 10;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(0.2, 20);
XTraceSampler::taskSampler = 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]);
REQUIRE(XTraceSampler::sample(XTraceSampler::TASK) == expectedDecisions[i]);
}
}
TEST_CASE( "Check results for sampling modes", "[sample]" ) {
XTraceSampler::initSampler();
int tracesNumber = 5;
int logsNumber = 5;
double pTask = 0.6;
double pLog = 0.8;
bool expectedTasksDecisions[] = { true, false, true, true, false };
bool expectedLogsDecisions[] = { true, true, false, true, true };
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(pTask, 20);
XTraceSampler::logSampler = new XTraceSampler::UniformSampler(pLog, 30);
for (int i = 0; i < tracesNumber; i++) {
REQUIRE(XTraceSampler::sample(XTraceSampler::TASK) == expectedTasksDecisions[i]);
}
for (int i = 0; i < logsNumber; i++) {
REQUIRE(XTraceSampler::sample(XTraceSampler::LOG) == expectedLogsDecisions[i]);
}
}
......@@ -6,6 +6,7 @@
#include "xtrace_baggage.h"
TEST_CASE( "Test sample = false", "[no-sample]" ) {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceSampler::initSampler();
int tracesNumber = 1000;
......@@ -18,32 +19,33 @@ TEST_CASE( "Test sample = false", "[no-sample]" ) {
XTrace::log("Ending trace");
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == tracesNumber);
REQUIRE(XTraceSampler::taskSampler -> getCount() == tracesNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
}
TEST_CASE( "Test sample = true, p = 0", "[sample]" ) {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceSampler::initSampler();
int tracesNumber = 1000;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(0, static_cast<unsigned int>(clock()));
XTraceSampler::taskSampler = 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(!XTraceBaggage::HasParentEventIDs());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == 0);
REQUIRE(XTraceSampler::taskSampler -> getCount() == 0);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
}
TEST_CASE( "Test sample = true, p = 1", "[sample]" ) {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceSampler::initSampler();
int tracesNumber = 1000;
XTraceSampler::sampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Testing trace");
......@@ -52,21 +54,21 @@ TEST_CASE( "Test sample = true, p = 1", "[sample]" ) {
REQUIRE(XTraceBaggage::HasParentEventIDs());
}
REQUIRE(XTraceSampler::getStartedTracesNumber() == tracesNumber);
REQUIRE(XTraceSampler::taskSampler -> getCount() == tracesNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
}
TEST_CASE( "Check has parents for sample = true", "[sample]" ) {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceSampler::initSampler();
int tracesNumber = 2;
bool expectedDecisions[2] = {true, false};
XTraceSampler::sampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTraceSampler::sampler -> setParameter(1 - i);
XTraceSampler::taskSampler -> setParameter(1 - i);
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(XTraceBaggage::HasTaskID() == expectedDecisions[i]);
REQUIRE(XTraceBaggage::HasParentEventIDs() == expectedDecisions[i]);
......@@ -74,21 +76,214 @@ TEST_CASE( "Check has parents for sample = true", "[sample]" ) {
}
TEST_CASE( "Check no parents for sample = true", "[sample]" ) {
XTraceBaggage::Clear(); // Clear old X-Trace metadata if it exists
XTraceSampler::initSampler();
int tracesNumber = 2;
bool expectedDecisions[2] = {false, true};
XTraceSampler::sampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(1, static_cast<unsigned int>(clock()));
for (int i = 0; i < tracesNumber; i++) {
XTraceSampler::sampler -> setParameter(i);
XTraceSampler::taskSampler -> setParameter(i);
XTrace::StartTrace("Testing trace");
XTrace::log("Ending trace");
REQUIRE(XTraceBaggage::HasTaskID() == expectedDecisions[i]);
REQUIRE(XTraceBaggage::HasParentEventIDs() == expectedDecisions[i]);
}
}
TEST_CASE("No task sampling, log sampling, p = 0.5", "[sample]") {
XTraceBaggage::Clear();
XTraceSampler::initSampler();
int tracesNumber = 100;
int logsNumber = 20;
double p = 0.5;
XTraceSampler::taskSampler = new XTraceSampler::Sampler();
XTraceSampler::logSampler = new XTraceSampler::UniformSampler(p, 20);
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Test trace");
REQUIRE(XTraceBaggage::HasTaskID());
for (int j = 0; j < logsNumber; j++) {
XTrace::log("Log No. " + j);
REQUIRE(XTraceBaggage::HasParentEventIDs());
}
XTraceBaggage::Clear();
}
REQUIRE(XTraceSampler::taskSampler -> getCount() == tracesNumber);
REQUIRE(XTraceSampler::logSampler -> getCount() == 0);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
}
TEST_CASE("Task sampling, no log sampling, p = 1", "[sample]") {
XTraceBaggage::Clear();
XTraceSampler::initSampler();
int tracesNumber = 100;
int logsNumber = 20;
double p = 1.0;
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(p, 20);
XTraceSampler::logSampler = new XTraceSampler::Sampler();
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Test trace");
REQUIRE(XTraceBaggage::HasTaskID());
for (int j = 0; j < logsNumber; j++) {
XTrace::log("Log No. " + j);
REQUIRE(XTraceBaggage::HasParentEventIDs());
}
XTraceBaggage::Clear();
}
REQUIRE(XTraceSampler::taskSampler -> getCount() == tracesNumber);
REQUIRE(XTraceSampler::logSampler -> getCount() == 0);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber);
}
TEST_CASE("Task sampling, no log sampling, p = 0", "[sample]") {
XTraceBaggage::Clear();
XTraceSampler::initSampler();
int tracesNumber = 100;
int logsNumber = 20;
double p = 0.0;
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(p, 20);
XTraceSampler::logSampler = new XTraceSampler::Sampler();
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Test trace");
REQUIRE(!XTraceBaggage::HasTaskID());
for (int j = 0; j < logsNumber; j++) {
XTrace::log("Log No. " + j);
REQUIRE(XTraceBaggage::HasTaskID());
REQUIRE(XTraceBaggage::HasParentEventIDs());
}
XTraceBaggage::Clear();
}
REQUIRE(XTraceSampler::taskSampler -> getCount() == 0);
REQUIRE(XTraceSampler::logSampler -> getCount() == tracesNumber);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber * 2);
}
TEST_CASE("Task sampling, no log sampling, p = 0.5", "[sample]") {
XTraceBaggage::Clear();
XTraceSampler::initSampler();
int tracesNumber = 100;
int logsNumber = 20;
double p = 0.5;
int expectedSampledTasks = 55;
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(p, 20);
XTraceSampler::logSampler = new XTraceSampler::Sampler();
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Test trace");
for (int j = 0; j < logsNumber; j++) {
XTrace::log("Log No. " + j);
REQUIRE(XTraceBaggage::HasTaskID());
REQUIRE(XTraceBaggage::HasParentEventIDs());
}
XTraceBaggage::Clear();
}
REQUIRE(XTraceSampler::taskSampler -> getCount() + XTraceSampler::logSampler -> getCount() == tracesNumber);
REQUIRE(XTraceSampler::taskSampler -> getCount() == expectedSampledTasks);
REQUIRE(XTraceSampler::getSamplingDecisionsNumber() == tracesNumber * 2 - expectedSampledTasks);
}
TEST_CASE("Task sampling, log sampling, pTask = 0.5, pLog = 0", "[sample]") {
XTraceBaggage::Clear();
XTraceSampler::initSampler();
int tracesNumber = 100;
int logsNumber = 20;
double pTask = 0.5;
double pLog = 0.0;
int expectedSampledTasks = 46;
XTraceSampler::taskSampler = new XTraceSampler::UniformSampler(pTask, 20);
XTraceSampler::logSampler = new XTraceSampler::UniformSampler(pLog, 20);
for (int i = 0; i < tracesNumber; i++) {
XTrace::StartTrace("Test trace");
bool isSampled = XTraceBaggage::HasTaskID();