OpenShot Library | libopenshot-audio 0.2.0
juce_PerformanceCounter.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26static void appendToFile (const File& f, const String& s)
27{
28 if (f.getFullPathName().isNotEmpty())
29 {
30 FileOutputStream out (f);
31
32 if (! out.failedToOpen())
33 out << s << newLine;
34 }
35}
36
38 : runsPerPrint (runsPerPrintout), startTime (0), outputFile (loggingFile)
39{
40 stats.name = name;
41 appendToFile (outputFile, "**** Counter for \"" + name + "\" started at: " + Time::getCurrentTime().toString (true, true));
42}
43
48
49PerformanceCounter::Statistics::Statistics() noexcept
50 : averageSeconds(), maximumSeconds(), minimumSeconds(), totalSeconds(), numRuns()
51{
52}
53
54void PerformanceCounter::Statistics::clear() noexcept
55{
56 averageSeconds = maximumSeconds = minimumSeconds = totalSeconds = 0;
57 numRuns = 0;
58}
59
60void PerformanceCounter::Statistics::addResult (double elapsed) noexcept
61{
62 if (numRuns == 0)
63 {
64 maximumSeconds = elapsed;
65 minimumSeconds = elapsed;
66 }
67 else
68 {
69 maximumSeconds = jmax (maximumSeconds, elapsed);
70 minimumSeconds = jmin (minimumSeconds, elapsed);
71 }
72
73 ++numRuns;
74 totalSeconds += elapsed;
75}
76
77static String timeToString (double secs)
78{
79 return String ((int64) (secs * (secs < 0.01 ? 1000000.0 : 1000.0) + 0.5))
80 + (secs < 0.01 ? " microsecs" : " millisecs");
81}
82
83String PerformanceCounter::Statistics::toString() const
84{
85 MemoryOutputStream s;
86
87 s << "Performance count for \"" << name << "\" over " << numRuns << " run(s)" << newLine
88 << "Average = " << timeToString (averageSeconds)
89 << ", minimum = " << timeToString (minimumSeconds)
90 << ", maximum = " << timeToString (maximumSeconds)
91 << ", total = " << timeToString (totalSeconds);
92
93 return s.toString();
94}
95
100
102{
104
105 if (stats.numRuns < runsPerPrint)
106 return false;
107
109 return true;
110}
111
113{
114 const String desc (getStatisticsAndReset().toString());
115
117 appendToFile (outputFile, desc);
118}
119
121{
122 Statistics s (stats);
123 stats.clear();
124
125 if (s.numRuns > 0)
126 s.averageSeconds = s.totalSeconds / s.numRuns;
127
128 return s;
129}
130
131} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
Represents a local file or directory.
Definition juce_File.h:45
static void JUCE_CALLTYPE outputDebugString(const String &text)
Writes a message to the standard error stream.
bool stop()
Stops timing and prints out the results.
Statistics getStatisticsAndReset()
Returns a copy of the current stats, and resets the internal counter.
PerformanceCounter(const String &counterName, int runsPerPrintout=100, const File &loggingFile=File())
Creates a PerformanceCounter object.
void start() noexcept
Starts timing.
void printStatistics()
Dumps the current metrics to the debugger output and to a file.
The JUCE String class!
Definition juce_String.h:43
static int64 getHighResolutionTicks() noexcept
Returns the current high-resolution counter's tick-count.
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
static double highResolutionTicksToSeconds(int64 ticks) noexcept
Converts a number of high-resolution ticks into seconds.