VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_core / time / juce_PerformanceCounter.cpp
blob6bc000df37e81e3512668203b1feb3a129038d38
1 /*
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
8 licensing.
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
18 DISCLAIMED.
20 ==============================================================================
23 namespace juce
26 static void appendToFile (const File& f, const String& s)
28 if (f.getFullPathName().isNotEmpty())
30 FileOutputStream out (f);
32 if (! out.failedToOpen())
33 out << s << newLine;
37 PerformanceCounter::PerformanceCounter (const String& name, int runsPerPrintout, const File& loggingFile)
38 : runsPerPrint (runsPerPrintout), startTime (0), outputFile (loggingFile)
40 stats.name = name;
41 appendToFile (outputFile, "**** Counter for \"" + name + "\" started at: " + Time::getCurrentTime().toString (true, true));
44 PerformanceCounter::~PerformanceCounter()
46 if (stats.numRuns > 0)
47 printStatistics();
50 PerformanceCounter::Statistics::Statistics() noexcept
51 : averageSeconds(), maximumSeconds(), minimumSeconds(), totalSeconds(), numRuns()
55 void PerformanceCounter::Statistics::clear() noexcept
57 averageSeconds = maximumSeconds = minimumSeconds = totalSeconds = 0;
58 numRuns = 0;
61 void PerformanceCounter::Statistics::addResult (double elapsed) noexcept
63 if (numRuns == 0)
65 maximumSeconds = elapsed;
66 minimumSeconds = elapsed;
68 else
70 maximumSeconds = jmax (maximumSeconds, elapsed);
71 minimumSeconds = jmin (minimumSeconds, elapsed);
74 ++numRuns;
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
86 MemoryOutputStream s;
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);
94 return s.toString();
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)
107 return false;
109 printStatistics();
110 return true;
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);
124 stats.clear();
126 if (s.numRuns > 0)
127 s.averageSeconds = s.totalSeconds / (float) s.numRuns;
129 return s;
132 } // namespace juce