我正在處理通過API接收請求並將其添加到FIFO的項目。我希望能夠記錄收到的請求數(添加到隊列中)和處理的請求數(從隊列中刪除)。目前我正在嘗試收到第二個平均值。計算每秒添加到隊列的請求數 - 總是返回爲零
我這樣做是通過一個包含60個元素的long數組來完成的,每個元素將存儲在那秒接收到的請求的數量。
我使用以下這樣:
if (fifo->enqueue(crashInfo))
{
this->requestProcessMutex.lock();
this->currentRequestsASecond++; //Used to get the total requests a second
this->requestProcessMutex.unlock();
cout << "Current Requests Incremented to " << this->currentRequestsASecond << endl;
return true;
}
else
{
return false;
}
從上面的代碼的COUT是表示計數器遞增,然後復位爲0作爲預期每個第二。
要將請求每秒添加到數組中,我執行以下操作,我還每10秒註銷一次當前平均值。
void FIFOManager::processRequestsSecondArray()
{
int counter = 0;
time_t lastLoggedTime = std::time(NULL);
while (this->bitsLibrary->getApplicationStatus() != StatusManager::ApplicationStatus::Stopping)
{
this->requestProcessMutex.lock();
time_t current_time = std::time(NULL);
long timeDiff = current_time - lastLoggedTime;
if (timeDiff >= 10) //Only log every 10 seconds
{
stringstream logstream;
logstream << this->getAverageRequestProcessTime(AverageRetrievalType::RequestsASec) << " requests received a second";
this->bitsLibrary->writeToLog(logstream.str(), "FIFOManager", "processRequestsSecondArray");
lastLoggedTime = std::time(NULL);
}
requestsASecondForAMinute[counter] = this->currentRequestsASecond;
cout << "ADDING REQUEST COUNTER VALUE " << this->currentRequestsASecond << " AT " << counter << endl;
if (counter < 59)
{
counter++;
}
else
{
counter = 0; //Only storing a minutes worth (60 secondS) so reset and start to overwrite
}
this->requestProcessMutex.unlock();
this_thread::sleep_for(chrono::seconds(1));
this->requestProcessMutex.lock();
this->currentRequestsASecond = 0;
this->requestProcessMutex.unlock();
}
}
的processRequestsSecondArray
是在陣列休眠1秒,在每一個第二應在當前的第二元件的currentRequestsASecond
的值存儲到所述陣列,它包裝每分鐘,並通過在陣列覆蓋。
ADDING REQUEST COUNTER VALUE
的輸出始終表明它正在添加0
但currentRequestsASecond
不會重置爲0,直到發生睡眠之後,我做錯了什麼?
Duh現在非常明顯,盯着它已經很久沒有看到它了。在將循環計數器插入數組後,我將循環中的計數器復位移至0。謝謝你的幫助 – Boardy