<commit_before>#include "test_common.hh"

#include <dune/stuff/common/profiler.hh>
#include <dune/stuff/common/math.hh>
#include <dune/stuff/common/ranges.hh>

using namespace Dune::Stuff::Common;
const int wait_ms = 142;

void scoped_busywait(const std::string& name, int ms) {
  Profiler::ScopedTiming scopedTiming(name);
  busywait(ms);
}

TEST(ProfilerTest, Timing) {
  for ( auto i : valueRange(1,4)) {
    DSC_PROFILER.startTiming("ProfilerTest.Timing");
    busywait(wait_ms);
    DSC_PROFILER.stopTiming("ProfilerTest.Timing");
    EXPECT_GE(DSC_PROFILER.getTiming("ProfilerTest.Timing"), i * wait_ms);
  }
}

TEST(ProfilerTest, ScopedTiming) {
  const auto dvalueRange = valueRange(1,4);
  for ( auto DUNE_UNUSED(i) : dvalueRange) {
    scoped_busywait("ProfilerTest.ScopedTiming", wait_ms);
  }
  EXPECT_GE(DSC_PROFILER.getTiming("ProfilerTest.ScopedTiming"), long(dvalueRange.size() * wait_ms));
}

TEST(ProfilerTest, MultiRuns) {
  const auto dvalueRange = valueRange(1,3);
  //needs to be dvalueRange.size() + 1 since we're calling nextRun() dvalueRange.size() times
  DSC_PROFILER.reset(dvalueRange.size()+1);
  for ( auto i : dvalueRange) {
    scoped_busywait("ProfilerTest.MultiRuns", i * wait_ms);
    DSC_PROFILER.nextRun();
  }
  const double confidence_margin = 0.95f;
  for ( auto i : dvalueRange) {
    //i-1 cause runs have 0-based index
    EXPECT_GE(DSC_PROFILER.getTimingIdx("ProfilerTest.MultiRuns", i-1), i * wait_ms * confidence_margin);
  }
}

TEST(ProfilerTest, OutputConstness) {
  DSC_PROFILER.reset(1);
  const auto& prof = DSC_PROFILER;
  prof.outputAveraged(0,0);
  prof.outputTimings("timings");
  prof.outputTimings(DSC::dev_null);
}

TEST(ProfilerTest, ExpectedFailures) {
  EXPECT_THROW(DSC_PROFILER.reset(0),Dune::RangeError);
  EXPECT_THROW(DSC_PROFILER.reset(-1),Dune::RangeError);
  EXPECT_THROW(DSC_PROFILER.stopTiming("This_section_was_never_start"),Dune::RangeError);
}<commit_msg>[tests] allow 10% error on busywait test<commit_after>#include "test_common.hh"

#include <dune/stuff/common/profiler.hh>
#include <dune/stuff/common/math.hh>
#include <dune/stuff/common/ranges.hh>

using namespace Dune::Stuff::Common;
const int wait_ms = 142;

void scoped_busywait(const std::string& name, int ms) {
  Profiler::ScopedTiming scopedTiming(name);
  busywait(ms);
}

static double confidence_margin() { return 0.90f; }

TEST(ProfilerTest, Timing) {
  for ( auto i : valueRange(1,4)) {
    DSC_PROFILER.startTiming("ProfilerTest.Timing");
    busywait(wait_ms);
    DSC_PROFILER.stopTiming("ProfilerTest.Timing");
    EXPECT_GE(DSC_PROFILER.getTiming("ProfilerTest.Timing"), i * wait_ms * confidence_margin());
  }
}

TEST(ProfilerTest, ScopedTiming) {
  const auto dvalueRange = valueRange(1,4);
  for ( auto DUNE_UNUSED(i) : dvalueRange) {
    scoped_busywait("ProfilerTest.ScopedTiming", wait_ms);
  }
  EXPECT_GE(DSC_PROFILER.getTiming("ProfilerTest.ScopedTiming"), long(dvalueRange.size() * wait_ms));
}

TEST(ProfilerTest, MultiRuns) {
  const auto dvalueRange = valueRange(1,3);
  //needs to be dvalueRange.size() + 1 since we're calling nextRun() dvalueRange.size() times
  DSC_PROFILER.reset(dvalueRange.size()+1);
  for ( auto i : dvalueRange) {
    scoped_busywait("ProfilerTest.MultiRuns", i * wait_ms);
    DSC_PROFILER.nextRun();
  }

  for ( auto i : dvalueRange) {
    //i-1 cause runs have 0-based index
    EXPECT_GE(DSC_PROFILER.getTimingIdx("ProfilerTest.MultiRuns", i-1), i * wait_ms * confidence_margin());
  }
}

TEST(ProfilerTest, OutputConstness) {
  DSC_PROFILER.reset(1);
  const auto& prof = DSC_PROFILER;
  prof.outputAveraged(0,0);
  prof.outputTimings("timings");
  prof.outputTimings(DSC::dev_null);
}

TEST(ProfilerTest, ExpectedFailures) {
  EXPECT_THROW(DSC_PROFILER.reset(0),Dune::RangeError);
  EXPECT_THROW(DSC_PROFILER.reset(-1),Dune::RangeError);
  EXPECT_THROW(DSC_PROFILER.stopTiming("This_section_was_never_started"),Dune::RangeError);
}
{'fraction_non_alphanumeric': 0.13994392046902882, 'fraction_numerical': 0.011725720112159062, 'mean_word_length': 5.6848381601362865, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4606475', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1575, 'n_words': 230}