Documentation
¶
Overview ¶
Package wsstat measures the latency of WebSocket connections. It wraps the gorilla/websocket package and includes latency measurements in the Result struct.
Package wsstat measures the latency of WebSocket connections. It wraps the gorilla/websocket package and includes latency measurements in the Result struct.
Index ¶
- type CertificateDetails
- type Option
- type Result
- func MeasureLatency(targetURL *url.URL, msg string, customHeaders http.Header) (*Result, []byte, error)
- func MeasureLatencyBurst(targetURL *url.URL, msgs []string, customHeaders http.Header) (*Result, []string, error)
- func MeasureLatencyBurstWithContext(ctx context.Context, targetURL *url.URL, msgs []string, ...) (*Result, []string, error)
- func MeasureLatencyJSON(targetURL *url.URL, v any, customHeaders http.Header) (*Result, any, error)
- func MeasureLatencyJSONBurst(targetURL *url.URL, v []any, customHeaders http.Header) (*Result, []any, error)
- func MeasureLatencyJSONBurstWithContext(ctx context.Context, targetURL *url.URL, v []any, customHeaders http.Header, ...) (*Result, []any, error)
- func MeasureLatencyPing(targetURL *url.URL, customHeaders http.Header) (*Result, error)
- func MeasureLatencyPingBurst(targetURL *url.URL, pingCount int, customHeaders http.Header) (*Result, error)
- func MeasureLatencyPingBurstWithContext(ctx context.Context, targetURL *url.URL, pingCount int, ...) (*Result, error)
- type Subscription
- type SubscriptionMessage
- type SubscriptionOptions
- type SubscriptionStats
- type WSStat
- func (ws *WSStat) Close()
- func (ws *WSStat) Dial(targetURL *url.URL, customHeaders http.Header) error
- func (ws *WSStat) ExtractResult() *Result
- func (ws *WSStat) OneHitMessage(messageType int, data []byte) ([]byte, error)
- func (ws *WSStat) OneHitMessageJSON(v any) (any, error)
- func (ws *WSStat) PingPong() error
- func (ws *WSStat) ReadMessage() (int, []byte, error)
- func (ws *WSStat) ReadMessageJSON() (any, error)
- func (ws *WSStat) ReadPong() error
- func (ws *WSStat) Subscribe(ctx context.Context, opts SubscriptionOptions) (*Subscription, error)
- func (ws *WSStat) SubscribeOnce(ctx context.Context, opts SubscriptionOptions) (SubscriptionMessage, error)
- func (ws *WSStat) WriteMessage(messageType int, data []byte)
- func (ws *WSStat) WriteMessageJSON(v any)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CertificateDetails ¶
type CertificateDetails struct {
CommonName string
Issuer string
NotBefore time.Time
NotAfter time.Time
PublicKeyAlgorithm x509.PublicKeyAlgorithm
SignatureAlgorithm x509.SignatureAlgorithm
DNSNames []string
IPAddresses []net.IP
URIs []*url.URL
}
CertificateDetails holds details regarding a certificate.
type Option ¶
type Option func(*options)
Option configures a WSStat instance.
func WithBufferSize ¶
WithBufferSize sets the buffer size for read/write/pong channels.
func WithLogger ¶
WithLogger sets the logger for the WSStat instance.
func WithResolves ¶
WithResolves sets DNS resolution overrides for specific host:port combinations. Map key format: "host:port", value: "ip_address".
func WithTLSConfig ¶
WithTLSConfig sets the TLS configuration for the connection.
func WithTimeout ¶
WithTimeout sets the timeout used for dialing and read deadlines.
type Result ¶
type Result struct {
IPs []string // IP addresses of the WebSocket connection
URL *url.URL // URL of the WebSocket connection
RequestHeaders http.Header // Headers of the initial request
ResponseHeaders http.Header // Headers of the response
TLSState *tls.ConnectionState // State of the TLS connection
MessageCount int // Number of messages sent and received
// Subscription statistics captured when long-lived streams are active.
Subscriptions map[string]SubscriptionStats // Metrics by subscription ID
SubscriptionFirstEvent time.Duration // Time until first subscription event
SubscriptionLastEvent time.Duration // Time until last subscription event
// Duration of each phase of the connection
DNSLookup time.Duration // Time to resolve DNS
TCPConnection time.Duration // TCP connection establishment time
TLSHandshake time.Duration // Time to perform TLS handshake
WSHandshake time.Duration // Time to perform WebSocket handshake
MessageRTT time.Duration // Time to send message and receive response
// Cumulative durations over the connection timeline
DNSLookupDone time.Duration // Time to resolve DNS (might be redundant with DNSLookup)
TCPConnected time.Duration // Time until the TCP connection is established
TLSHandshakeDone time.Duration // Time until the TLS handshake is completed
WSHandshakeDone time.Duration // Time until the WS handshake is completed
FirstMessageResponse time.Duration // Time until the first message is received
TotalTime time.Duration // Total time from opening to closing the connection
}
Result holds durations of each phase of a WebSocket connection, cumulative durations over the connection timeline, and other relevant connection details.
func MeasureLatency ¶
func MeasureLatency( targetURL *url.URL, msg string, customHeaders http.Header, ) (*Result, []byte, error)
MeasureLatency is a wrapper around a one-hit usage of the WSStat instance. It establishes a WebSocket connection, sends a message, reads the response, and closes the connection. Note: sets all times in the Result object.
func MeasureLatencyBurst ¶
func MeasureLatencyBurst( targetURL *url.URL, msgs []string, customHeaders http.Header, ) (*Result, []string, error)
MeasureLatencyBurst is a convenience wrapper around the WSStat instance, used to measure the latency of a WebSocket connection with multiple messages sent in quick succession. It connects to the server, sends all messages, reads the responses, and closes the connection. Note: sets all times in the Result object, where the MessageRTT will be the mean round trip time of all messages sent.
func MeasureLatencyBurstWithContext ¶
func MeasureLatencyBurstWithContext( ctx context.Context, targetURL *url.URL, msgs []string, customHeaders http.Header, opts ...Option, ) (*Result, []string, error)
MeasureLatencyBurstWithContext measures latency with cancellation support
func MeasureLatencyJSON ¶
func MeasureLatencyJSON( targetURL *url.URL, v any, customHeaders http.Header, ) (*Result, any, error)
MeasureLatencyJSON is a wrapper around a one-hit usage of the WSStat instance. It establishes a WebSocket connection, sends a JSON message, reads the response, and closes the connection. Note: sets all times in the Result object.
func MeasureLatencyJSONBurst ¶
func MeasureLatencyJSONBurst( targetURL *url.URL, v []any, customHeaders http.Header, ) (*Result, []any, error)
MeasureLatencyJSONBurst is a convenience wrapper around the WSStat instance, used to measure the latency of a WebSocket connection with multiple messages sent in quick succession. It connects to the server, sends all JSON messages, reads the responses, and closes the connection. Note: sets all times in the Result object, where the MessageRTT will be the mean round trip time of all messages sent.
func MeasureLatencyJSONBurstWithContext ¶
func MeasureLatencyJSONBurstWithContext( ctx context.Context, targetURL *url.URL, v []any, customHeaders http.Header, opts ...Option, ) (*Result, []any, error)
MeasureLatencyJSONBurstWithContext measures latency with cancellation support
func MeasureLatencyPing ¶
MeasureLatencyPing is a convenience wrapper around a one-hit usage of the WSStat instance. It establishes a WebSocket connection, sends a ping message, awaits the pong response, and closes the connection. Note: sets all times in the Result object.
func MeasureLatencyPingBurst ¶
func MeasureLatencyPingBurst( targetURL *url.URL, pingCount int, customHeaders http.Header, ) (*Result, error)
MeasureLatencyPingBurst is a convenience wrapper around a one-hit usage of the WSStat instance. It establishes a WebSocket connection, sends ping messages according to pingCount, awaits the pong responses, and closes the connection. Note: sets all times in the Result object.
func MeasureLatencyPingBurstWithContext ¶
func MeasureLatencyPingBurstWithContext( ctx context.Context, targetURL *url.URL, pingCount int, customHeaders http.Header, opts ...Option, ) (*Result, error)
MeasureLatencyPingBurstWithContext measures latency with cancellation support
func (*Result) CertificateDetails ¶
func (r *Result) CertificateDetails() []CertificateDetails
CertificateDetails returns a slice of CertificateDetails for each certificate in the TLS connection.
type Subscription ¶
type Subscription struct {
ID string
// contains filtered or unexported fields
}
Subscription captures a long-lived stream registered through Subscribe. Counters are updated atomically by the WSStat instance.
func (*Subscription) ByteCount ¶
func (s *Subscription) ByteCount() uint64
ByteCount reports the aggregate payload size delivered to the subscription.
func (*Subscription) Cancel ¶
func (s *Subscription) Cancel()
Cancel stops the subscription and prevents further deliveries.
func (*Subscription) Done ¶
func (s *Subscription) Done() <-chan struct{}
Done returns a channel that closes once the subscription is fully torn down.
func (*Subscription) MessageCount ¶
func (s *Subscription) MessageCount() uint64
MessageCount reports the total number of messages delivered to the subscription.
func (*Subscription) Unsubscribe ¶
func (s *Subscription) Unsubscribe()
Unsubscribe is an alias for Cancel and preserves semantic clarity for callers.
func (*Subscription) Updates ¶
func (s *Subscription) Updates() <-chan SubscriptionMessage
Updates exposes the buffered stream of subscription messages.
type SubscriptionMessage ¶
type SubscriptionMessage struct {
MessageType int
Data []byte
Decoded any
Received time.Time
Err error
Size int
}
SubscriptionMessage represents a single frame delivered to a subscription consumer.
type SubscriptionOptions ¶
type SubscriptionOptions struct {
// ID can be provided to preassign a human-readable identifier. If empty, WSStat
// allocates an incremental identifier.
ID string
// MessageType and Payload describe the initial frame sent to initiate the subscription.
MessageType int
Payload []byte
// Buffer controls the per-subscription delivery queue length. Zero implies the default.
Buffer int
// contains filtered or unexported fields
}
SubscriptionOptions configures how WSStat establishes and demultiplexes a subscription.
type SubscriptionStats ¶
type SubscriptionStats struct {
FirstEvent time.Duration
LastEvent time.Duration
MessageCount uint64
ByteCount uint64
MeanInterArrival time.Duration
Error error
}
SubscriptionStats snapshots per-subscription metrics for reporting through Result.
type WSStat ¶
type WSStat struct {
// contains filtered or unexported fields
}
WSStat wraps the gorilla/websocket package with latency measuring capabilities.
func New ¶
New creates and returns a new WSStat instance. To adjust channel buffer size or timeouts, use options. If not provided, package defaults are used for compatibility.
func (*WSStat) Close ¶
func (ws *WSStat) Close()
Close closes the WebSocket connection and cleans up the WSStat instance. Sets result times: CloseDone
func (*WSStat) Dial ¶
Dial establishes a new WebSocket connection using the custom dialer defined in this package. If required, specify custom headers to merge with the default headers. Sets times: dialStart, wsHandshakeDone
func (*WSStat) ExtractResult ¶
ExtractResult calculate the current results and returns a copy of the Result object.
func (*WSStat) OneHitMessage ¶
OneHitMessage sends a single message through the WebSocket connection, and waits for the response. Note: this function assumes that the response received is the response to the sent message, make sure to only run this function sequentially to avoid unexpected behavior. Sets result times: MessageReads, MessageWrites
func (*WSStat) OneHitMessageJSON ¶
OneHitMessageJSON sends a single JSON message through the WebSocket connection, and waits for the response. Note: this function assumes that the response received is the response to the sent message, make sure to only run this function sequentially to avoid unexpected behavior. Sets result times: MessageReads, MessageWrites
func (*WSStat) PingPong ¶
PingPong sends a ping message through the WebSocket connection and awaits the pong. Note: this function assumes that the pong received is the response to the sent message, make sure to only run this function sequentially to avoid unexpected behavior. Sets result times: MessageReads, MessageWrites
func (*WSStat) ReadMessage ¶
ReadMessage reads a message from the WebSocket connection and measures the round-trip time. If an error occurs, it will be returned. Sets time: MessageReads
func (*WSStat) ReadMessageJSON ¶
ReadMessageJSON reads a message from the WebSocket connection and measures the round-trip time. Sets time: MessageReads
func (*WSStat) ReadPong ¶
ReadPong reads a pong message from the WebSocket connection and measures the round-trip time. Sets time: MessageReads
func (*WSStat) Subscribe ¶
func (ws *WSStat) Subscribe(ctx context.Context, opts SubscriptionOptions) (*Subscription, error)
Subscribe registers a long-lived listener using the supplied options and context. The returned Subscription can be used to consume streamed frames until cancellation.
func (*WSStat) SubscribeOnce ¶
func (ws *WSStat) SubscribeOnce( ctx context.Context, opts SubscriptionOptions, ) (SubscriptionMessage, error)
SubscribeOnce registers a subscription and waits for the first delivered message before canceling the subscription. The returned message is a snapshot of the first delivery.
func (*WSStat) WriteMessage ¶
WriteMessage sends a message through the WebSocket connection. Sets time: MessageWrites
func (*WSStat) WriteMessageJSON ¶
WriteMessageJSON sends a message through the WebSocket connection. Sets time: MessageWrites
Directories
¶
| Path | Synopsis |
|---|---|
|
Package main provides examples of how to use the wsstat package.
|
Package main provides examples of how to use the wsstat package. |
|
cmd
|
|
|
wsstat
command
Package main implements the wsstat command-line tool for measuring WebSocket connection latency and streaming subscription events.
|
Package main implements the wsstat command-line tool for measuring WebSocket connection latency and streaming subscription events. |
|
internal
|
|
|
app
Package app provides a high-level client for measuring WebSocket latency and streaming subscription events.
|
Package app provides a high-level client for measuring WebSocket latency and streaming subscription events. |
|
app/color
Package color provides ANSI color support for terminal output.
|
Package color provides ANSI color support for terminal output. |