Skip to content

Commit

Permalink
adds total host count to metric struct; closes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed Oct 27, 2019
1 parent 64eaf92 commit a997811
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pkg/analysis/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Metric struct {
// protocols that exhibit similar reliability. Finally, note that in pure gossip approaches, RMR is closely related with \
// the protocol fanout, as it tends to fanout−1.
RelativeMessageRedundancy float32 `json:"relativeMessageRedundancy,omitempty"`
// TotalHostCount is the toal count of hosts who received this message
TotalHostCount uint `json:"totalCount,omitempty"`
}

// MessageLog is a log output by the host that contains important pubsub metric data
Expand Down
8 changes: 4 additions & 4 deletions pkg/analysis/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func buildMetricsFromSortedMessageLogs(sortedMessageLogs []*types.MessageLog) (*
return nil, err
}

metric.RelativeMessageRedundancy, err = calcRMR(sortedMessageLogs)
metric.RelativeMessageRedundancy, metric.TotalHostCount, err = calcRMRAndTotalCount(sortedMessageLogs)
if err != nil {
logger.Errorf("err calculating rmr for msg %s:\n%v", metric.MessageID, err)
return nil, err
Expand All @@ -156,13 +156,13 @@ func calcTotalNanoTime(sortedMessageLogs []*types.MessageLog) (uint64, error) {
return uint64(sortedMessageLogs[len(sortedMessageLogs)-1].NanoTime - sortedMessageLogs[0].NanoTime), nil
}

func calcRMR(sortedMessageLogs []*types.MessageLog) (float32, error) {
func calcRMRAndTotalCount(sortedMessageLogs []*types.MessageLog) (float32, uint, error) {
uniqueHosts := countUniqueHosts(sortedMessageLogs)
if uniqueHosts == 0 || uniqueHosts == 1 {
return 0.0, errors.New("cannot calculate RMR with none or one host")
return 0.0, 0, errors.New("cannot calculate RMR with none or one host")
}

return (float32(len(sortedMessageLogs)) / (float32(uniqueHosts - 1))) - 1.0, nil
return (float32(len(sortedMessageLogs)) / (float32(uniqueHosts - 1))) - 1.0, uniqueHosts, nil
}

func calcLastDeliveryHop(sortedMessageLogs []*types.MessageLog) uint {
Expand Down
51 changes: 39 additions & 12 deletions pkg/analysis/utils_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,22 @@ func TestPrependString(t *testing.T) {
}
}

func TestCalcRMR(t *testing.T) {
type testCalcRMRAndTotalCountOut struct {
rmr float32
count uint
}

func TestCalcRMRAndTotalCount(t *testing.T) {
var (
result float32
err error
rmr float32
count uint
err error
)

for i, tt := range []struct {
in []*types.MessageLog
toErr bool
out float32
out testCalcRMRAndTotalCountOut
}{
{
in: []*types.MessageLog{
Expand All @@ -401,7 +407,10 @@ func TestCalcRMR(t *testing.T) {
},
},
toErr: true,
out: 0.0,
out: testCalcRMRAndTotalCountOut{
rmr: 0,
count: 1,
},
},
{
in: []*types.MessageLog{
Expand All @@ -415,7 +424,10 @@ func TestCalcRMR(t *testing.T) {
},
},
toErr: false,
out: 1.0,
out: testCalcRMRAndTotalCountOut{
rmr: 1.0,
count: 2,
},
},
{
in: []*types.MessageLog{
Expand All @@ -437,11 +449,14 @@ func TestCalcRMR(t *testing.T) {
},
},
toErr: false,
out: 3.0,
out: testCalcRMRAndTotalCountOut{
rmr: 3.0,
count: 2,
},
},
} {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
result, err = calcRMR(tt.in)
rmr, count, err = calcRMRAndTotalCount(tt.in)

if err != nil {
if !tt.toErr {
Expand All @@ -450,12 +465,15 @@ func TestCalcRMR(t *testing.T) {
} else {
if tt.toErr {
t.Fatal("expected err but received none")
} else {
if rmr != tt.out.rmr {
t.Errorf("want rmr %v; got %v", tt.out.rmr, rmr)
}
if count != tt.out.count {
t.Errorf("want count %v; got %v", tt.out.count, count)
}
}
}

if result != tt.out {
t.Errorf("want %v; got %v", tt.out, result)
}
})
}
}
Expand Down Expand Up @@ -930,6 +948,7 @@ func TestBuildMetricsFromSortedMessageLogs(t *testing.T) {
out: &types.Metric{
MessageID: "foo",
OriginatorHostID: "1",
TotalHostCount: 2,
TotalNanoTime: 0,
LastDeliveryHop: 1,
RelativeMessageRedundancy: 0,
Expand Down Expand Up @@ -972,6 +991,7 @@ func TestBuildMetricsFromSortedMessageLogs(t *testing.T) {
out: &types.Metric{
MessageID: "foo",
OriginatorHostID: "1",
TotalHostCount: 5,
TotalNanoTime: 4,
LastDeliveryHop: 4,
RelativeMessageRedundancy: (5.0 / (5.0 - 1.0)) - 1.0,
Expand Down Expand Up @@ -1040,6 +1060,7 @@ func TestBuildMetricsFromSortedMessageLogs(t *testing.T) {
OriginatorHostID: "1",
TotalNanoTime: 7,
LastDeliveryHop: 5,
TotalHostCount: 9,
RelativeMessageRedundancy: (9.0 / (9.0 - 1.0)) - 1.0,
},
},
Expand Down Expand Up @@ -1176,20 +1197,23 @@ func TestBuildMetricsFromMessageLogs(t *testing.T) {
},
out: []*types.Metric{
&types.Metric{
TotalHostCount: 2,
MessageID: "1",
OriginatorHostID: "1",
TotalNanoTime: 0,
LastDeliveryHop: 1,
RelativeMessageRedundancy: 0,
},
&types.Metric{
TotalHostCount: 5,
MessageID: "2",
OriginatorHostID: "1",
TotalNanoTime: 4,
LastDeliveryHop: 4,
RelativeMessageRedundancy: (5.0 / (5.0 - 1.0)) - 1.0,
},
&types.Metric{
TotalHostCount: 9,
MessageID: "3",
OriginatorHostID: "1",
TotalNanoTime: 7,
Expand Down Expand Up @@ -1352,16 +1376,19 @@ func TestBuildMetricsFromMessageLogsGroups(t *testing.T) {
OriginatorHostID: "1",
TotalNanoTime: 0,
LastDeliveryHop: 1,
TotalHostCount: 2,
RelativeMessageRedundancy: 0,
},
&types.Metric{
TotalHostCount: 5,
MessageID: "2",
OriginatorHostID: "1",
TotalNanoTime: 4,
LastDeliveryHop: 4,
RelativeMessageRedundancy: (5.0 / (5.0 - 1.0)) - 1.0,
},
&types.Metric{
TotalHostCount: 9,
MessageID: "3",
OriginatorHostID: "1",
TotalNanoTime: 7,
Expand Down

0 comments on commit a997811

Please sign in to comment.