如何在高峰时段实时显示消息

发布于 2024-11-30 14:08:05 字数 4346 浏览 5 评论 0原文

我正在尝试找到每秒显示大量消息的最佳方法。我开发了以下程序,该程序生成随机数据,该数据模拟必须实时显示且无延迟的流。

程序显示 4 列:真实时间戳、数据时间戳、延迟时间和消息 id。 一个例子:
00:00:00.002000 00:00:00.000000 00:00:00.000000 #1
00:00:00.592034 00:00:00.585000 00:00:00.585000 #2
00:00:01.653095 00:00:01.642000 00:00:01.057000 #3
00:00:01.692097 00:00:01.675000 00:00:00.033000 #4
00:00:01.698097 00:00:01.675000 00:00:00.000000 #5
00:00:01.698097 00:00:01.675000 00:00:00.000000 #6
00:00:01.698097 00:00:01.675000 00:00:00.000000 #7
00:00:01.698097 00:00:01.675000 00:00:00.000000 #8
00:00:01.698097 00:00:01.675000 00:00:00.000000 #9
00:00:01.698097 00:00:01.675000 00:00:00.000000 #10
...

例如,第 4 行在 1.675 秒被“接收”,它与第 3 行相比有 0.033 秒的延迟,并且该消息实际上在 1.692097 处显示。第一列和第二列应尽可能接近。然而,当有数据选择时,计时器会出现分歧。运行程序时,您可以注意到第 2 列是粘滞的,因为在同一毫秒显示的消息是逐行绘制的,而不是一次全部显示。我不知道这是硬件限制还是实施不当,但测试最后显示第一列的时间比第二列的时间高几倍。我知道计算和数据显示需要一些时间,但我认为差异太大了。 我怎样才能匹配两个计时器?如果我不能,我怎样才能让它们尽可能接近?

非常感谢您抽出时间,

#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <time.h>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>

using namespace std;
using namespace boost::posix_time;

int main(int argc, char* argv[])
{
    srand (time(NULL));
    int rmil, num=0; //Integers for generating random milliseconds and counting messages 
    time_duration snapshot, sum = milliseconds(0); //Sum of total message delais and its holding value

    struct message
    {
        time_duration delay;
        string print;
    } m;

    list<message> mlist; //List of messages

    //Simulating 30 seconds of data with peaks of volume. 

    //The first message is at time 0
    m.delay = milliseconds(0); num++;
    m.print = to_simple_string(sum)+"  00:00:00.000000  #"+boost::lexical_cast<std::string>(num);
    mlist.push_back(m);

    while(sum<seconds(30)) //Generating 30 seconds of data
    {
        if(rand()%100<10) // Probability to have a peak data volume
        {
                snapshot = sum;
                while(sum<snapshot+seconds(1)) //Generating messages for 1 second
                {
                    rmil = rand() % 100; //0.050 second delay between packs of messages
                    int mpm = rand() % 150; //Num of Message per millisecond

                    m.delay = milliseconds(rmil);
                    num++; sum += milliseconds(rmil);
                    m.print = to_simple_string(sum)+"  "+to_simple_string(m.delay)+"  #"+boost::lexical_cast<std::string>(num);
                    mlist.push_back(m); 
                    for(int n=0;n<mpm;n++) //Adding messages at the same millisecond
                    {
                        m.delay = milliseconds(0); num++;
                        m.print = to_simple_string(sum)+"  00:00:00.000000  #"+boost::lexical_cast<std::string>(num);
                        mlist.push_back(m); //Push message to the list
                    }
                }
        }
        else
        {
            rmil = rand() % 2000; //1 second delay (average) between messages, no peak volume
            m.delay = milliseconds(rmil); 
            num++; sum += milliseconds(rmil);
            m.print = to_simple_string(sum)+"  "+to_simple_string(m.delay)+"  #"+boost::lexical_cast<std::string>(num);
            mlist.push_back(m);
        }
    }

    //Displaying messages with delay
    list<message>::iterator it = mlist.begin();

    stringstream scrmsg;
    ptime ltime = microsec_clock::local_time(); //Record the local time
    while(it!=mlist.end())
    {
        if((*it).delay > boost::posix_time::milliseconds(0)) 
        {
            boost::this_thread::sleep((*it).delay); 
            cout << to_simple_string(microsec_clock::local_time()-ltime) << "  " <<(*it).print << endl;
            it++;
        }
        else //Group the messages at the same millisecond
        {
            while((*it).delay == boost::posix_time::milliseconds(0))
            {
                scrmsg << to_simple_string(microsec_clock::local_time()-ltime) << "  " << (*it).print << endl;
                it++;
            }
            cout << scrmsg.str();
            scrmsg.str("");
        }
    }
}

I am trying to find the best way to display a huge number of messages per second. I developed the following program that generates random data which simulates a stream that has to be displayed in real time with no latancy.

