mirror of
https://github.com/tendermint/tendermint.git
synced 2026-05-02 05:15:44 +00:00
working basic report generator
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package payload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
func CalculateUnpaddedSizeBytes() (int, error) {
|
||||
func init() {
|
||||
p := &Payload{
|
||||
Time: timestamppb.Now(),
|
||||
Connections: math.MaxUint64,
|
||||
@@ -17,7 +20,57 @@ func CalculateUnpaddedSizeBytes() (int, error) {
|
||||
}
|
||||
b, err := proto.Marshal(p)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
panic(err)
|
||||
}
|
||||
return len(b), nil
|
||||
payloadSizeBytes = len(b)
|
||||
}
|
||||
|
||||
const keyPrefix = "a="
|
||||
|
||||
var payloadSizeBytes int
|
||||
|
||||
type Options struct {
|
||||
Conns uint64
|
||||
Rate uint64
|
||||
Size uint64
|
||||
}
|
||||
|
||||
func UnpaddedSizeBytes() int {
|
||||
return payloadSizeBytes
|
||||
}
|
||||
|
||||
func NewBytes(o Options) ([]byte, error) {
|
||||
if o.Size < uint64(UnpaddedSizeBytes()) {
|
||||
return nil, fmt.Errorf("configured size %d not large enough to fit unpadded transaction size %d", o.Size, UnpaddedSizeBytes())
|
||||
}
|
||||
p := &Payload{
|
||||
Time: timestamppb.Now(),
|
||||
Connections: o.Conns,
|
||||
Rate: o.Rate,
|
||||
Size: o.Size,
|
||||
Padding: make([]byte, o.Size-uint64(UnpaddedSizeBytes())),
|
||||
}
|
||||
_, err := rand.Read(p.Padding)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err := proto.Marshal(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// prepend a single key so that the kv store only ever stores a single
|
||||
// transaction instead of storing all tx and ballooning in size.
|
||||
return append([]byte(keyPrefix), b...), nil
|
||||
// return b, nil
|
||||
}
|
||||
|
||||
func FromBytes(b []byte) (*Payload, error) {
|
||||
p := &Payload{}
|
||||
tr := bytes.TrimPrefix(b, []byte(keyPrefix))
|
||||
err := proto.Unmarshal(tr, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
@@ -8,12 +8,25 @@ import (
|
||||
|
||||
const payloadSizeTarget = 1024 // 1kb
|
||||
|
||||
func TestCalculateSize(t *testing.T) {
|
||||
s, err := payload.CalculateUnpaddedSizeBytes()
|
||||
if err != nil {
|
||||
t.Fatalf("calculating unpadded size %s", err)
|
||||
}
|
||||
func TestSize(t *testing.T) {
|
||||
s := payload.UnpaddedSizeBytes()
|
||||
if s > payloadSizeTarget {
|
||||
t.Fatalf("unpadded payload size %d exceeds target %d", s, payloadSizeTarget)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
b, err := payload.NewBytes(payload.Options{
|
||||
Size: 1024,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("generating payload %s", err)
|
||||
}
|
||||
p, err := payload.FromBytes(b)
|
||||
if err != nil {
|
||||
t.Fatalf("reading payload %s", err)
|
||||
}
|
||||
if p.Size != 1024 {
|
||||
t.Fatalf("payload size value %d does not match expected %d", p.Size, 1024)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user