forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GODRIVER-3095 Add moving STD to RTT Stats (mongodb#1845)
- Loading branch information
Showing
4 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (C) MongoDB, Inc. 2024-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package topology | ||
|
||
import ( | ||
"container/list" | ||
"math" | ||
"time" | ||
) | ||
|
||
func standardDeviationList(l *list.List) float64 { | ||
if l.Len() == 0 { | ||
return 0 | ||
} | ||
|
||
var mean, variance float64 | ||
count := 0.0 | ||
|
||
for el := l.Front(); el != nil; el = el.Next() { | ||
count++ | ||
sample := float64(el.Value.(time.Duration)) | ||
|
||
delta := sample - mean | ||
mean += delta / count | ||
variance += delta * (sample - mean) | ||
} | ||
|
||
return math.Sqrt(variance / count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (C) MongoDB, Inc. 2022-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package topology | ||
|
||
import ( | ||
"container/list" | ||
"testing" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/v2/internal/assert" | ||
) | ||
|
||
func TestStandardDeviationList_Duration(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data []time.Duration | ||
want float64 | ||
}{ | ||
{ | ||
name: "empty", | ||
data: []time.Duration{}, | ||
want: 0, | ||
}, | ||
{ | ||
name: "multiple", | ||
data: []time.Duration{ | ||
time.Millisecond, | ||
2 * time.Millisecond, | ||
time.Microsecond, | ||
}, | ||
want: 816088.36667497, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
l := list.New() | ||
for _, d := range test.data { | ||
l.PushBack(d) | ||
} | ||
|
||
got := standardDeviationList(l) | ||
|
||
assert.InDelta(t, test.want, got, 1e-6) | ||
}) | ||
} | ||
} |