test-perf.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 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(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. log-num 0
  58. log-console yes
  59. speed-check-mode none
  60. log-level error
  61. cache-persist no)""");
  62. std::string file = "/tmp/smartdns-perftest-domain.list" + smartdns::GenerateRandomString(5);
  63. std::string cmd = "dnsperf -p 60053";
  64. cmd += " -d ";
  65. cmd += file;
  66. std::ofstream ofs(file);
  67. ASSERT_TRUE(ofs.is_open());
  68. Defer
  69. {
  70. ofs.close();
  71. unlink(file.c_str());
  72. };
  73. for (int i = 0; i < 100000; i++) {
  74. std::string domain = smartdns::GenerateRandomString(10);
  75. domain += ".";
  76. domain += smartdns::GenerateRandomString(3);
  77. if (random() % 2 == 0) {
  78. domain += " A";
  79. } else {
  80. domain += " AAAA";
  81. }
  82. domain += "\n";
  83. ofs.write(domain.c_str(), domain.length());
  84. ofs.flush();
  85. }
  86. system(cmd.c_str());
  87. }