diff --git a/aecho2/src/aecho.cpp b/aecho2/src/aecho.cpp index d9be4c83e7fca9d732c4422d8ad861ece7c219e7..6aa088f0ab6e145e4236d20675989114374bcd09 100644 --- a/aecho2/src/aecho.cpp +++ b/aecho2/src/aecho.cpp @@ -13,6 +13,8 @@ #include <sax/ParserException.h> #include <object/Object.h> +#include <chrono> + int main(int argc, char** argv) { try { TCLAP::CmdLine cmd("echo binary", ' ', "0.01"); @@ -20,6 +22,12 @@ int main(int argc, char** argv) { TCLAP::ValueArg<std::string> input( "i", "input", "Input to echo", false, "-", "file"); cmd.add( input ); + TCLAP::SwitchArg measure( "m", "measure", "Measure times", false); + cmd.add( measure ); + + TCLAP::SwitchArg verbose( "v", "verbose", "Be verbose", false); + cmd.add( verbose ); + cmd.parse(argc, argv); std::deque<sax::Token> tokens; @@ -33,8 +41,17 @@ int main(int argc, char** argv) { sax::SaxParseInterface::parseStdin(tokens); } + std::chrono::measurements measurements; + measurements.start("Overal", std::chrono::measurements::Type::OVERALL); + measurements.start("XML Parse", std::chrono::measurements::Type::INIT); alib::Object object = alib::XmlDataFactory::fromTokens<alib::Object>(tokens); + measurements.end(); + measurements.start("XML Compose", std::chrono::measurements::Type::FINALIZE); alib::XmlDataFactory::toStdout(object); + measurements.end(); + measurements.end(); + + if(measure.getValue()) std::clog << measurements.results() << std::endl; return 0; } catch(const exception::AlibException& exception) { diff --git a/alib2std/src/chrono b/alib2std/src/chrono new file mode 100644 index 0000000000000000000000000000000000000000..bf4e51f27b73eea380e37ee57b802f2d444dc9ab --- /dev/null +++ b/alib2std/src/chrono @@ -0,0 +1,7 @@ +#ifndef __CHRONO_HEADER_WRAPPER_ +#define __CHRONO_HEADER_WRAPPER_ + +#include <bits/../chrono> +#include "extensions/chrono.hpp" + +#endif /* __CHRONO_HEADER_WRAPPER_ */ diff --git a/alib2std/src/extensions/chrono.hpp b/alib2std/src/extensions/chrono.hpp new file mode 100644 index 0000000000000000000000000000000000000000..158284e9a706ae4a62cb587942f0dc7e96cd3964 --- /dev/null +++ b/alib2std/src/extensions/chrono.hpp @@ -0,0 +1,100 @@ +/* + * clone.hpp + * + * Created on: Apr 1, 2013 + * Author: Jan Travnicek + */ + +#ifndef CHRONO_HPP_ +#define CHRONO_HPP_ + +namespace std { + +namespace chrono { + +class measure_duration { + time_point<system_clock> m_Start; + time_point<system_clock> m_End; + +public: + void start() { + m_Start = system_clock::now(); + } + + void end() { + m_End = system_clock::now(); + } + + microseconds duration() { + return duration_cast<microseconds>(m_End - m_Start); + } + +}; + +class measurements { +public: + enum class Type { + OVERALL, + INIT, + FINALIZE, + MAIN, + AUXILARY + }; + +private: + std::vector<std::tuple<std::string, measurements::Type, microseconds, microseconds>> measurements_vector; // name, type, complete duration, real duration + std::deque<std::tuple<std::string, measurements::Type, time_point<system_clock>, microseconds>> measurements_stack; // name, type, start, sub measurements duration + +public: + void start(std::string name, measurements::Type type) { + measurements_stack.emplace_back(std::move(name), type, system_clock::now(), microseconds(0)); + } + + void end() { + if(measurements_stack.empty()) throw std::logic_error("no measurement started"); + + std::tuple<std::string, measurements::Type, time_point<system_clock>, microseconds> current = std::move(measurements_stack.back()); + measurements_stack.pop_back(); + + microseconds dur = duration_cast<microseconds>(system_clock::now() - std::get<2>(current)); + + measurements_vector.emplace_back(std::move(std::get<0>(current)), std::get<1>(current), dur, dur - std::get<3>(current)); + if(!measurements_stack.empty()) + std::get<3>(measurements_stack.back()) = std::get<3>(measurements_stack.back()) + std::get<2>(measurements_vector.back()); + } + + const decltype(measurements_vector)& results() const { + return measurements_vector; + } + +}; + +std::ostream& operator<<(std::ostream& out, microseconds time) { + out << time.count() << "ms"; +} + +std::ostream& operator<<(std::ostream& out, measurements::Type type) { + switch(type) { + case measurements::Type::INIT: + out << "INIT"; + break; + case measurements::Type::FINALIZE: + out << "FINALIZE"; + break; + case measurements::Type::MAIN: + out << "MAIN"; + break; + case measurements::Type::AUXILARY: + out << "AUXILARY"; + break; + case measurements::Type::OVERALL: + out << "OVERALL"; + break; + } +} + +} /* namespace chrono */ + +} /* namespace std */ + +#endif /* CHRONO_HPP_ */ diff --git a/alib2std/test-src/extensions/ChronoTest.cpp b/alib2std/test-src/extensions/ChronoTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdaedd4fce53b93a675dbc2d0176a1bc7d570808 --- /dev/null +++ b/alib2std/test-src/extensions/ChronoTest.cpp @@ -0,0 +1,52 @@ +#include "ChronoTest.h" +#include <chrono> +#include <thread> + + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ChronoTest, "bits" ); +CPPUNIT_TEST_SUITE_REGISTRATION( ChronoTest ); + +void ChronoTest::setUp() { +} + +void ChronoTest::tearDown() { +} + +void ChronoTest::testChrono1() { + std::chrono::measure_duration tmp; + tmp.start(); + tmp.end(); + + std::cout << tmp.duration() << std::endl; + + auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(tmp.duration()); + std::cout << elapsed << std::endl; +} + +void ChronoTest::testChrono2() { + std::chrono::measurements meas; + meas.start("global", std::chrono::measurements::Type::MAIN); + + meas.start("init", std::chrono::measurements::Type::INIT); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + meas.end(); + + meas.start("main", std::chrono::measurements::Type::MAIN); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + meas.start("aux", std::chrono::measurements::Type::AUXILARY); + std::this_thread::sleep_for(std::chrono::milliseconds(40)); + meas.end(); + std::this_thread::sleep_for(std::chrono::milliseconds(80)); + meas.end(); + + meas.start("fin", std::chrono::measurements::Type::FINALIZE); + std::this_thread::sleep_for(std::chrono::milliseconds(30)); + meas.end(); + meas.end(); + + std::cout << meas.results() << std::endl; + + CPPUNIT_ASSERT((std::get<3>(meas.results()[1]) + std::get<3>(meas.results()[2]))/1000 == std::get<2>(meas.results()[2])/1000); + CPPUNIT_ASSERT((std::get<3>(meas.results()[0]) + std::get<3>(meas.results()[1]) + std::get<3>(meas.results()[2]) + std::get<3>(meas.results()[3]))/1000 == std::get<2>(meas.results()[4])/1000); +} + diff --git a/alib2std/test-src/extensions/ChronoTest.h b/alib2std/test-src/extensions/ChronoTest.h new file mode 100644 index 0000000000000000000000000000000000000000..6f1d4b6151751a110110187e61ba6185639fb3a2 --- /dev/null +++ b/alib2std/test-src/extensions/ChronoTest.h @@ -0,0 +1,21 @@ +#ifndef CHRONO_TEST_H_ +#define CHRONO_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class ChronoTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( ChronoTest ); + CPPUNIT_TEST( testChrono1 ); + CPPUNIT_TEST( testChrono2 ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testChrono1(); + void testChrono2(); +}; + +#endif // CHRONO_TEST_H_