简单使用rest rpc测试分布式网络速度

256 篇文章 3 订阅
149 篇文章 2 订阅

服务端:

#include "rpc_server.h"
using namespace rest_rpc;
using namespace rpc_service;

int main() {
    rpc_server server(9980, std::thread::hardware_concurrency());
    server.register_handler("publish", [&server] (rpc_conn conn, std::string key, std::string token, std::string val) {
        server.publish(std::move(key), std::move(val));
    });
    std::string str(1024, 'A');
    std::thread thd([&server, &str] {
        while (true) {
            server.publish("test_network_speed", str);
        }
    });

    server.run();
    
    return 0;
}

客户端:

#include <sys/timeb.h>
#include "rpc_client.hpp"
using namespace rest_rpc;
using namespace rpc_service;
int main(int argc, const char **argv) {
    if (argc != 3) {
        std::cerr << "usage:./program ip port" << std::endl;
        return -1;
    }
    const char *ip = argv[1];
    int port = atoi(argv[2]);
    rpc_client client;
    client.enable_auto_reconnect();
    client.enable_auto_heartbeat();
    bool r = client.connect(ip, port);
    if (false == r) {
        std::cerr << "connect to " << ip << ":" << port << "failed." << std::endl;
        return -1;
    }
    struct stat_info {
        const char *ip;
        int count = 0;
        long last_time = 0;
        long now_time = 0;
        long interval = 0;
        double speed = 0;
    };
    stat_info info;
    info.ip = ip;
    info.last_time = time(nullptr);
    const int M_100 = 1024 * 100;
    while (true) {
        client.subscribe("test_network_speed", [&info] (string_view str) {
            if (++info.count >= M_100) {     // 100M
                info.now_time = time(nullptr);
                info.interval = info.now_time - info.last_time;
                info.speed = 100.0 / info.interval;
                info.last_time = info.now_time;
                info.count = 0;
                std::cout << "from ip:" << info.ip << " to loalhost speed:" << info.speed << "M/S" << std::endl;
            }
        });
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值