The program shows 4 columns: Real timestamps, data timestamps, delayed time and message id.
An Example:
00:00:00.002000 00:00:00.000000 00:00:00.000000 #1
00:00:00.592034 00:00:00.585000 00:00:00.585000 #2
00:00:01.653095 00:00:01.642000 00:00:01.057000 #3
00:00:01.692097 00:00:01.675000 00:00:00.033000 #4
00:00:01.698097 00:00:01.675000 00:00:00.000000 #5
00:00:01.698097 00:00:01.675000 00:00:00.000000 #6
00:00:01.698097 00:00:01.675000 00:00:00.000000 #7
00:00:01.698097 00:00:01.675000 00:00:00.000000 #8
00:00:01.698097 00:00:01.675000 00:00:00.000000 #9
00:00:01.698097 00:00:01.675000 00:00:00.000000 #10
...

For instance, Line #4 was "received" at second 1.675, it had a delay of 0.033 seconds from line #3 and this message was actually displayed at 1.692097. The first and the second column should be as close as possible. However, timers diverge when there are picks of data. While running the program, you can notice how the column 2 is being sticky because messages displayed at the same millisecond are drawn line by line instead of being displayed all at once. I do not know whether it is a hardware limitation or a bad implementation, but the test shows at the end how the time of the first column is several times higher than the time of the second. I know calculations and data displaying take some time but I think that the difference is too much.
How can I match both timers? If I can't, how can I make them as close as possible?

Thank you very much for your time,

#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <time.h>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>

using namespace std;
using namespace boost::posix_time;

int main(int argc, char* argv[])
{
    srand (time(NULL));
    int rmil, num=0; //Integers for generating random milliseconds and counting messages 
    time_duration snapshot, sum = milliseconds(0); //Sum of total message delais and its holding value

    struct message
    {
        time_duration delay;
        string print;
    } m;

    list<message> mlist; //List of messages

    //Simulating 30 seconds of data with peaks of volume. 

    //The first message is at time 0
    m.delay = milliseconds(0); num++;
    m.print = to_simple_string(sum)+"  00:00:00.000000  #"+boost::lexical_cast<std::string>(num);
    mlist.push_back(m);

    while(sum<seconds(30)) //Generating 30 seconds of data
    {
        if(rand()%100<10) // Probability to have a peak data volume
        {
                snapshot = sum;
                while(sum<snapshot+seconds(1)) //Generating messages for 1 second
                {
                    rmil = rand() % 100; //0.050 second delay between packs of messages
                    int mpm = rand() % 150; //Num of Message per millisecond

                    m.delay = milliseconds(rmil);
                    num++; sum += milliseconds(rmil);
                    m.print = to_simple_string(sum)+"  "+to_simple_string(m.delay)+"  #"+boost::lexical_cast<std::string>(num);
                    mlist.push_back(m); 
                    for(int n=0;n<mpm;n++) //Adding messages at the same millisecond
                    {
                        m.delay = milliseconds(0); num++;
                        m.print = to_simple_string(sum)+"  00:00:00.000000  #"+boost::lexical_cast<std::string>(num);
                        mlist.push_back(m); //Push message to the list
                    }
                }
        }
        else
        {
            rmil = rand() % 2000; //1 second delay (average) between messages, no peak volume
            m.delay = milliseconds(rmil); 
            num++; sum += milliseconds(rmil);
            m.print = to_simple_string(sum)+"  "+to_simple_string(m.delay)+"  #"+boost::lexical_cast<std::string>(num);
            mlist.push_back(m);
        }
    }

    //Displaying messages with delay
    list<message>::iterator it = mlist.begin();

    stringstream scrmsg;
    ptime ltime = microsec_clock::local_time(); //Record the local time
    while(it!=mlist.end())
    {
        if((*it).delay > boost::posix_time::milliseconds(0)) 
        {
            boost::this_thread::sleep((*it).delay); 
            cout << to_simple_string(microsec_clock::local_time()-ltime) << "  " <<(*it).print << endl;
            it++;
        }
        else //Group the messages at the same millisecond
        {
            while((*it).delay == boost::posix_time::milliseconds(0))
            {
                scrmsg << to_simple_string(microsec_clock::local_time()-ltime) << "  " << (*it).print << endl;
                it++;
            }
            cout << scrmsg.str();
            scrmsg.str("");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虫児飞 2024-12-07 14:08:06

你无法匹配计时器。

没有可靠的方法来计时函数调用在同一毫秒内发生。您的操作系统可能需要暂停程序的执行以执行具有更高优先级的调用(例如在本例中为控制台输出)。

You could not match the timers.

There is no reliable way to time functions calls to happen in the same millisecond. Your operating system may need to pause the execution of your program to do calls with higher priority (in this case the console output for example).

朕就是辣么酷 2024-12-07 14:08:06

您可能正在查看线程量子。即使调度程序决定让您的线程立即再次运行,它仍然必须暂停它来决定 - 并且它可能决定允许其他线程运行。没有办法真正让你的程序以这种精度运行。当控制台 I/O 阻塞时尤其如此,这会轻而易举地打乱你的时间。

You're probably looking at the thread quantum. Even if the scheduler decides to let your thread run again immediately, it still has to pause it to decide that- and it may decide to allow other threads to run instead. There would be no way to actually make your program run within that kind of accuracy. This is especially true as console I/O is blocking, which will trivially throw off your times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文