TNB Library
TnbParSecondCounter.h
[詳解]
1#pragma once
11#include "TnbSync.h"
12#include "TnbTickCount.h"
13
14
15
16//TNB Library
17namespace TNB
18{
19
20
21
33{
34public:
35
40 CParSecondCounter(size_t second = 5)
41 {
42 Initialize(second);
43 }
44
50 void Initialize(size_t second = 5)
51 {
52 if ( second < 1 )
53 {
54 second = 1;
55 }
56 m_postCounts.Resize(second + 1);
57 m_postCounts[0] = 0;
58 m_validCount = 0;
59 m_newestIndex = 0;
60 m_nowTickSecond = 0;
61 m_maxPostCount = 0;
62 m_tick.Reset();
63 }
64
70 void Post(DWORD c = 1)
71 {
72 EXCLUSIVE(&m_sync);
73 DWORD t = m_tick.GetPassedCount() / 1000;
74 if ( m_nowTickSecond == t )
75 {
76 m_postCounts[m_newestIndex] += c;
77 return;
78 }
79 m_nowTickSecond = t;
80 size_t count = m_postCounts.GetSize();
81 if ( m_validCount < count )
82 {
83 m_validCount++;
84 }
85 if ( m_postCounts[m_newestIndex] > m_maxPostCount )
86 {
87 m_maxPostCount = m_postCounts[m_newestIndex];
88 }
89 m_newestIndex = (m_newestIndex + 1) % count;
90 m_postCounts[m_newestIndex] = c;
91 }
92
97 double GetAverage(void) const
98 {
99 EXCLUSIVE(&m_sync);
100 if ( m_validCount > 1 )
101 {
102 size_t count = m_postCounts.GetSize();
103 DWORD r = 0;
104 loop ( i, m_validCount - 1 )
105 {
106 r += m_postCounts[(count + count + m_newestIndex - i - 1) % count];
107 }
108 double a = r;
109 return a / (m_validCount - 1);
110 }
111 return 0;
112 }
113
118 DWORD GetMax(void) const
119 {
120 return m_maxPostCount;
121 }
122
123private:
124 CWorkMemT<DWORD> m_postCounts;
125 DWORD m_validCount;
126 DWORD m_newestIndex;
127 DWORD m_nowTickSecond;
128 DWORD m_maxPostCount;
129 CTickCount m_tick;
130 CSyncSection m_sync;
131};
132
133
134
135};
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
同期処理関係のヘッダ
経過時間管理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
一秒間平均カウンタ管理クラス
CParSecondCounter(size_t second=5)
コンストラクタ.
void Post(DWORD c=1)
[設定] ポスト.
DWORD GetMax(void) const
[取得] 最大数取得.
void Initialize(size_t second=5)
[設定] 初期化.
double GetAverage(void) const
[取得] 平均取得.
Section排他管理クラス
Definition: TnbSync.h:125
経過時間管理クラス
Definition: TnbTickCount.h:57
DWORD GetPassedCount(void) const
[取得] 経過時間取得.
Definition: TnbTickCount.h:102
void Reset(void)
[設定] リセット.
Definition: TnbTickCount.h:82
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
Definition: TnbSync.h:788
TNB Library
Definition: TnbDoxyTitle.txt:2