2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
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.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 static void appendToFile (const File
& f
, const String
& s
)
28 if (f
.getFullPathName().isNotEmpty())
30 FileOutputStream
out (f
);
32 if (! out
.failedToOpen())
37 PerformanceCounter::PerformanceCounter (const String
& name
, int runsPerPrintout
, const File
& loggingFile
)
38 : runsPerPrint (runsPerPrintout
), startTime (0), outputFile (loggingFile
)
41 appendToFile (outputFile
, "**** Counter for \"" + name
+ "\" started at: " + Time::getCurrentTime().toString (true, true));
44 PerformanceCounter::~PerformanceCounter()
46 if (stats
.numRuns
> 0)
50 PerformanceCounter::Statistics::Statistics() noexcept
51 : averageSeconds(), maximumSeconds(), minimumSeconds(), totalSeconds(), numRuns()
55 void PerformanceCounter::Statistics::clear() noexcept
57 averageSeconds
= maximumSeconds
= minimumSeconds
= totalSeconds
= 0;
61 void PerformanceCounter::Statistics::addResult (double elapsed
) noexcept
65 maximumSeconds
= elapsed
;
66 minimumSeconds
= elapsed
;
70 maximumSeconds
= jmax (maximumSeconds
, elapsed
);
71 minimumSeconds
= jmin (minimumSeconds
, elapsed
);
75 totalSeconds
+= elapsed
;
78 static String
timeToString (double secs
)
80 return String ((int64
) (secs
* (secs
< 0.01 ? 1000000.0 : 1000.0) + 0.5))
81 + (secs
< 0.01 ? " microsecs" : " millisecs");
84 String
PerformanceCounter::Statistics::toString() const
88 s
<< "Performance count for \"" << name
<< "\" over " << numRuns
<< " run(s)" << newLine
89 << "Average = " << timeToString (averageSeconds
)
90 << ", minimum = " << timeToString (minimumSeconds
)
91 << ", maximum = " << timeToString (maximumSeconds
)
92 << ", total = " << timeToString (totalSeconds
);
97 void PerformanceCounter::start() noexcept
99 startTime
= Time::getHighResolutionTicks();
102 bool PerformanceCounter::stop()
104 stats
.addResult (Time::highResolutionTicksToSeconds (Time::getHighResolutionTicks() - startTime
));
106 if (stats
.numRuns
< runsPerPrint
)
113 void PerformanceCounter::printStatistics()
115 const String
desc (getStatisticsAndReset().toString());
117 Logger::writeToLog (desc
);
118 appendToFile (outputFile
, desc
);
121 PerformanceCounter::Statistics
PerformanceCounter::getStatisticsAndReset()
123 Statistics
s (stats
);
127 s
.averageSeconds
= s
.totalSeconds
/ (float) s
.numRuns
;