PageRenderTime 191ms CodeModel.GetById 58ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/newview/llsimplestat.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 152 lines | 68 code | 18 blank | 66 comment | 4 complexity | 5bc697beca1d1c678c5d3e814fb2a431 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsimplestat.h
  3. * @brief Runtime statistics accumulation.
  4. *
  5. * $LicenseInfo:firstyear=2010&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_SIMPLESTAT_H
  27. #define LL_SIMPLESTAT_H
  28. // History
  29. //
  30. // The original source for this code is the server repositories'
  31. // llcommon/llstat.h file. This particular code was added after the
  32. // viewer/server code schism but before the effort to convert common
  33. // code to libraries was complete. Rather than add to merge issues,
  34. // the needed code was cut'n'pasted into this new header as it isn't
  35. // too awful a burden. Post-modularization, we can look at removing
  36. // this redundancy.
  37. /**
  38. * @class LLSimpleStatCounter
  39. * @brief Just counts events.
  40. *
  41. * Really not needed but have a pattern in mind in the future.
  42. * Interface limits what can be done at that's just fine.
  43. *
  44. * *TODO: Update/transfer unit tests
  45. * Unit tests: indra/test/llcommon_llstat_tut.cpp
  46. */
  47. class LLSimpleStatCounter
  48. {
  49. public:
  50. inline LLSimpleStatCounter() { reset(); }
  51. // Default destructor and assignment operator are valid
  52. inline void reset() { mCount = 0; }
  53. inline void merge(const LLSimpleStatCounter & src)
  54. { mCount += src.mCount; }
  55. inline U32 operator++() { return ++mCount; }
  56. inline U32 getCount() const { return mCount; }
  57. protected:
  58. U32 mCount;
  59. };
  60. /**
  61. * @class LLSimpleStatMMM
  62. * @brief Templated collector of min, max and mean data for stats.
  63. *
  64. * Fed a stream of data samples, keeps a running account of the
  65. * min, max and mean seen since construction or the last reset()
  66. * call. A freshly-constructed or reset instance returns counts
  67. * and values of zero.
  68. *
  69. * Overflows and underflows (integer, inf or -inf) and NaN's
  70. * are the caller's problem. As is loss of precision when
  71. * the running sum's exponent (when parameterized by a floating
  72. * point of some type) differs from a given data sample's.
  73. *
  74. * Unit tests: indra/test/llcommon_llstat_tut.cpp
  75. */
  76. template <typename VALUE_T = F32>
  77. class LLSimpleStatMMM
  78. {
  79. public:
  80. typedef VALUE_T Value;
  81. public:
  82. LLSimpleStatMMM() { reset(); }
  83. // Default destructor and assignment operator are valid
  84. /**
  85. * Resets the object returning all counts and derived
  86. * values back to zero.
  87. */
  88. void reset()
  89. {
  90. mCount = 0;
  91. mMin = Value(0);
  92. mMax = Value(0);
  93. mTotal = Value(0);
  94. }
  95. void record(Value v)
  96. {
  97. if (mCount)
  98. {
  99. mMin = llmin(mMin, v);
  100. mMax = llmax(mMax, v);
  101. }
  102. else
  103. {
  104. mMin = v;
  105. mMax = v;
  106. }
  107. mTotal += v;
  108. ++mCount;
  109. }
  110. void merge(const LLSimpleStatMMM<VALUE_T> & src)
  111. {
  112. if (! mCount)
  113. {
  114. *this = src;
  115. }
  116. else if (src.mCount)
  117. {
  118. mMin = llmin(mMin, src.mMin);
  119. mMax = llmax(mMax, src.mMax);
  120. mCount += src.mCount;
  121. mTotal += src.mTotal;
  122. }
  123. }
  124. inline U32 getCount() const { return mCount; }
  125. inline Value getMin() const { return mMin; }
  126. inline Value getMax() const { return mMax; }
  127. inline Value getMean() const { return mCount ? mTotal / mCount : mTotal; }
  128. protected:
  129. U32 mCount;
  130. Value mMin;
  131. Value mMax;
  132. Value mTotal;
  133. };
  134. #endif // LL_SIMPLESTAT_H