Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
fe33d5e6
Commit
fe33d5e6
authored
2 years ago
by
Jono Wenger
Committed by
Benjamin Wenger
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
XX-4284 / Add tests to histogram.go
parent
660365ed
No related branches found
Branches containing commit
No related tags found
Tags containing commit
3 merge requests
!510
Release
,
!425
XX-4284 / Add tests to histogram.go
,
!340
Project/channels
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cmix/attempts/histrogram.go
+14
-0
14 additions, 0 deletions
cmix/attempts/histrogram.go
cmix/attempts/histrogram_test.go
+89
-0
89 additions, 0 deletions
cmix/attempts/histrogram_test.go
with
103 additions
and
0 deletions
cmix/attempts/histrogram.go
+
14
−
0
View file @
fe33d5e6
...
...
@@ -18,11 +18,19 @@ const (
optimalAttemptsInitValue
=
-
1
)
// SendAttemptTracker tracks the number of attempts it took to send a cMix
// message in order to predict how many attempt are needed.
type
SendAttemptTracker
interface
{
// SubmitProbeAttempt feeds the number of attempts it took to send a cMix
// message into the tracker and updates the optimal number of attempts.
SubmitProbeAttempt
(
numAttemptsUntilSuccessful
int
)
// GetOptimalNumAttempts returns the number of optimal sends. If there is
// insufficient data to calculate, then ready is false.
GetOptimalNumAttempts
()
(
attempts
int
,
ready
bool
)
}
// sendAttempts tracks the number of attempts to send a cMix message.
type
sendAttempts
struct
{
optimalAttempts
*
int32
isFull
bool
...
...
@@ -31,6 +39,7 @@ type sendAttempts struct {
lock
sync
.
Mutex
}
// NewSendAttempts initialises a new SendAttemptTracker.
func
NewSendAttempts
()
SendAttemptTracker
{
optimalAttempts
:=
int32
(
optimalAttemptsInitValue
)
sa
:=
&
sendAttempts
{
...
...
@@ -43,6 +52,8 @@ func NewSendAttempts() SendAttemptTracker {
return
sa
}
// SubmitProbeAttempt feeds the number of attempts it took to send a cMix
// message into the tracker and updates the optimal number of attempts.
func
(
sa
*
sendAttempts
)
SubmitProbeAttempt
(
numAttemptsUntilSuccessful
int
)
{
sa
.
lock
.
Lock
()
defer
sa
.
lock
.
Unlock
()
...
...
@@ -58,6 +69,8 @@ func (sa *sendAttempts) SubmitProbeAttempt(numAttemptsUntilSuccessful int) {
sa
.
computeOptimalUnsafe
()
}
// GetOptimalNumAttempts returns the number of optimal sends. If there is
// insufficient data to calculate, then ready is false.
func
(
sa
*
sendAttempts
)
GetOptimalNumAttempts
()
(
attempts
int
,
ready
bool
)
{
optimalAttempts
:=
atomic
.
LoadInt32
(
sa
.
optimalAttempts
)
...
...
@@ -68,6 +81,7 @@ func (sa *sendAttempts) GetOptimalNumAttempts() (attempts int, ready bool) {
return
int
(
optimalAttempts
),
true
}
// computeOptimalUnsafe updates the optimal send attempts.
func
(
sa
*
sendAttempts
)
computeOptimalUnsafe
()
{
toCopy
:=
maxHistogramSize
if
!
sa
.
isFull
{
...
...
This diff is collapsed.
Click to expand it.
cmix/attempts/histrogram_test.go
0 → 100644
+
89
−
0
View file @
fe33d5e6
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package
attempts
import
(
"math/rand"
"reflect"
"testing"
)
// Tests that NewSendAttempts returns a new sendAttempts with the expected
// fields.
func
TestNewSendAttempts
(
t
*
testing
.
T
)
{
optimalAttempts
:=
int32
(
optimalAttemptsInitValue
)
expected
:=
&
sendAttempts
{
optimalAttempts
:
&
optimalAttempts
,
isFull
:
false
,
currentIndex
:
0
,
numAttempts
:
make
([]
int
,
maxHistogramSize
),
}
sa
:=
NewSendAttempts
()
if
!
reflect
.
DeepEqual
(
expected
,
sa
)
{
t
.
Errorf
(
"New SendAttemptTracker does not match expected."
+
"
\n
expected: %+v
\n
receivedL %+v"
,
expected
,
sa
)
}
}
// Tests that sendAttempts.SubmitProbeAttempt properly increments and stores the
// attempts.
func
Test_sendAttempts_SubmitProbeAttempt
(
t
*
testing
.
T
)
{
sa
:=
NewSendAttempts
()
.
(
*
sendAttempts
)
for
i
:=
0
;
i
<
maxHistogramSize
+
20
;
i
++
{
sa
.
SubmitProbeAttempt
(
i
)
if
sa
.
currentIndex
!=
(
i
+
1
)
%
maxHistogramSize
{
t
.
Errorf
(
"Incorrect currentIndex (%d).
\n
expected: %d
\n
received: %d"
,
i
,
(
i
+
1
)
%
maxHistogramSize
,
sa
.
currentIndex
)
}
else
if
sa
.
numAttempts
[
i
%
maxHistogramSize
]
!=
i
{
t
.
Errorf
(
"Incorrect numAttempts at %d.
\n
expected: %d
\n
received: %d"
,
i
,
i
,
sa
.
numAttempts
[
i
%
maxHistogramSize
])
}
else
if
i
>
maxHistogramSize
&&
!
sa
.
isFull
{
t
.
Errorf
(
"Should be marked full when numAttempts > %d."
,
maxHistogramSize
)
}
}
}
// Tests sendAttempts.GetOptimalNumAttempts returns numbers close to 70% of the
// average of attempts feeding in.
func
Test_sendAttempts_GetOptimalNumAttempts
(
t
*
testing
.
T
)
{
prng
:=
rand
.
New
(
rand
.
NewSource
(
42
))
sa
:=
NewSendAttempts
()
.
(
*
sendAttempts
)
attempts
,
ready
:=
sa
.
GetOptimalNumAttempts
()
if
ready
{
t
.
Errorf
(
"Marked ready when no attempts have been made."
)
}
else
if
attempts
!=
0
{
t
.
Errorf
(
"Incorrect number of attempt.
\n
expected: %d
\n
received: %d"
,
0
,
attempts
)
}
const
n
=
100
factor
:=
(
n
*
7
)
/
10
for
i
:=
0
;
i
<
500
;
i
++
{
sa
.
SubmitProbeAttempt
(
prng
.
Intn
(
n
))
attempts
,
ready
=
sa
.
GetOptimalNumAttempts
()
if
(
sa
.
currentIndex
<
minElements
&&
!
sa
.
isFull
)
&&
ready
{
t
.
Errorf
(
"Ready when less than %d attempts made (%d)."
,
minElements
,
i
)
}
else
if
sa
.
currentIndex
>=
minElements
{
if
!
ready
{
t
.
Errorf
(
"Not ready when more than %d attempts made (%d)."
,
minElements
,
i
)
}
else
if
attempts
<
factor
-
25
||
attempts
>
factor
+
25
{
t
.
Errorf
(
"Attempts is not close to average (%d)."
+
"
\n
average: %d
\n
attempts: %d"
,
i
,
factor
,
attempts
)
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment