types.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package bwtest
  2. import (
  3. "time"
  4. )
  5. // ServerConfig defines the Bandwidth Test server configuration
  6. type ServerConfig struct {
  7. Enabled bool `json:"enabled"`
  8. Port int `json:"port"`
  9. ListenAddr string `json:"listen_addr"`
  10. AllowAnonymous bool `json:"allow_anonymous"`
  11. }
  12. // SessionRequest defines parameters sent by the client to initiate a test
  13. type SessionRequest struct {
  14. Target string `json:"target"` // Target IP or hostname
  15. Port int `json:"port"` // Target control port (default 2000)
  16. Protocol string `json:"protocol"` // "udp" or "tcp"
  17. Direction string `json:"direction"` // "tx", "rx", "both"
  18. BandwidthLimit int64 `json:"bandwidth_limit"` // Target bits/sec (0 = unlimited line-rate)
  19. PacketSize int `json:"packet_size"` // Packet payload size in bytes (e.g. 1500)
  20. Streams int `json:"streams"` // Number of parallel worker streams (1..8)
  21. Duration int `json:"duration"` // Duration in seconds (0 = continuous until stopped)
  22. }
  23. // SessionHandshake defines the control plane message exchanged between client and server
  24. type SessionHandshake struct {
  25. Type string `json:"type"` // "request", "accept", "reject", "report", "stop"
  26. SessionID string `json:"session_id"`
  27. Protocol string `json:"protocol"` // "udp" or "tcp"
  28. Direction string `json:"direction"` // "tx", "rx", "both"
  29. BandwidthLimit int64 `json:"bandwidth_limit"` // Target bits/sec
  30. PacketSize int `json:"packet_size"`
  31. Streams int `json:"streams"`
  32. Duration int `json:"duration"`
  33. UDPPort int `json:"udp_port,omitempty"`
  34. Message string `json:"message,omitempty"`
  35. RxBytes int64 `json:"rx_bytes,omitempty"`
  36. RxPackets int64 `json:"rx_packets,omitempty"`
  37. LostPackets int64 `json:"lost_packets,omitempty"`
  38. JitterMs float64 `json:"jitter_ms,omitempty"`
  39. }
  40. // LiveSample represents real-time bandwidth metrics captured at an interval (e.g. every 500ms)
  41. type LiveSample struct {
  42. Timestamp time.Time `json:"timestamp"`
  43. ElapsedSec float64 `json:"elapsed_sec"`
  44. TxBps int64 `json:"tx_bps"`
  45. RxBps int64 `json:"rx_bps"`
  46. TxMbps float64 `json:"tx_mbps"`
  47. RxMbps float64 `json:"rx_mbps"`
  48. TxPackets int64 `json:"tx_packets"`
  49. RxPackets int64 `json:"rx_packets"`
  50. TxBytes int64 `json:"tx_bytes"`
  51. RxBytes int64 `json:"rx_bytes"`
  52. LostPackets int64 `json:"lost_packets"`
  53. LossPct float64 `json:"loss_pct"`
  54. JitterMs float64 `json:"jitter_ms"`
  55. RTTMs float64 `json:"rtt_ms"`
  56. }
  57. // TestSummary represents the finalized results of a completed bandwidth test
  58. type TestSummary struct {
  59. DurationSec float64 `json:"duration_sec"`
  60. StartTime time.Time `json:"start_time"`
  61. EndTime time.Time `json:"end_time"`
  62. TotalTxBytes int64 `json:"total_tx_bytes"`
  63. TotalRxBytes int64 `json:"total_rx_bytes"`
  64. AvgTxMbps float64 `json:"avg_tx_mbps"`
  65. AvgRxMbps float64 `json:"avg_rx_mbps"`
  66. PeakTxMbps float64 `json:"peak_tx_mbps"`
  67. PeakRxMbps float64 `json:"peak_rx_mbps"`
  68. TotalTxPackets int64 `json:"total_tx_packets"`
  69. TotalRxPackets int64 `json:"total_rx_packets"`
  70. TotalLostPackets int64 `json:"total_lost_packets"`
  71. AvgLossPct float64 `json:"avg_loss_pct"`
  72. AvgJitterMs float64 `json:"avg_jitter_ms"`
  73. }
  74. // TestStatus provides a complete real-time snapshot of the active client test
  75. type TestStatus struct {
  76. Running bool `json:"running"`
  77. SessionID string `json:"session_id"`
  78. Target string `json:"target"`
  79. Port int `json:"port"`
  80. Protocol string `json:"protocol"`
  81. Direction string `json:"direction"`
  82. BandwidthLimit int64 `json:"bandwidth_limit"`
  83. PacketSize int `json:"packet_size"`
  84. Streams int `json:"streams"`
  85. Duration int `json:"duration"`
  86. StartTime time.Time `json:"start_time"`
  87. ElapsedSec float64 `json:"elapsed_sec"`
  88. CurrentSample *LiveSample `json:"current_sample,omitempty"`
  89. RecentSamples []*LiveSample `json:"recent_samples,omitempty"`
  90. Summary *TestSummary `json:"summary,omitempty"`
  91. Error string `json:"error,omitempty"`
  92. }
  93. // ServerStatus provides metrics and status for the local BwTest server daemon
  94. type ServerStatus struct {
  95. Enabled bool `json:"enabled"`
  96. Port int `json:"port"`
  97. Running bool `json:"running"`
  98. ActiveSessions int `json:"active_sessions"`
  99. TotalSessionsServed int64 `json:"total_sessions_served"`
  100. TotalBytesProcessed int64 `json:"total_bytes_processed"`
  101. Error string `json:"error,omitempty"`
  102. }
  103. // HistoricalTestRecord stores past test results for review
  104. type HistoricalTestRecord struct {
  105. ID string `json:"id"`
  106. Target string `json:"target"`
  107. Protocol string `json:"protocol"`
  108. Direction string `json:"direction"`
  109. Timestamp time.Time `json:"timestamp"`
  110. Summary *TestSummary `json:"summary"`
  111. }