Skip to content

Commit

Permalink
Remove stopwatch second and adapt tests (#247)
Browse files Browse the repository at this point in the history
* Remove stopwatch second and adapt tests

* Add timer debug

* Add more time to test stop watch

* 10% time margin
  • Loading branch information
thibault-martinez authored and Cylix committed Mar 6, 2018
1 parent e49c2ad commit d738e2d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
7 changes: 1 addition & 6 deletions include/iota/utils/stop_watch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ class StopWatch {
/**
* @return Elapsed time in milliseconds.
*/
std::chrono::milliseconds getElapsedTimeMilliSeconds() const;

/**
* @return Elapsed time in seconds.
*/
std::chrono::seconds getElapsedTimeSeconds() const;
std::chrono::milliseconds getElapsedTime() const;

public:
/**
Expand Down
16 changes: 8 additions & 8 deletions source/api/extended.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Extended::getInputs(const Models::Seed& seed, const int32_t& start, const int32_
auto res = getBalancesAndFormat(addresses, threshold);

//! update duration
res.setDuration(stopWatch.getElapsedTimeMilliSeconds().count());
res.setDuration(stopWatch.getElapsedTime().count());

return res;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ Extended::getBalancesAndFormat(const std::vector<Models::Address>& addresses,
throw Errors::IllegalState("Not enough balance");
}

return { inputs, totalBalance, stopWatch.getElapsedTimeMilliSeconds().count() };
return { inputs, totalBalance, stopWatch.getElapsedTime().count() };
}

Responses::GetNewAddresses
Expand Down Expand Up @@ -165,7 +165,7 @@ Extended::getNewAddresses(const Models::Seed& seed, const uint32_t& index, const
allAddresses.erase(std::begin(allAddresses), std::end(allAddresses) - 1);
}

return { allAddresses, stopWatch.getElapsedTimeMilliSeconds().count() };
return { allAddresses, stopWatch.getElapsedTime().count() };
}

Models::Bundle
Expand Down Expand Up @@ -563,7 +563,7 @@ Extended::getBundle(const Types::Trytes& transaction) const {
//! verify bundle integrity
verifyBundle(bundle);

return { bundle.getTransactions(), stopWatch.getElapsedTimeMilliSeconds().count() };
return { bundle.getTransactions(), stopWatch.getElapsedTime().count() };
}

void
Expand Down Expand Up @@ -655,7 +655,7 @@ Extended::getTransfers(const Models::Seed& seed, int start, int end, bool inclus
const auto gna = getNewAddresses(seed, start, end - start, true);
const auto bundles = bundlesFromAddresses(gna.getAddresses(), inclusionStates);

return { bundles, stopWatch.getElapsedTimeMilliSeconds().count() };
return { bundles, stopWatch.getElapsedTime().count() };
}

Responses::SendTransfer
Expand All @@ -675,7 +675,7 @@ Extended::sendTransfer(const Models::Seed& seed, int depth, int minWeightMagnitu
successful.push_back(!response.getHashes().empty());
}

return { successful, stopWatch.getElapsedTimeMilliSeconds().count() };
return { successful, stopWatch.getElapsedTime().count() };
}

std::vector<Models::Transaction>
Expand Down Expand Up @@ -735,7 +735,7 @@ Extended::getAccountData(const Models::Seed& seed, int start, int end, bool incl
const auto balances = getBalancesAndFormat(addresses.getAddresses(), threshold);

return { addresses.getAddresses(), transfers, balances.getTotalBalance(),
stopWatch.getElapsedTimeMilliSeconds().count() };
stopWatch.getElapsedTime().count() };
}

std::vector<Types::Trytes>
Expand Down Expand Up @@ -800,7 +800,7 @@ Extended::replayBundle(const Types::Trytes& transaction, int depth, int minWeigh
successful.push_back(!response.getHashes().empty());
}

return { successful, stopWatch.getElapsedTimeMilliSeconds().count() };
return { successful, stopWatch.getElapsedTime().count() };
}

std::vector<Models::Transaction>
Expand Down
9 changes: 2 additions & 7 deletions source/utils/stop_watch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ StopWatch::pause() {
return;
}

currentTime_ = getElapsedTimeMilliSeconds();
currentTime_ = getElapsedTime();
running_ = false;
}

Expand All @@ -66,19 +66,14 @@ StopWatch::resume() {
}

std::chrono::milliseconds
StopWatch::getElapsedTimeMilliSeconds() const {
StopWatch::getElapsedTime() const {
if (running_) {
return now() - startTime_;
}

return currentTime_;
}

std::chrono::seconds
StopWatch::getElapsedTimeSeconds() const {
return std::chrono::duration_cast<std::chrono::seconds>(getElapsedTimeMilliSeconds());
}

} // namespace Utils

} // namespace IOTA
35 changes: 12 additions & 23 deletions test/source/utils/stop_watch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ TEST(StopWatch, CtorShouldStartTimer) {

std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_EQ(s.getElapsedTimeSeconds().count(), 1);
EXPECT_TRUE(s.getElapsedTime().count() >= 900);
EXPECT_TRUE(s.getElapsedTime().count() <= 1100);
}

TEST(StopWatch, NowShouldReturnCurrentTime) {
Expand Down Expand Up @@ -67,7 +68,8 @@ TEST(StopWatch, RestartShouldRestartTimer) {
s.restart();
std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_EQ(s.getElapsedTimeSeconds().count(), 1);
EXPECT_TRUE(s.getElapsedTime().count() >= 900);
EXPECT_TRUE(s.getElapsedTime().count() <= 1100);
}

TEST(StopWatch, PauseShouldStopTimer) {
Expand All @@ -77,7 +79,8 @@ TEST(StopWatch, PauseShouldStopTimer) {
s.pause();
std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_EQ(s.getElapsedTimeSeconds().count(), 1);
EXPECT_TRUE(s.getElapsedTime().count() >= 900);
EXPECT_TRUE(s.getElapsedTime().count() <= 1100);
}

TEST(StopWatch, MultiplePauseShouldNotReset) {
Expand All @@ -89,7 +92,8 @@ TEST(StopWatch, MultiplePauseShouldNotReset) {
s.pause();
std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_EQ(s.getElapsedTimeSeconds().count(), 1);
EXPECT_TRUE(s.getElapsedTime().count() >= 900);
EXPECT_TRUE(s.getElapsedTime().count() <= 1100);
}

TEST(StopWatch, ResumeShouldResumePausedTimer) {
Expand All @@ -101,7 +105,8 @@ TEST(StopWatch, ResumeShouldResumePausedTimer) {
s.resume();
std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_EQ(s.getElapsedTimeSeconds().count(), 2);
EXPECT_TRUE(s.getElapsedTime().count() >= 1800);
EXPECT_TRUE(s.getElapsedTime().count() <= 2200);
}

TEST(StopWatch, ResumeShouldDoNothingIfRunning) {
Expand All @@ -111,22 +116,6 @@ TEST(StopWatch, ResumeShouldDoNothingIfRunning) {
s.resume();
std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_EQ(s.getElapsedTimeSeconds().count(), 2);
}

TEST(StopWatch, GetElapsedTimeMiliSeconds) {
IOTA::Utils::StopWatch s;

std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_TRUE(s.getElapsedTimeMilliSeconds().count() >= 1000);
EXPECT_TRUE(s.getElapsedTimeMilliSeconds().count() < 2000);
}

TEST(StopWatch, GetElapsedTimeSeconds) {
IOTA::Utils::StopWatch s;

std::this_thread::sleep_for(std::chrono::seconds(1));

EXPECT_EQ(s.getElapsedTimeSeconds().count(), 1);
EXPECT_TRUE(s.getElapsedTime().count() >= 1800);
EXPECT_TRUE(s.getElapsedTime().count() <= 2200);
}

0 comments on commit d738e2d

Please sign in to comment.