bandwidth_limit.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.LegacyDefaultServerConfig
  18. clientConf := consts.LegacyDefaultClientConfig
  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. [tcp]
  25. type = tcp
  26. local_port = %d
  27. remote_port = %d
  28. bandwidth_limit = 10KB
  29. `, localPort, remotePort)
  30. f.RunProcesses([]string{serverConf}, []string{clientConf})
  31. content := strings.Repeat("a", 50*1024) // 5KB
  32. start := time.Now()
  33. framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) {
  34. r.Body([]byte(content)).Timeout(30 * time.Second)
  35. }).ExpectResp([]byte(content)).Ensure()
  36. duration := time.Since(start)
  37. framework.Logf("request duration: %s", duration.String())
  38. framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String())
  39. })
  40. ginkgo.It("Proxy Bandwidth Limit by Server", func() {
  41. // new test plugin server
  42. newFunc := func() *plugin.Request {
  43. var r plugin.Request
  44. r.Content = &plugin.NewProxyContent{}
  45. return &r
  46. }
  47. pluginPort := f.AllocPort()
  48. handler := func(req *plugin.Request) *plugin.Response {
  49. var ret plugin.Response
  50. content := req.Content.(*plugin.NewProxyContent)
  51. content.BandwidthLimit = "10KB"
  52. content.BandwidthLimitMode = "server"
  53. ret.Content = content
  54. return &ret
  55. }
  56. pluginServer := pluginpkg.NewHTTPPluginServer(pluginPort, newFunc, handler, nil)
  57. f.RunServer("", pluginServer)
  58. serverConf := consts.LegacyDefaultServerConfig + fmt.Sprintf(`
  59. [plugin.test]
  60. addr = 127.0.0.1:%d
  61. path = /handler
  62. ops = NewProxy
  63. `, pluginPort)
  64. clientConf := consts.LegacyDefaultClientConfig
  65. localPort := f.AllocPort()
  66. localServer := streamserver.New(streamserver.TCP, streamserver.WithBindPort(localPort))
  67. f.RunServer("", localServer)
  68. remotePort := f.AllocPort()
  69. clientConf += fmt.Sprintf(`
  70. [tcp]
  71. type = tcp
  72. local_port = %d
  73. remote_port = %d
  74. `, localPort, remotePort)
  75. f.RunProcesses([]string{serverConf}, []string{clientConf})
  76. content := strings.Repeat("a", 50*1024) // 5KB
  77. start := time.Now()
  78. framework.NewRequestExpect(f).Port(remotePort).RequestModify(func(r *request.Request) {
  79. r.Body([]byte(content)).Timeout(30 * time.Second)
  80. }).ExpectResp([]byte(content)).Ensure()
  81. duration := time.Since(start)
  82. framework.Logf("request duration: %s", duration.String())
  83. framework.ExpectTrue(duration.Seconds() > 8, "100Kb with 10KB limit, want > 8 seconds, but got %s", duration.String())
  84. })
  85. })