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 
23 namespace juce
24 {
25 
26 static 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 
37 PerformanceCounter::PerformanceCounter (const String& name, int runsPerPrintout, const File& loggingFile)
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 
45 {
47 }
48 
49 PerformanceCounter::Statistics::Statistics() noexcept
50  : averageSeconds(), maximumSeconds(), minimumSeconds(), totalSeconds(), numRuns()
51 {
52 }
53 
54 void PerformanceCounter::Statistics::clear() noexcept
55 {
56  averageSeconds = maximumSeconds = minimumSeconds = totalSeconds = 0;
57  numRuns = 0;
58 }
59 
60 void 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 
77 static 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 
83 String 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 
97 {
98  startTime = Time::getHighResolutionTicks();
99 }
100 
102 {
103  stats.addResult (Time::highResolutionTicksToSeconds (Time::getHighResolutionTicks() - startTime));
104 
105  if (stats.numRuns < runsPerPrint)
106  return false;
107 
108  printStatistics();
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
juce::PerformanceCounter::printStatistics
void printStatistics()
Dumps the current metrics to the debugger output and to a file.
Definition: juce_PerformanceCounter.cpp:112
juce::PerformanceCounter::~PerformanceCounter
~PerformanceCounter()
Destructor.
Definition: juce_PerformanceCounter.cpp:44
juce::Time::highResolutionTicksToSeconds
static double highResolutionTicksToSeconds(int64 ticks) noexcept
Converts a number of high-resolution ticks into seconds.
Definition: juce_Time.cpp:278
juce::PerformanceCounter::getStatisticsAndReset
Statistics getStatisticsAndReset()
Returns a copy of the current stats, and resets the internal counter.
Definition: juce_PerformanceCounter.cpp:120
juce::Time::getCurrentTime
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
Definition: juce_Time.cpp:218
juce::Time::getHighResolutionTicks
static int64 getHighResolutionTicks() noexcept
Returns the current high-resolution counter's tick-count.
juce::File
Represents a local file or directory.
Definition: juce_File.h:44
juce::PerformanceCounter::start
void start() noexcept
Starts timing.
Definition: juce_PerformanceCounter.cpp:96
juce::PerformanceCounter::PerformanceCounter
PerformanceCounter(const String &counterName, int runsPerPrintout=100, const File &loggingFile=File())
Creates a PerformanceCounter object.
Definition: juce_PerformanceCounter.cpp:37
juce::PerformanceCounter::Statistics
Holds the current statistics.
Definition: juce_PerformanceCounter.h:96
juce::Logger::outputDebugString
static void JUCE_CALLTYPE outputDebugString(const String &text)
Writes a message to the standard error stream.
juce::PerformanceCounter::stop
bool stop()
Stops timing and prints out the results.
Definition: juce_PerformanceCounter.cpp:101
juce::String
The JUCE String class!
Definition: juce_String.h:42