brpc_h2_unsent_message_unittest.cpp 2.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// brpc - A framework to host and access services throughout Baidu.
// Copyright (c) 2018 BiliBili, Inc.

// Date: Tue Oct 9 20:27:18 CST 2018

#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include "bthread/bthread.h"
#include "butil/atomicops.h"
#include "brpc/policy/http_rpc_protocol.h"
#include "brpc/policy/http2_rpc_protocol.h"
zhujiashun's avatar
zhujiashun committed
12
#include "butil/gperftools_profiler.h"
13 14 15 16 17 18 19 20 21

int main(int argc, char* argv[]) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

TEST(H2UnsentMessage, request_throughput) {
    brpc::Controller cntl;
    butil::IOBuf request_buf;
zhujiashun's avatar
zhujiashun committed
22
    cntl.http_request().uri() = "0.0.0.0:8010/HttpService/Echo";
23 24 25 26 27 28 29 30 31 32 33 34 35
    brpc::policy::SerializeHttpRequest(&request_buf, &cntl, NULL);

    brpc::SocketId id;
    brpc::SocketUniquePtr h2_client_sock;
    brpc::SocketOptions h2_client_options;
    h2_client_options.user = brpc::get_client_side_messenger();
    EXPECT_EQ(0, brpc::Socket::Create(h2_client_options, &id));
    EXPECT_EQ(0, brpc::Socket::Address(id, &h2_client_sock));

    brpc::policy::H2Context* ctx =
        new brpc::policy::H2Context(h2_client_sock.get(), NULL);
    CHECK_EQ(ctx->Init(), 0);
    h2_client_sock->initialize_parsing_context(&ctx);
36
    ctx->_last_sent_stream_id = 0;
zhujiashun's avatar
zhujiashun committed
37
    ctx->_remote_window_left = brpc::H2Settings::MAX_WINDOW_SIZE;
38

39
    int64_t ntotal = 500000;
40 41 42

    // calc H2UnsentRequest throughput
    butil::IOBuf dummy_buf;
zhujiashun's avatar
zhujiashun committed
43
    ProfilerStart("h2_unsent_req.prof");
44 45
    int64_t start_us = butil::gettimeofday_us();
    for (int i = 0; i < ntotal; ++i) {
46 47
        brpc::policy::H2UnsentRequest* req = brpc::policy::H2UnsentRequest::New(&cntl);
        req->AppendAndDestroySelf(&dummy_buf, h2_client_sock.get());
48 49
    }
    int64_t end_us = butil::gettimeofday_us();
zhujiashun's avatar
zhujiashun committed
50
    ProfilerStop();
zhujiashun's avatar
zhujiashun committed
51 52 53
    int64_t elapsed = end_us - start_us;
    LOG(INFO) << "H2UnsentRequest average qps="
        << (ntotal * 1000000L) / elapsed << "/s, data throughput="
54
        << dummy_buf.size() * 1000000L / elapsed << "/s";
55 56 57 58 59

    // calc H2UnsentResponse throughput
    dummy_buf.clear();
    start_us = butil::gettimeofday_us();
    for (int i = 0; i < ntotal; ++i) {
60 61 62 63 64 65
        // H2UnsentResponse::New would release cntl.http_response() and swap
        // cntl.response_attachment()
        cntl.http_response().set_content_type("text/plain");
        cntl.response_attachment().append("0123456789abcedef");
        brpc::policy::H2UnsentResponse* res = brpc::policy::H2UnsentResponse::New(&cntl, 0, false);
        res->AppendAndDestroySelf(&dummy_buf, h2_client_sock.get());
66 67
    }
    end_us = butil::gettimeofday_us();
zhujiashun's avatar
zhujiashun committed
68 69 70
    elapsed = end_us - start_us;
    LOG(INFO) << "H2UnsentResponse average qps="
        << (ntotal * 1000000L) / elapsed << "/s, data throughput="
71
        << dummy_buf.size() * 1000000L / elapsed << "/s";
72
}