-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/calculating-consumption
- Loading branch information
Showing
32 changed files
with
6,176 additions
and
4,045 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,54 @@ | ||
package metric | ||
|
||
import "github.com/pactus-project/pactus/sync/bundle/message" | ||
|
||
type Counter struct { | ||
Bytes int64 | ||
Bundles int64 | ||
} | ||
|
||
type Metric struct { | ||
TotalInvalid Counter | ||
TotalSent Counter | ||
TotalReceived Counter | ||
MessageSent map[message.Type]*Counter | ||
MessageReceived map[message.Type]*Counter | ||
} | ||
|
||
func NewMetric() Metric { | ||
return Metric{ | ||
MessageSent: make(map[message.Type]*Counter), | ||
MessageReceived: make(map[message.Type]*Counter), | ||
} | ||
} | ||
|
||
func (m *Metric) UpdateSentMetric(msgType message.Type, bytes int64) { | ||
m.TotalSent.Bundles++ | ||
m.TotalSent.Bytes += bytes | ||
|
||
_, ok := m.MessageSent[msgType] | ||
if !ok { | ||
m.MessageSent[msgType] = &Counter{} | ||
} | ||
|
||
m.MessageSent[msgType].Bundles++ | ||
m.MessageSent[msgType].Bytes += bytes | ||
} | ||
|
||
func (m *Metric) UpdateReceivedMetric(msgType message.Type, bytes int64) { | ||
m.TotalReceived.Bundles++ | ||
m.TotalReceived.Bytes += bytes | ||
|
||
_, ok := m.MessageReceived[msgType] | ||
if !ok { | ||
m.MessageReceived[msgType] = &Counter{} | ||
} | ||
|
||
m.MessageReceived[msgType].Bundles++ | ||
m.MessageReceived[msgType].Bytes += bytes | ||
} | ||
|
||
func (m *Metric) UpdateInvalidMetric(bytes int64) { | ||
m.TotalInvalid.Bundles++ | ||
m.TotalInvalid.Bytes += bytes | ||
} |
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,46 @@ | ||
package metric | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pactus-project/pactus/sync/bundle/message" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpdateSentMetric(t *testing.T) { | ||
metric := NewMetric() | ||
|
||
testMsgType := message.Type(1) | ||
|
||
metric.UpdateSentMetric(testMsgType, 100) | ||
|
||
assert.Equal(t, int64(1), metric.TotalSent.Bundles) | ||
assert.Equal(t, int64(100), metric.TotalSent.Bytes) | ||
|
||
assert.NotNil(t, metric.MessageSent[testMsgType]) | ||
assert.Equal(t, int64(1), metric.MessageSent[testMsgType].Bundles) | ||
assert.Equal(t, int64(100), metric.MessageSent[testMsgType].Bytes) | ||
} | ||
|
||
func TestUpdateReceivedMetric(t *testing.T) { | ||
metric := NewMetric() | ||
|
||
testMsgType := message.Type(2) | ||
|
||
metric.UpdateReceivedMetric(testMsgType, 200) | ||
|
||
assert.Equal(t, int64(1), metric.TotalReceived.Bundles) | ||
assert.Equal(t, int64(200), metric.TotalReceived.Bytes) | ||
|
||
assert.NotNil(t, metric.MessageReceived[testMsgType]) | ||
assert.Equal(t, int64(1), metric.MessageReceived[testMsgType].Bundles) | ||
assert.Equal(t, int64(200), metric.MessageReceived[testMsgType].Bytes) | ||
} | ||
|
||
func TestUpdateInvalidMetric(t *testing.T) { | ||
metric := NewMetric() | ||
|
||
metric.UpdateInvalidMetric(123) | ||
assert.Equal(t, int64(1), metric.TotalInvalid.Bundles) | ||
assert.Equal(t, int64(123), metric.TotalInvalid.Bytes) | ||
} |
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
Oops, something went wrong.