Skip to content

cHttpServerStatistics Performance Statistics

Overview

cHttpServerStatistics is the server-level performance statistics container for HttpServer, implementing zero-overhead increment through Public Long fields — no Dictionary lookups, no Property calls, suitable for high-frequency hot path instrumentation.

The globally unique instance is created in cHttpServer.Class_Initialize and accessible via two paths:

vb
' Method 1: Via Server object (available throughout server lifecycle)
Server.Statistics.TotalRequests

' Method 2: Via request context (more natural in controllers/middleware)
ctx.Statistics.TotalRequests

Both point to the same object instance. ctx.Statistics only performs a Set .Statistics = Statistics reference assignment in ProcessHttpRequest, with no additional overhead.

Lifecycle Note: Statistics is a server-level singleton. Neither cHttpServerContext.ReleaseRequest nor Release releases this reference.


Field Overview

Request Statistics

FieldTypeDescription
TotalRequestsLongCumulative total requests (including normal and exceptional)
GetRequestsLongGET request count
PostRequestsLongPOST request count
PutRequestsLongPUT request count
DeleteRequestsLongDELETE request count
OptionsRequestsLongOPTIONS request count
HeadRequestsLongHEAD request count
PatchRequestsLongPATCH request count
OtherRequestsLongRequest count for methods other than above

Method classification is done internally by IncrementByMethod, based on cHttpServerRequest.EnumRequestMethod enum values:

Enum ValueConstant NameIncremented Field
1ReqPostPostRequests
2ReqGetGetRequests
3ReqPutPutRequests
4ReqDeleteDeleteRequests
5ReqOptionsOptionsRequests
6HeadHeadRequests
7PatchPatchRequests
OtherOtherRequests

Status Code Statistics

FieldTypeDescription
Status1xxLong1xx response count (Informational)
Status2xxLong2xx response count (Success)
Status3xxLong3xx response count (Redirection)
Status4xxLong4xx response count (Client Error)
Status5xxLong5xx response count (Server Error)

Status code range classification is done internally by IncrementByStatusCode, grouping by the hundreds digit of the actual status code.

Prerequisite: cHttpServerResponse.StatusCode field is assigned in SendHeader. If the response doesn't reach SendHeader (e.g., connection already closed), StatusCode is 0 and not counted in any range.

Connection Statistics

FieldTypeDescription
TotalConnectionsAcceptedLongCumulative total connections accepted
RejectedConnectionsLongConnections rejected due to MaxConnections limit
PeakConnectionsLongPeak concurrent connections
IdleConnectionsCleanedLongConnections cleaned by CleanupIdleConnections due to idle timeout

Error Statistics

FieldTypeDescription
RequestErrorsLongRequest processing exceptions (EH path in ProcessHttpRequest)
RequestSizeRejectedLong413 Payload Too Large count
SSEEntryErrorsLongSSE Entry failure count (reserved field, not instrumented in current version)

Traffic Statistics

FieldTypeDescription
TotalBytesReceivedLongCumulative bytes received
TotalBytesSentLongCumulative bytes sent

Current Status: Fields are reserved but not yet instrumented in cClientCallback.OnDataArrival and cHttpServerResponse.SendBodyByte. Precise byte counting in high-traffic scenarios may impact performance; enable as needed.

SSE Statistics

FieldTypeDescription
SSEConnectionsAcceptedLongTotal SSE connections

Session Statistics

FieldTypeDescription
TotalSessionsCreatedLongCumulative total sessions created (only valid sessions with Session.HasID)

Time Statistics

FieldTypeDescription
StartTimeDateServer start time (assigned Now in Start method)

Computed Properties

UptimeSeconds

Server uptime in seconds.

vb
Public Property Get UptimeSeconds() As Long

Returns 0 if StartTime = 0 (not started).

AverageQPS

Average requests per second.

vb
Public Property Get AverageQPS() As Double

Calculation: TotalRequests / UptimeSeconds. Returns 0 when uptime is 0.


Friend Methods

The following methods are for internal cHttpServer use; external code does not need to call them directly.

IncrementByMethod

Increment the corresponding counter by request method enum value.

vb
Friend Sub IncrementByMethod(ByVal MethodValue As Long)

See the enum value table in the "Request Statistics" section above for mapping.

IncrementByStatusCode

Increment the corresponding range counter by actual status code.

vb
Friend Sub IncrementByStatusCode(ByVal StatusCode As Long)

Divides StatusCode \ 100 to get the hundreds digit, mapping to Status1xx~Status5xx.

UpdatePeakConnections

Update peak connections (only updates when current value exceeds historical peak).

vb
Friend Sub UpdatePeakConnections(ByVal CurrentCount As Long)

