[vcpkg] Deduplicate all timer classes.

This commit is contained in:
Robert Schumacher 2017-03-04 06:25:05 -08:00
parent 4806aaf460
commit 19695fc832
6 changed files with 15 additions and 125 deletions

View File

@ -10,7 +10,7 @@ namespace vcpkg
public:
static ElapsedTime createStarted();
constexpr ElapsedTime() :m_startTick() {}
constexpr ElapsedTime() : m_startTick() {}
template <class TimeUnit>
TimeUnit elapsed() const
@ -18,42 +18,14 @@ namespace vcpkg
return std::chrono::duration_cast<TimeUnit>(std::chrono::high_resolution_clock::now() - this->m_startTick);
}
std::string toString() const;
private:
std::chrono::steady_clock::time_point m_startTick;
};
class Stopwatch
{
public:
static Stopwatch createUnstarted();
static Stopwatch createStarted();
bool isRunning() const;
const Stopwatch& start();
const Stopwatch& stop();
Stopwatch& reset();
template <class TimeUnit>
TimeUnit elapsed() const
double microseconds() const
{
return std::chrono::duration_cast<TimeUnit>(elapsedNanos());
return elapsed<std::chrono::duration<double, std::micro>>().count();
}
std::string toString() const;
private:
Stopwatch();
std::chrono::nanoseconds elapsedNanos() const;
bool m_isRunning;
std::chrono::nanoseconds m_elapsedNanos;
std::chrono::steady_clock::time_point m_startTick;
std::chrono::high_resolution_clock::time_point m_startTick;
};
}

View File

@ -84,15 +84,6 @@ namespace vcpkg::System
return println(c, Strings::format(messageTemplate, messageArgs...).c_str());
}
struct Stopwatch2
{
int64_t start_time, end_time, freq;
void start();
void stop();
double microseconds() const;
};
optional<std::wstring> get_environmental_variable(const wchar_t* varname) noexcept;
void set_environmental_variable(const wchar_t* varname, const wchar_t* varvalue) noexcept;

View File

@ -6,6 +6,7 @@
#include "PostBuildLint.h"
#include "vcpkg_Dependencies.h"
#include "vcpkg_System.h"
#include "vcpkg_Chrono.h"
#include "vcpkg_Environment.h"
#include "metrics.h"
#include "vcpkg_Enums.h"
@ -53,11 +54,11 @@ namespace vcpkg::Commands::Build
port_dir.generic_wstring(),
ports_cmake_script_path.generic_wstring());
System::Stopwatch2 timer;
timer.start();
ElapsedTime timer = ElapsedTime::createStarted();
int return_code = System::cmd_execute(command);
timer.stop();
TrackMetric("buildtimeus-" + spec.toString(), timer.microseconds());
auto buildtimeus = timer.microseconds();
TrackMetric("buildtimeus-" + spec.toString(), buildtimeus);
if (return_code != 0)
{

View File

@ -13,6 +13,7 @@
#include "vcpkg_Input.h"
#include "Paragraphs.h"
#include "vcpkg_Strings.h"
#include "vcpkg_Chrono.h"
using namespace vcpkg;
@ -153,8 +154,6 @@ static void loadConfig()
}
}
static System::Stopwatch2 g_timer;
static std::string trim_path_from_command_line(const std::string& full_command_line)
{
Checks::check_exit(full_command_line.size() > 0, "Internal failure - cannot have empty command line");
@ -175,16 +174,18 @@ static std::string trim_path_from_command_line(const std::string& full_command_l
return std::string(it, full_command_line.cend());
}
static ElapsedTime g_timer;
int wmain(const int argc, const wchar_t* const* const argv)
{
if (argc == 0)
std::abort();
g_timer.start();
g_timer = ElapsedTime::createStarted();
atexit([]()
{
g_timer.stop();
TrackMetric("elapsed_us", g_timer.microseconds());
auto elapsed_us = g_timer.microseconds();
TrackMetric("elapsed_us", elapsed_us);
Flush();
});

View File

@ -60,60 +60,4 @@ namespace vcpkg
{
return format_time_userfriendly(elapsed<std::chrono::nanoseconds>());
}
Stopwatch Stopwatch::createUnstarted()
{
return Stopwatch();
}
Stopwatch Stopwatch::createStarted()
{
return Stopwatch().start();
}
bool Stopwatch::isRunning() const
{
return this->m_isRunning;
}
const Stopwatch& Stopwatch::start()
{
Checks::check_exit(!this->m_isRunning, "This stopwatch is already running.");
this->m_isRunning = true;
this->m_startTick = std::chrono::high_resolution_clock::now();
return *this;
}
const Stopwatch& Stopwatch::stop()
{
auto tick = std::chrono::high_resolution_clock::now();
Checks::check_exit(this->m_isRunning, "This stopwatch is already stopped.");
this->m_isRunning = false;
this->m_elapsedNanos += tick - this->m_startTick;
return *this;
}
Stopwatch& Stopwatch::reset()
{
this->m_elapsedNanos = std::chrono::nanoseconds();
this->m_isRunning = false;
return *this;
}
std::string Stopwatch::toString() const
{
return format_time_userfriendly(this->elapsedNanos());
}
Stopwatch::Stopwatch() : m_isRunning(false), m_elapsedNanos(0), m_startTick() { }
std::chrono::nanoseconds Stopwatch::elapsedNanos() const
{
if (this->m_isRunning)
{
return std::chrono::high_resolution_clock::now() - this->m_startTick + this->m_elapsedNanos;
}
return this->m_elapsedNanos;
}
}

View File

@ -96,23 +96,4 @@ namespace vcpkg::System
{
_wputenv_s(varname, varvalue);
}
void Stopwatch2::start()
{
static_assert(sizeof(start_time) == sizeof(LARGE_INTEGER), "");
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&start_time));
}
void Stopwatch2::stop()
{
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&end_time));
QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&freq));
}
double Stopwatch2::microseconds() const
{
return (reinterpret_cast<const LARGE_INTEGER*>(&end_time)->QuadPart -
reinterpret_cast<const LARGE_INTEGER*>(&start_time)->QuadPart) * 1000000.0 / reinterpret_cast<const LARGE_INTEGER*>(&freq)->QuadPart;
}
}