我正在从事一个项目,该项目通过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
currentRequestsASecond
在当前第二个元素的数组中,每分钟它都会通过数组进行包装和覆盖。
的输出
ADDING REQUEST COUNTER VALUE
总是说它在添加
0
但是
当前请求秒
直到睡眠发生后才重置为0,那么我做错了什么?