OpenShot Audio Library | OpenShotAudio  0.3.1
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 {
46  if (stats.numRuns > 0)
48 }
49 
50 PerformanceCounter::Statistics::Statistics() noexcept
51  : averageSeconds(), maximumSeconds(), minimumSeconds(), totalSeconds(), numRuns()
52 {
53 }
54 
55 void PerformanceCounter::Statistics::clear() noexcept
56 {
57  averageSeconds = maximumSeconds = minimumSeconds = totalSeconds = 0;
58  numRuns = 0;
59 }
60 
61 void PerformanceCounter::Statistics::addResult (double elapsed) noexcept
62 {
63  if (numRuns == 0)
64  {
65  maximumSeconds = elapsed;
66  minimumSeconds = elapsed;
67  }
68  else
69  {
70  maximumSeconds = jmax (maximumSeconds, elapsed);
71  minimumSeconds = jmin (minimumSeconds, elapsed);
72  }
73 
74  ++numRuns;
75  totalSeconds += elapsed;
76 }
77 
78 static String timeToString (double secs)
79 {
80  return String ((int64) (secs * (secs < 0.01 ? 1000000.0 : 1000.0) + 0.5))
81  + (secs < 0.01 ? " microsecs" : " millisecs");
82 }
83 
84 String PerformanceCounter::Statistics::toString() const
85 {
87 
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);
93 
94  return s.toString();
95 }
96 
98 {
99  startTime = Time::getHighResolutionTicks();
100 }
101 
103 {
104  stats.addResult (Time::highResolutionTicksToSeconds (Time::getHighResolutionTicks() - startTime));
105 
106  if (stats.numRuns < runsPerPrint)
107  return false;
108 
109  printStatistics();
110  return true;
111 }
112 
114 {
115  const String desc (getStatisticsAndReset().toString());
116 
118  appendToFile (outputFile, desc);
119 }
120 
122 {
123  Statistics s (stats);
124  stats.clear();
125 
126  if (s.numRuns > 0)
127  s.averageSeconds = s.totalSeconds / s.numRuns;
128 
129  return s;
130 }
131 
132 } // namespace juce
static int64 getHighResolutionTicks() noexcept
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition: juce_Time.cpp:218
static double highResolutionTicksToSeconds(int64 ticks) noexcept
Definition: juce_Time.cpp:278
static void JUCE_CALLTYPE outputDebugString(const String &text)
PerformanceCounter(const String &counterName, int runsPerPrintout=100, const File &loggingFile=File())