我不应该看到单线程与多线程websocketpp服务器之间CPU使用率的差异吗?

问题描述 投票:-1回答:1

我正在使用我配置的mulithreaded websocketpp服务器:

Server::Server(int ep) {
    using websocketpp::lib::placeholders::_1;
    using websocketpp::lib::placeholders::_2;
    using websocketpp::lib::bind;

    Server::wspp_server.clear_access_channels(websocketpp::log::alevel::all);

    Server::wspp_server.init_asio();

    Server::wspp_server.set_open_handler(bind(&Server::on_open, this, _1));;
    Server::wspp_server.set_close_handler(bind(&Server::on_close, this, _1));
    Server::wspp_server.set_message_handler(bind(&Server::on_message, this, _1, _2));

    try {
        Server::wspp_server.listen(ep);
    } catch (const websocketpp::exception &e){
        std::cout << "Error in Server::Server(int): " << e.what() << std::endl;
    }
    Server::wspp_server.start_accept();
}

void Server::run(int threadCount) {
    boost::thread_group tg;

    for (int i = 0; i < threadCount; i++) {
        tg.add_thread(new boost::thread(
            &websocketpp::server<websocketpp::config::asio>::run,
            &Server::wspp_server));
        std::cout << "Spawning thread " << (i + 1) << std::endl;
    }

    tg.join_all();
}

void Server::updateClients() {
    /*
       run updates
    */
    for (websocketpp::connection_hdl hdl : Server::conns) {
        try {
            std::string message = "personalized message for this client from the ran update above";
            wspp_server.send(hdl, message, websocketpp::frame::opcode::text);
        } catch (const websocketpp::exception &e) {
            std::cout << "Error in Server::updateClients(): " << e.what() << std::endl;
        }
    }
}

void Server::on_open(websocketpp::connection_hdl hdl) {
    boost::lock_guard<boost::shared_mutex> lock(Server::conns_mutex);
    Server::conns.insert(hdl);

    //do stuff


    //when the first client connects, start the update routine
    if (conns.size() == 1) {
        Server::run = true;
        bool *run = &(Server::run);
        std::thread([run] () {
            while (*run) {
                auto nextTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(15);
                Server::updateClients();
                std::this_thread::sleep_until(nextTime);
            }
        }).detach();
    }
}

void Server::on_close(websocketpp::connection_hdl hdl) {
    boost::lock_guard<boost::shared_mutex> lock(Server::conns_mutex);
    Server::conns.erase(hdl);

    //do stuff

    //stop the update loop when all clients are gone
    if (conns.size() < 1)
        Server::run = false;
}

void Server::on_message(
        websocketpp::connection_hdl hdl,
        websocketpp::server<websocketpp::config::asio>::message_ptr msg) {
    boost::lock_guard<boost::shared_mutex> lock(Server::conns_mutex);

    //do stuff
}

我启动服务器:

int port = 9000;
Server server(port);
server.run(/* number of threads */);

添加连接时唯一的实质区别在于消息发射[wssp.send(...)]。越来越多的客户端并没有真正为内部计算添加任何东西。它只是增加了要发出的消息量。

我的问题是,无论我使用1个还是多个线程,CPU使用率似乎都没有那么大的差异。

我用server.run(1)server.run(4)启动服务器并不重要(两者都在4核CPU专用服务器上)。对于类似的负载,CPU使用率图表显示大致相同的百分比。我希望4个线程并行运行时使用率会降低。我想错了吗?

在某些时候,我感觉到并行性真的适用于听力部分而不是发射。因此,我尝试将send封装在一个新线程(我分离)中,因此它独立于需要它的序列,但它没有改变图形上的任何内容。

我不应该看到CPU产生的工作有什么不同吗?否则,我做错了什么?为了强制从不同的线程发出消息,我还缺少另一个步骤吗?

c++ multithreading boost-asio websocket++
1个回答
1
投票

“我的问题是,无论我使用1个还是多个线程,CPU使用率似乎都没那么大。”

那不是问题。这是事实。它只是意味着整个事情不受CPU限制。这应该是非常明显的,因为它是网络IO。事实上,出于这个原因,高性能服务器通常只将1个线程专用于所有IO任务。

“我期待4个线程并行运行时使用率会降低。我是否认为这是错误的方法?”

是的,似乎。如果您以4种方式分摊账单,您不希望支付更少的费用。

实际上,就像在餐馆吃饭一样,由于分担负担(成本/任务)的开销,您最终会支付更多费用。除非您需要更多的CPU容量/更低的响应时间,否则单个IO线程(显然)更高效,因为没有调度开销和/或上下文切换损失。

另一种心理锻炼:

  • 如果您运行100个线程,处理器将在最佳情况下在可用内核中安排所有线程
  • 同样,如果您的系统上正在运行其他进程(显然,总是存在),则处理器可能会将您的4个线程安排在同一个逻辑核心上。您是否期望CPU负载更低?为什么? (提示:当然不是)。

背景:What is the difference between concurrency, parallelism and asynchronous methods?

© www.soinside.com 2019 - 2024. All rights reserved.