github.com/hamba/avro@v1.8.0/bench_test.go (about)

     1  package avro_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/hamba/avro"
     8  )
     9  
    10  type Superhero struct {
    11  	ID            int           `avro:"id"`
    12  	AffiliationID int           `avro:"affiliation_id"`
    13  	Name          string        `avro:"name"`
    14  	Life          float32       `avro:"life"`
    15  	Energy        float32       `avro:"energy"`
    16  	Powers        []*Superpower `avro:"powers"`
    17  }
    18  
    19  type Superpower struct {
    20  	ID      int     `avro:"id"`
    21  	Name    string  `avro:"name"`
    22  	Damage  float32 `avro:"damage"`
    23  	Energy  float32 `avro:"energy"`
    24  	Passive bool    `avro:"passive"`
    25  }
    26  
    27  type PartialSuperhero struct {
    28  	ID            int     `avro:"id"`
    29  	AffiliationID int     `avro:"affiliation_id"`
    30  	Name          string  `avro:"name"`
    31  	Life          float32 `avro:"life"`
    32  	Energy        float32 `avro:"energy"`
    33  }
    34  
    35  func BenchmarkSuperheroDecode(b *testing.B) {
    36  	data, err := ioutil.ReadFile("testdata/superhero.bin")
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  
    41  	schema, err := avro.ParseFiles("testdata/superhero.avsc")
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  
    46  	super := &Superhero{}
    47  
    48  	b.ReportAllocs()
    49  	b.ResetTimer()
    50  	for i := 0; i < b.N; i++ {
    51  		_ = avro.Unmarshal(schema, data, super)
    52  	}
    53  }
    54  
    55  func BenchmarkSuperheroEncode(b *testing.B) {
    56  	schema, err := avro.ParseFiles("testdata/superhero.avsc")
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  
    61  	super := &Superhero{
    62  		ID:            234765,
    63  		AffiliationID: 9867,
    64  		Name:          "Wolverine",
    65  		Life:          85.25,
    66  		Energy:        32.75,
    67  		Powers: []*Superpower{
    68  			{ID: 2345, Name: "Bone Claws", Damage: 5, Energy: 1.15, Passive: false},
    69  			{ID: 2346, Name: "Regeneration", Damage: -2, Energy: 0.55, Passive: true},
    70  			{ID: 2347, Name: "Adamant skeleton", Damage: -10, Energy: 0, Passive: true},
    71  		},
    72  	}
    73  
    74  	b.ReportAllocs()
    75  	b.ResetTimer()
    76  	for i := 0; i < b.N; i++ {
    77  		_, _ = avro.Marshal(schema, super)
    78  	}
    79  }
    80  
    81  func BenchmarkPartialSuperheroDecode(b *testing.B) {
    82  	data, err := ioutil.ReadFile("testdata/superhero.bin")
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  
    87  	schema, err := avro.ParseFiles("testdata/superhero.avsc")
    88  	if err != nil {
    89  		panic(err)
    90  	}
    91  
    92  	super := &PartialSuperhero{}
    93  
    94  	b.ReportAllocs()
    95  	b.ResetTimer()
    96  	for i := 0; i < b.N; i++ {
    97  		_ = avro.Unmarshal(schema, data, super)
    98  	}
    99  }
   100  
   101  func BenchmarkSuperheroGenericDecode(b *testing.B) {
   102  	data, err := ioutil.ReadFile("testdata/superhero.bin")
   103  	if err != nil {
   104  		panic(err)
   105  	}
   106  
   107  	schema, err := avro.ParseFiles("testdata/superhero.avsc")
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  
   112  	b.ReportAllocs()
   113  	b.ResetTimer()
   114  	for i := 0; i < b.N; i++ {
   115  		var m interface{}
   116  		_ = avro.Unmarshal(schema, data, &m)
   117  	}
   118  }
   119  
   120  func BenchmarkSuperheroGenericEncode(b *testing.B) {
   121  	schema, err := avro.ParseFiles("testdata/superhero.avsc")
   122  	if err != nil {
   123  		panic(err)
   124  	}
   125  
   126  	super := map[string]interface{}{
   127  		"id":             234765,
   128  		"affiliation_id": 9867,
   129  		"Name":           "Wolverine",
   130  		"life":           85.25,
   131  		"energy":         32.75,
   132  		"powers": []map[string]interface{}{
   133  			{"id": 2345, "name": "Bone Claws", "damage": 5, "energy": 1.15, "passive": false},
   134  			{"id": 2346, "name": "Regeneration", "damage": -2, "energy": 0.55, "passive": true},
   135  			{"id": 2347, "name": "Adamant skeleton", "damage": -10, "energy": 0, "passive": true},
   136  		},
   137  	}
   138  
   139  	b.ReportAllocs()
   140  	b.ResetTimer()
   141  	for i := 0; i < b.N; i++ {
   142  		_, _ = avro.Marshal(schema, super)
   143  	}
   144  }