
https://svn.lrde.epita.fr/svn/ranch/trunk Index: ChangeLog from Nicolas Despr�s <nicolas.despres@gmail.com> Add scores after stop. * lib/cxx/test/bencher-test.htt: Test it. * lib/cxx/src/ranch-cxx/outputs.hh, * lib/cxx/src/ranch-cxx/outputs.hxx: Add size accessor. * lib/cxx/src/ranch-cxx/output/output.hxx, * lib/cxx/src/ranch-cxx/output/output.hh, * lib/cxx/src/ranch-cxx/output/output.cc: Ensure that multiple call to start/stop perform the real start/stop only once. Use start_/stop_ internally. * lib/cxx/src/ranch-cxx/output/u-time.cc, * lib/cxx/src/ranch-cxx/output/u-time.hh: Overload start_/stop_. * lib/cxx/src/ranch-cxx/bencher.cc, * lib/cxx/src/ranch-cxx/bencher.hh: Add the add_score methods that allow to add the result once the benchmark is stopped. src/ranch-cxx/bencher.cc | 43 ++++++++++++++++++++++++++++++- src/ranch-cxx/bencher.hh | 14 ++++++++++ src/ranch-cxx/output/output.cc | 4 +- src/ranch-cxx/output/output.hh | 10 +++++-- src/ranch-cxx/output/output.hxx | 21 ++++++++++++++- src/ranch-cxx/output/u-time.cc | 4 +- src/ranch-cxx/output/u-time.hh | 7 ++--- src/ranch-cxx/outputs.hh | 4 ++ src/ranch-cxx/outputs.hxx | 5 +++ test/bencher-test.htt | 54 ++++++++++++++++++++++++++++++++++++++++ 10 files changed, 153 insertions(+), 13 deletions(-) Index: lib/cxx/test/bencher-test.htt --- lib/cxx/test/bencher-test.htt (revision 33) +++ lib/cxx/test/bencher-test.htt (working copy) @@ -81,6 +81,60 @@ TS_ASSERT_EQUALS(oss.str(), out); } + void test_one_add_score_after_stop_with_specified_output() + { + std::ostringstream oss; + Ranch::Dumper::Yaml dumper(oss); + Ranch::Bencher::dumper(dumper); + + Ranch::Input::Input foo("foo", "unit_foo"); + Ranch::Input::Input bar("bar", "unit_bar"); + + Ranch::Output::Output toto("toto", "unit_toto"); + Ranch::Output::Output tata("tata", "unit_tata"); + + Ranch::Bencher b("test1", "a comment"); + b.add_input(foo); + b.add_input(bar); + b.add_output(toto).add_output(tata); + + b.start(1.0, 2.0); + std::string out = OUTPUT_CTOR_1; + out += OUTPUT_START_1; + TS_ASSERT_EQUALS(oss.str(), out); + b.stop(); + b.add_score(tata, 22.0).add_score(toto, 11.0); + out += OUTPUT_STOP_1; + TS_ASSERT_EQUALS(oss.str(), out); + } + + void test_one_add_score_after_stop_without_specified_output() + { + std::ostringstream oss; + Ranch::Dumper::Yaml dumper(oss); + Ranch::Bencher::dumper(dumper); + + Ranch::Input::Input foo("foo", "unit_foo"); + Ranch::Input::Input bar("bar", "unit_bar"); + + Ranch::Output::Output toto("toto", "unit_toto"); + Ranch::Output::Output tata("tata", "unit_tata"); + + Ranch::Bencher b("test1", "a comment"); + b.add_input(foo); + b.add_input(bar); + b.add_output(toto).add_output(tata); + + b.start(1.0, 2.0); + std::string out = OUTPUT_CTOR_1; + out += OUTPUT_START_1; + TS_ASSERT_EQUALS(oss.str(), out); + b.stop(); + b.add_score(11.0).add_score(22.0); + out += OUTPUT_STOP_1; + TS_ASSERT_EQUALS(oss.str(), out); + } + void test_several() { std::ostringstream oss; Index: lib/cxx/src/ranch-cxx/outputs.hh --- lib/cxx/src/ranch-cxx/outputs.hh (revision 33) +++ lib/cxx/src/ranch-cxx/outputs.hh (working copy) @@ -25,6 +25,10 @@ iterator begin(); iterator end(); + typedef list_type::size_type size_type; + + size_type size() const; + protected: list_type list_; }; // class Outputs Index: lib/cxx/src/ranch-cxx/outputs.hxx --- lib/cxx/src/ranch-cxx/outputs.hxx (revision 33) +++ lib/cxx/src/ranch-cxx/outputs.hxx (working copy) @@ -33,6 +33,11 @@ return list_.end(); } + inline Outputs::size_type Outputs::size() const + { + return list_.size(); + } + } // namespace Ranch #endif // !RANCH_OUTPUTS_HXX Index: lib/cxx/src/ranch-cxx/bencher.cc --- lib/cxx/src/ranch-cxx/bencher.cc (revision 33) +++ lib/cxx/src/ranch-cxx/bencher.cc (working copy) @@ -28,7 +28,9 @@ inputs_(inputs), outputs_(outputs), comment_(comment), - first_start_call_(true) + first_start_call_(true), + score_set_num_(0), + cur_output_(NULL) { } @@ -37,7 +39,9 @@ inputs_(), outputs_(), comment_(comment), - first_start_call_(true) + first_start_call_(true), + score_set_num_(0), + cur_output_(NULL) { } @@ -73,6 +77,8 @@ { va_list ap; + stop_outputs_(); + Outputs::iterator i = outputs_.begin(); (*i)->value(score_output_1); @@ -83,6 +89,29 @@ dumper_->end_score(*this); } + void Bencher::stop() + { + stop_outputs_(); + } + + Bencher& Bencher::add_score(Output::Output& output, double score) + { + output.value(score); + ++score_set_num_; + if (score_set_num_ >= outputs_.size()) + dumper_->end_score(*this); + return *this; + } + + Bencher& Bencher::add_score(double score) + { + (*cur_output_)->value(score); + ++cur_output_; + if (cur_output_ == outputs_.end()) + dumper_->end_score(*this); + return *this; + } + void Bencher::start_outputs_() { for (Outputs::iterator i = outputs_.begin(); @@ -91,8 +120,18 @@ (*i)->start(); } + void Bencher::stop_outputs_() + { + for (Outputs::iterator i = outputs_.begin(); + i != outputs_.end(); + ++i) + (*i)->stop(); + } + void Bencher::begin_bench_() { + score_set_num_ = 0; + cur_output_ = outputs_.begin(); if (first_start_call_) { first_start_call_ = false; Index: lib/cxx/src/ranch-cxx/output/output.cc --- lib/cxx/src/ranch-cxx/output/output.cc (revision 33) +++ lib/cxx/src/ranch-cxx/output/output.cc (working copy) @@ -9,11 +9,11 @@ { } - void Output::start() + void Output::start_() { } - void Output::stop() + void Output::stop_() { } Index: lib/cxx/src/ranch-cxx/output/u-time.cc --- lib/cxx/src/ranch-cxx/output/u-time.cc (revision 33) +++ lib/cxx/src/ranch-cxx/output/u-time.cc (working copy) @@ -9,12 +9,12 @@ namespace Output { - void UTime::start() + void UTime::start_() { start_time_ = utime_(); } - void UTime::stop() + void UTime::stop_() { value_ = (utime_() - start_time_) * sysconf(_SC_CLK_TCK) * 1.0e-4; } Index: lib/cxx/src/ranch-cxx/output/output.hxx --- lib/cxx/src/ranch-cxx/output/output.hxx (revision 33) +++ lib/cxx/src/ranch-cxx/output/output.hxx (working copy) @@ -11,7 +11,8 @@ { inline Output::Output(const std::string& name, const std::string& unit) : - IO(name, unit) + IO(name, unit), + started_(false) { } @@ -21,6 +22,24 @@ return value_; } + inline void Output::start() + { + if (!started_) + { + start_(); + started_ = true; + } + } + + inline void Output::stop() + { + if (started_) + { + stop_(); + started_ = false; + } + } + } // namespace Output } // namespace Ranch Index: lib/cxx/src/ranch-cxx/output/output.hh --- lib/cxx/src/ranch-cxx/output/output.hh (revision 33) +++ lib/cxx/src/ranch-cxx/output/output.hh (working copy) @@ -18,11 +18,17 @@ Output(const std::string& name, const std::string& unit = ""); virtual ~Output(); - virtual void start(); - virtual void stop(); + void start(); + void stop(); double operator() (); + protected: + virtual void start_(); + virtual void stop_(); + + bool started_; + }; // class Output } // namespace Output Index: lib/cxx/src/ranch-cxx/output/u-time.hh --- lib/cxx/src/ranch-cxx/output/u-time.hh (revision 33) +++ lib/cxx/src/ranch-cxx/output/u-time.hh (working copy) @@ -15,13 +15,12 @@ public: UTime(); - virtual void start(); - virtual void stop(); - protected: + virtual void start_(); + virtual void stop_(); + double utime_(); - protected: double start_time_; }; // class UTime Index: lib/cxx/src/ranch-cxx/bencher.hh --- lib/cxx/src/ranch-cxx/bencher.hh (revision 33) +++ lib/cxx/src/ranch-cxx/bencher.hh (working copy) @@ -37,10 +37,18 @@ void start(double size_input_1, ...); // There is always at least one output. void stop(double score_output_1, ...); + void stop(); Bencher& add_input(Input::Input& input); Bencher& add_output(Output::Output& output); + // Add a score according to the specified output. Do not mix with + // add_score(double). + Bencher& add_score(Output::Output& output, double score); + // Add a score in order of declaration. Do not mix with + // add_score(Output::Output&, double). + Bencher& add_score(double score); + const std::string& name() const; const Inputs& inputs() const; const Outputs& outputs() const; @@ -48,6 +56,7 @@ protected: void start_outputs_(); + void stop_outputs_(); void begin_bench_(); protected: @@ -55,7 +64,12 @@ Inputs inputs_; Outputs outputs_; std::string comment_; + // Necessary since we may use add_input/add_output. bool first_start_call_; + // Necessary to dump the score once every excepted score are registered. + unsigned score_set_num_; + // Necessary to manage add_score(double). + Outputs::iterator cur_output_; }; // class Bencher