brpc_h2_unsent_message_unittest.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // brpc - A framework to host and access services throughout Baidu.
  18. // Date: Tue Oct 9 20:27:18 CST 2018
  19. #include <gflags/gflags.h>
  20. #include <gtest/gtest.h>
  21. #include "bthread/bthread.h"
  22. #include "butil/atomicops.h"
  23. #include "brpc/policy/http_rpc_protocol.h"
  24. #include "brpc/policy/http2_rpc_protocol.h"
  25. #include "butil/gperftools_profiler.h"
  26. int main(int argc, char* argv[]) {
  27. testing::InitGoogleTest(&argc, argv);
  28. return RUN_ALL_TESTS();
  29. }
  30. TEST(H2UnsentMessage, request_throughput) {
  31. brpc::Controller cntl;
  32. butil::IOBuf request_buf;
  33. cntl.http_request().uri() = "0.0.0.0:8010/HttpService/Echo";
  34. brpc::policy::SerializeHttpRequest(&request_buf, &cntl, NULL);
  35. brpc::SocketId id;
  36. brpc::SocketUniquePtr h2_client_sock;
  37. brpc::SocketOptions h2_client_options;
  38. h2_client_options.user = brpc::get_client_side_messenger();
  39. EXPECT_EQ(0, brpc::Socket::Create(h2_client_options, &id));
  40. EXPECT_EQ(0, brpc::Socket::Address(id, &h2_client_sock));
  41. brpc::policy::H2Context* ctx =
  42. new brpc::policy::H2Context(h2_client_sock.get(), NULL);
  43. CHECK_EQ(ctx->Init(), 0);
  44. h2_client_sock->initialize_parsing_context(&ctx);
  45. ctx->_last_sent_stream_id = 0;
  46. ctx->_remote_window_left = brpc::H2Settings::MAX_WINDOW_SIZE;
  47. int64_t ntotal = 500000;
  48. // calc H2UnsentRequest throughput
  49. butil::IOBuf dummy_buf;
  50. ProfilerStart("h2_unsent_req.prof");
  51. int64_t start_us = butil::gettimeofday_us();
  52. for (int i = 0; i < ntotal; ++i) {
  53. brpc::policy::H2UnsentRequest* req = brpc::policy::H2UnsentRequest::New(&cntl);
  54. req->AppendAndDestroySelf(&dummy_buf, h2_client_sock.get());
  55. }
  56. int64_t end_us = butil::gettimeofday_us();
  57. ProfilerStop();
  58. int64_t elapsed = end_us - start_us;
  59. LOG(INFO) << "H2UnsentRequest average qps="
  60. << (ntotal * 1000000L) / elapsed << "/s, data throughput="
  61. << dummy_buf.size() * 1000000L / elapsed << "/s";
  62. // calc H2UnsentResponse throughput
  63. dummy_buf.clear();
  64. start_us = butil::gettimeofday_us();
  65. for (int i = 0; i < ntotal; ++i) {
  66. // H2UnsentResponse::New would release cntl.http_response() and swap
  67. // cntl.response_attachment()
  68. cntl.http_response().set_content_type("text/plain");
  69. cntl.response_attachment().append("0123456789abcedef");
  70. brpc::policy::H2UnsentResponse* res = brpc::policy::H2UnsentResponse::New(&cntl, 0, false);
  71. res->AppendAndDestroySelf(&dummy_buf, h2_client_sock.get());
  72. }
  73. end_us = butil::gettimeofday_us();
  74. elapsed = end_us - start_us;
  75. LOG(INFO) << "H2UnsentResponse average qps="
  76. << (ntotal * 1000000L) / elapsed << "/s, data throughput="
  77. << dummy_buf.size() * 1000000L / elapsed << "/s";
  78. }