Loading...
Searching...
No Matches
juce_PerformanceCounter.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
static void appendToFile (const File& f, const String& s)
{
if (f.getFullPathName().isNotEmpty())
{
FileOutputStream out (f);
if (! out.failedToOpen())
out << s << newLine;
}
}
PerformanceCounter::PerformanceCounter (const String& name, int runsPerPrintout, const File& loggingFile)
: runsPerPrint (runsPerPrintout), startTime (0), outputFile (loggingFile)
{
stats.name = name;
appendToFile (outputFile, "**** Counter for \"" + name + "\" started at: " + Time::getCurrentTime().toString (true, true));
}
PerformanceCounter::~PerformanceCounter()
{
if (stats.numRuns > 0)
printStatistics();
}
PerformanceCounter::Statistics::Statistics() noexcept
: averageSeconds(), maximumSeconds(), minimumSeconds(), totalSeconds(), numRuns()
{
}
void PerformanceCounter::Statistics::clear() noexcept
{
averageSeconds = maximumSeconds = minimumSeconds = totalSeconds = 0;
numRuns = 0;
}
void PerformanceCounter::Statistics::addResult (double elapsed) noexcept
{
if (numRuns == 0)
{
maximumSeconds = elapsed;
minimumSeconds = elapsed;
}
else
{
maximumSeconds = jmax (maximumSeconds, elapsed);
minimumSeconds = jmin (minimumSeconds, elapsed);
}
++numRuns;
totalSeconds += elapsed;
}
static String timeToString (double secs)
{
return String ((int64) (secs * (secs < 0.01 ? 1000000.0 : 1000.0) + 0.5))
+ (secs < 0.01 ? " microsecs" : " millisecs");
}
String PerformanceCounter::Statistics::toString() const
{
MemoryOutputStream s;
s << "Performance count for \"" << name << "\" over " << numRuns << " run(s)" << newLine
<< "Average = " << timeToString (averageSeconds)
<< ", minimum = " << timeToString (minimumSeconds)
<< ", maximum = " << timeToString (maximumSeconds)
<< ", total = " << timeToString (totalSeconds);
return s.toString();
}
void PerformanceCounter::start() noexcept
{
startTime = Time::getHighResolutionTicks();
}
bool PerformanceCounter::stop()
{
stats.addResult (Time::highResolutionTicksToSeconds (Time::getHighResolutionTicks() - startTime));
if (stats.numRuns < runsPerPrint)
return false;
printStatistics();
return true;
}
void PerformanceCounter::printStatistics()
{
const String desc (getStatisticsAndReset().toString());
Logger::writeToLog (desc);
appendToFile (outputFile, desc);
}
PerformanceCounter::Statistics PerformanceCounter::getStatisticsAndReset()
{
Statistics s (stats);
stats.clear();
if (s.numRuns > 0)
s.averageSeconds = s.totalSeconds / (float) s.numRuns;
return s;
}
} // namespace juce
juce::FileOutputStream
Definition juce_FileOutputStream.h:35
juce::File
Definition juce_File.h:45
juce::Logger::writeToLog
static void JUCE_CALLTYPE writeToLog(const String &message)
Definition juce_Logger.cpp:40
juce::PerformanceCounter::stop
bool stop()
Definition juce_PerformanceCounter.cpp:102
juce::PerformanceCounter::~PerformanceCounter
~PerformanceCounter()
Definition juce_PerformanceCounter.cpp:44
juce::PerformanceCounter::getStatisticsAndReset
Statistics getStatisticsAndReset()
Definition juce_PerformanceCounter.cpp:121
juce::PerformanceCounter::PerformanceCounter
PerformanceCounter(const String &counterName, int runsPerPrintout=100, const File &loggingFile=File())
Definition juce_PerformanceCounter.cpp:37
juce::PerformanceCounter::start
void start() noexcept
Definition juce_PerformanceCounter.cpp:97
juce::PerformanceCounter::printStatistics
void printStatistics()
Definition juce_PerformanceCounter.cpp:113
juce::String
Definition juce_String.h:53
juce::Time::getHighResolutionTicks
static int64 getHighResolutionTicks() noexcept
juce::Time::getCurrentTime
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition juce_Time.cpp:233
juce::Time::highResolutionTicksToSeconds
static double highResolutionTicksToSeconds(int64 ticks) noexcept
Definition juce_Time.cpp:293
juce::PerformanceCounter::Statistics
Definition juce_PerformanceCounter.h:93