Skip to content

Commit

Permalink
xdr and support/db: Minor benchmark code quality improvements (stella…
Browse files Browse the repository at this point in the history
…r#4093)

* Minor benchmark code quality improvements

* Address review feedback
  • Loading branch information
2opremio authored Nov 23, 2021
1 parent da266e0 commit 17a2e98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 14 additions & 1 deletion benchmarks/xdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func BenchmarkXDRUnmarshalWithReflection(b *testing.B) {
r bytes.Reader
te xdr.TransactionEnvelope
)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
r.Reset(input)
_, _ = xdr3.Unmarshal(&r, &te)
Expand All @@ -49,6 +50,7 @@ func BenchmarkXDRUnmarshalWithReflection(b *testing.B) {

func BenchmarkXDRUnmarshal(b *testing.B) {
var te xdr.TransactionEnvelope
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = te.UnmarshalBinary(input)
}
Expand All @@ -59,25 +61,29 @@ func BenchmarkGXDRUnmarshal(b *testing.B) {
te gxdr.TransactionEnvelope
r bytes.Reader
)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
r.Reset(input)
te.XdrMarshal(&goxdr.XdrIn{In: &r}, "")
}
}

func BenchmarkXDRMarshalWithReflection(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = xdr3.Marshal(&bytes.Buffer{}, xdrInput)
}
}

func BenchmarkXDRMarshal(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = xdrInput.MarshalBinary()
}
}

func BenchmarkXDRMarshalWithEncodingBuffer(b *testing.B) {
b.ReportAllocs()
e := xdr.NewEncodingBuffer()
for i := 0; i < b.N; i++ {
_, _ = e.UnsafeMarshalBinary(xdrInput)
Expand All @@ -86,47 +92,53 @@ func BenchmarkXDRMarshalWithEncodingBuffer(b *testing.B) {

func BenchmarkGXDRMarshal(b *testing.B) {
var output bytes.Buffer
// Benchmark.
b.ReportAllocs()
for i := 0; i < b.N; i++ {
output.Reset()
gxdrInput.XdrMarshal(&goxdr.XdrOut{Out: &output}, "")
}
}

func BenchmarkXDRMarshalHex(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = xdr.MarshalHex(xdrInput)
}
}

func BenchmarkXDRMarshalHexWithEncodingBuffer(b *testing.B) {
b.ReportAllocs()
e := xdr.NewEncodingBuffer()
for i := 0; i < b.N; i++ {
_, _ = e.MarshalHex(xdrInput)
}
}

func BenchmarkXDRUnsafeMarshalHexWithEncodingBuffer(b *testing.B) {
b.ReportAllocs()
e := xdr.NewEncodingBuffer()
for i := 0; i < b.N; i++ {
_, _ = e.UnsafeMarshalHex(xdrInput)
}
}

func BenchmarkXDRMarshalBase64(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = xdr.MarshalBase64(xdrInput)
}
}

func BenchmarkXDRMarshalBase64WithEncodingBuffer(b *testing.B) {
b.ReportAllocs()
e := xdr.NewEncodingBuffer()
for i := 0; i < b.N; i++ {
_, _ = e.MarshalBase64(xdrInput)
}
}

func BenchmarkXDRUnsafeMarshalBase64WithEncodingBuffer(b *testing.B) {
b.ReportAllocs()
e := xdr.NewEncodingBuffer()
for i := 0; i < b.N; i++ {
_, _ = e.UnsafeMarshalBase64(xdrInput)
Expand Down Expand Up @@ -179,6 +191,7 @@ var ledgerKeys = []xdr.LedgerKey{
}

func BenchmarkXDRMarshalCompress(b *testing.B) {
b.ReportAllocs()
e := xdr.NewEncodingBuffer()
for i := 0; i < b.N; i++ {
for _, lk := range ledgerKeys {
Expand Down
6 changes: 4 additions & 2 deletions support/db/batch_insert_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type invalidHungerRow struct {
}

func BenchmarkBatchInsertBuilder(b *testing.B) {
b.StopTimer()
// In order to show SQL queries
// log.SetLevel(logrus.DebugLevel)
db := dbtest.Postgres(b).Load(testSchema)
Expand All @@ -35,7 +34,9 @@ func BenchmarkBatchInsertBuilder(b *testing.B) {
Table: sess.GetTable("people"),
MaxBatchSize: maxBatchSize,
}
b.StartTimer()

// Do not count the test initialization
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < maxBatchSize; j++ {
err := insertBuilder.RowStruct(ctx, hungerRow{
Expand All @@ -47,6 +48,7 @@ func BenchmarkBatchInsertBuilder(b *testing.B) {
}
err := insertBuilder.Exec(ctx)

// Do not count the test ending
b.StopTimer()
assert.NoError(b, err)
var count []int
Expand Down

0 comments on commit 17a2e98

Please sign in to comment.