bandwidth_limit.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package features
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/onsi/ginkgo/v2"
  7. plugin "github.com/fatedier/frp/pkg/plugin/server"
  8. "github.com/fatedier/frp/test/e2e/framework"
  9. "github.com/fatedier/frp/test/e2e/framework/consts"
  10. "github.com/fatedier/frp/test/e2e/mock/server/streamserver"
  11. pluginpkg "github.com/fatedier/frp/test/e2e/pkg/plugin"
  12. "github.com/fatedier/frp/test/e2e/pkg/request"
  13. )
  14. var _ = ginkgo.Describe("[Feature: Bandwidth Limit]", func() {
  15. f := framework.NewDefaultFramework()
  16. ginkgo.It("Proxy Bandwidth Limit by Client", func() {
  17. serverConf := consts.DefaultServerConfig
  18. clientConf := consts.DefaultClientConfig
  19. localPort := f.AllocPort()
  20. localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort))
  21. f.RunServer("", localServer)
  22. remotePort := f.AllocPort()
  23. clientConf += fmt.Sprintf(`
  24. [[proxies]]
  25. name = "tcp"
  26. type = "tcp"
  27. localPort = %d
  28. remotePort = %d
  29. transport.bandwidthLimit = "10KB"
  30. `, localPort, remotePort)
  31. f.RunProcesses([]string{serverConf}, []string{clientConf})
  32. content := strings.Repeat("a", 50*1024) // 5KB
  33. start := time.Now()
  34. framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) {
  35. r.Body([]byte(content)).Timeout(30 * time.Second)
  36. }).ExpectResp([]byte(content)).Ensure()
  37. duration := time.Since(start)
  38. framework.Logf("request duration: %s", duration.String())
  39. framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String())
  40. })
  41. ginkgo.It("Proxy Bandwidth Limit by Server", func() {
  42. // new test plugin server
  43. newFunc := func() *plugin.Request {
  44. var r plugin.Request
  45. r.Content = &plugin.NewProxyContent{}
  46. return &r
  47. }
  48. pluginPort := f.AllocPort()
  49. handler := func(req *plugin.Request) *plugin.Response {
  50. var ret plugin.Response
  51. content := req.Content.(*plugin.NewProxyContent)
  52. content.BandwidthLimit = "10KB"
  53. content.BandwidthLimitMode = "server"
  54. ret.Content = content
  55. return &ret
  56. }
  57. pluginServer := pluginpkg.NewHTTPPluginServer(pluginPort, newFunc, handler, nil)
  58. f.RunServer("", pluginServer)
  59. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  60. [[httpPlugins]]
  61. name = "test"
  62. addr = "127.0.0.1:%d"
  63. path = "/handler"
  64. ops = ["NewProxy"]
  65. `, pluginPort)
  66. clientConf := consts.DefaultClientConfig
  67. localPort := f.AllocPort()
  68. localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort))
  69. f.RunServer("", localServer)
  70. remotePort := f.AllocPort()
  71. clientConf += fmt.Sprintf(`
  72. [[proxies]]
  73. name = "tcp"
  74. type = "tcp"
  75. localPort = %d
  76. remotePort = %d
  77. `, localPort, remotePort)
  78. f.RunProcesses([]string{serverConf}, []string{clientConf})
  79. content := strings.Repeat("a", 50*1024) // 5KB
  80. start := time.Now()
  81. framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) {
  82. r.Body([]byte(content)).Timeout(30 * time.Second)
  83. }).ExpectResp([]byte(content)).Ensure()
  84. duration := time.Since(start)
  85. framework.Logf("request duration: %s", duration.String())
  86. framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String())
  87. })
  88. })