sigmoid strat placeholder
Juan Batiz-Benet committed
Aug 20, 2014 at 18:29 UTC
e658ade6002ffba1beb52c9441018c0c8541abe2
2 files changed
+37
bitswap/strategy.go
new
+20
@@ -0,0 +1,20 @@
1
+package bitswap
2
+
3
+import (
4
+ "math"
5
+)
6
+
7
+func probabilitySend(ratio float64) float64 {
8
+ x := 1 + math.Exp(6-3*ratio)
9
+ y := 1 / x
10
+ return 1 - y
11
+}
12
+
13
+type debtRatio struct {
14
+ BytesSent uint64
15
+ BytesRecv uint64
16
+}
17
+
18
+func (dr *debtRatio) Value() float64 {
19
+ return float64(dr.BytesSent) / float64(dr.BytesRecv+1)
20
+}
bitswap/strategy_test.go
new
+17
@@ -0,0 +1,17 @@
1
+package bitswap
2
+
3
+import (
4
+ "testing"
5
+)
6
+
7
+func TestProbabilitySendDecreasesAsRatioIncreases(t *testing.T) {
8
+ grateful := debtRatio{BytesSent: 0, BytesRecv: 10000}
9
+ pWhenGrateful := probabilitySend(grateful.Value())
10
+
11
+ abused := debtRatio{BytesSent: 10000, BytesRecv: 0}
12
+ pWhenAbused := probabilitySend(abused.Value())
13
+
14
+ if pWhenGrateful < pWhenAbused {
15
+ t.Fail()
16
+ }
17
+}