mattermost/server/public/model/metrics_test.go
Agniva De Sarker 4ec4b4d525
MM-61886: Add actionable page navigation metrics (#29332)
Page load is one of the metrics that we track and present
to MLT. However, in its current form, it is not very
actionable because it also contains the network latency.

We split the whole metric into these parts:
startTime
|
responseStart = TTFB
|
responseEnd = TTLB
|
domInteractive = Start of processing phase
|
loadEventEnd = Load complete

This gives us better visibility into exactly
which phase in the load process is slow.

I have experimented with other metrics like
- domContentLoadedEventStart
- domContentLoadedEventEnd
- domComplete

and observed that they do not have sufficient
gaps in the timespan to have any relevance.

Additionally, I have moved TTFB from being a
web vitals metric to being tracked from the performance
metrics to remain consistent with the other navigation
metrics measured.

Lastly, I took this chance to improve some of the
validation errors that we threw to include more
context into the input that was passed and why
does it fail.

This also meant that I had to change the tests
to check for error strings rather than direct
errors which is a bad thing, but I don't think
it's worth the effort trying to have named error
variables for all of them.

https://mattermost.atlassian.net/browse/MM-61886

```release-note
NONE
```
2024-11-29 11:24:35 +05:30

77 lines
1.8 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestPerformanceReport_IsValid(t *testing.T) {
outdatedTimestamp := time.Now().Add(-6 * time.Minute).UnixMilli()
tests := []struct {
name string
report *PerformanceReport
expected string
}{
{
name: "ValidReport",
report: &PerformanceReport{
Version: "0.1.0",
Labels: map[string]string{"platform": "linux"},
Start: float64(time.Now().UnixMilli() - 10000),
End: float64(time.Now().UnixMilli()),
},
expected: "",
},
{
name: "NilReport",
report: nil,
expected: "the report is nil",
},
{
name: "UnsupportedVersion",
report: &PerformanceReport{
Version: "2.0.0",
Labels: map[string]string{"platform": "linux"},
Start: float64(time.Now().UnixMilli() - 10000),
End: float64(time.Now().UnixMilli()),
},
expected: "report version is not supported:",
},
{
name: "ErroneousTimestamps",
report: &PerformanceReport{
Version: "0.1.0",
Labels: map[string]string{"platform": "linux"},
Start: float64(time.Now().UnixMilli()),
End: float64(time.Now().Add(-1 * time.Hour).UnixMilli()),
},
expected: "report timestamps are erroneous",
},
{
name: "OutdatedReport",
report: &PerformanceReport{
Version: "0.1.0",
Labels: map[string]string{"platform": "linux"},
Start: float64(time.Now().Add(-7 * time.Minute).UnixMilli()),
End: float64(outdatedTimestamp),
},
expected: "report is outdated:",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.report.IsValid()
if tt.expected != "" {
require.Contains(t, err.Error(), tt.expected)
return
}
require.NoError(t, err)
})
}
}