test-perf.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2024 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "client.h"
  19. #include "dns.h"
  20. #include "include/utils.h"
  21. #include "server.h"
  22. #include "gtest/gtest.h"
  23. #include <fstream>
  24. class Perf : public ::testing::Test
  25. {
  26. protected:
  27. virtual void SetUp() {}
  28. virtual void TearDown() {}
  29. };
  30. TEST_F(Perf, no_speed_check)
  31. {
  32. smartdns::MockServer server_upstream;
  33. smartdns::Server server;
  34. if (smartdns::IsCommandExists("dnsperf") == false) {
  35. printf("dnsperf not found, skip test, please install dnsperf first.\n");
  36. GTEST_SKIP();
  37. }
  38. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  39. std::string domain = request->domain;
  40. if (request->domain.length() == 0) {
  41. return smartdns::SERVER_REQUEST_ERROR;
  42. }
  43. if (request->qtype == DNS_T_A) {
  44. unsigned char addr[4] = {1, 2, 3, 4};
  45. dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
  46. } else if (request->qtype == DNS_T_AAAA) {
  47. unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  48. dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
  49. } else {
  50. return smartdns::SERVER_REQUEST_ERROR;
  51. }
  52. request->response_packet->head.rcode = DNS_RC_NOERROR;
  53. return smartdns::SERVER_REQUEST_OK;
  54. });
  55. server.Start(R"""(bind [::]:60053
  56. server 127.0.0.1:61053
  57. speed-check-mode none
  58. log-level error
  59. socket-buff-size 1M
  60. )""");
  61. std::string file = "/tmp/smartdns-perftest-domain.list" + smartdns::GenerateRandomString(5);
  62. std::string cmd = "dnsperf -p 60053 -b 1024";
  63. cmd += " -d ";
  64. cmd += file;
  65. std::ofstream ofs(file);
  66. ASSERT_TRUE(ofs.is_open());
  67. Defer
  68. {
  69. ofs.close();
  70. unlink(file.c_str());
  71. };
  72. for (int i = 0; i < 100000; i++) {
  73. std::string domain = smartdns::GenerateRandomString(10);
  74. domain += ".";
  75. domain += smartdns::GenerateRandomString(3);
  76. if (random() % 2 == 0) {
  77. domain += " A";
  78. } else {
  79. domain += " AAAA";
  80. }
  81. domain += "\n";
  82. ofs.write(domain.c_str(), domain.length());
  83. ofs.flush();
  84. }
  85. system(cmd.c_str());
  86. }