Reset Method

Reset all statistics counters to 0, StartTime reset to Now.

vb
Public Sub Reset()

Example:

vb
' Reset statistics daily at midnight
Server.Statistics.Reset

Instrumentation Location Reference

Trigger LocationStatistics FieldsDescription
ConnectionRequest eventTotalConnectionsAccepted, PeakConnections, RejectedConnectionsOn new connection
Start methodStartTime = NowRecord time baseline on server start
413 path (request body exceeded)RequestSizeRejectedOnDataArrival detects limit exceeded
SSE Entry pathSSEConnectionsAcceptedWhen SSE long connection established
After Router.EntryTotalRequests, IncrementByMethod, IncrementByStatusCodeStatistics after routing completes
Session.HasID is TrueTotalSessionsCreatedWhen valid Session created
EH error handling pathRequestErrors, TotalRequestsExceptions also counted in TotalRequests
CleanupIdleConnectionsIdleConnectionsCleanedOn timer or manual cleanup

Usage Examples

Monitoring Endpoint

Expose a statistics API endpoint in a controller:

vb
' GET /api/stats
Public Sub GetStats(ctx As cHttpServerContext)
    Dim s As cHttpServerStatistics
    Set s = ctx.Statistics
    
    Dim result As New Scripting.Dictionary
    ' Request metrics
    result("total_requests") = s.TotalRequests
    result("get_requests") = s.GetRequests
    result("post_requests") = s.PostRequests
    
    ' Status code distribution
    result("status_2xx") = s.Status2xx
    result("status_4xx") = s.Status4xx
    result("status_5xx") = s.Status5xx
    
    ' Connection metrics
    result("current_connections") = ctx.Server.Parent.ConnectionCount
    result("peak_connections") = s.PeakConnections
    result("rejected_connections") = s.RejectedConnections
    
    ' Performance metrics
    result("uptime_seconds") = s.UptimeSeconds
    result("average_qps") = s.AverageQPS
    result("request_errors") = s.RequestErrors
    
    ctx.Response.Json result, 0, "OK"
End Sub

Error Rate Alerting

vb
' Check error rate in middleware
Public Sub ErrorRateCheck(ctx As cHttpServerContext)
    Dim s As cHttpServerStatistics
    Set s = ctx.Statistics
    
    If s.TotalRequests > 100 Then
        Dim errorRate As Double
        errorRate = (s.Status5xx + s.RequestErrors) / s.TotalRequests
        
        If errorRate > 0.1 Then
            ' 5xx + exceptions exceed 10%, log alert
            Call WriteAlertLog("Error rate too high: " & Format(errorRate, "0.00%"))
        End If
    End If
End Sub

Runtime Dashboard

vb
Public Sub Dashboard(ctx As cHttpServerContext)
    Dim s As cHttpServerStatistics
    Set s = ctx.Statistics
    
    Dim html As String
    html = "<h1>Server Status</h1>"
    html = html & "<p>Uptime: " & s.UptimeSeconds & " seconds</p>"
    html = html & "<p>Total Requests: " & s.TotalRequests & "</p>"
    html = html & "<p>Average QPS: " & Format(s.AverageQPS, "0.00") & "</p>"
    html = html & "<p>Peak Connections: " & s.PeakConnections & "</p>"
    
    ctx.Response.Html html
End Sub

Long Overflow Considerations

VB6 Long type is a 32-bit signed integer with an upper limit of 2,147,483,647.

ScenarioQPSOverflow Time
Low traffic10~6.8 years
Medium traffic100~249 days
High traffic1,000~24.9 days
Very high traffic10,000~2.5 days

Recommendations:

  • In high-traffic scenarios (QPS > 1000), periodically call Reset or record a snapshot when approaching the threshold and then reset
  • When reading from the monitoring endpoint, first check if TotalRequests is approaching the limit, and proactively alert
  • For longer accumulation periods, maintain a Double type accumulator externally, periodically reading increments from TotalRequests

Dependencies

cHttpServer

  ├── Public Statistics As cHttpServerStatistics    ← Server holds instance
  │     ├── Public Long fields × 20+
  │     ├── Property Get UptimeSeconds / AverageQPS
  │     ├── Friend Sub IncrementByMethod / IncrementByStatusCode / UpdatePeakConnections
  │     └── Public Sub Reset

  └── ProcessHttpRequest
        └── ctx.Statistics ──→ Same instance reference

cHttpServerResponse adds Public StatusCode As Long field, assigned in SendHeader, providing data source for IncrementByStatusCode.


Last Updated: 2026-06-13

VB6 and LOGO copyright of Microsoft Corporation