...

Source file src/go.mongodb.org/mongo-driver/benchmark/bson_struct.go

Documentation: go.mongodb.org/mongo-driver/benchmark

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package benchmark
     8  
     9  import (
    10  	"context"
    11  	"errors"
    12  
    13  	"go.mongodb.org/mongo-driver/bson"
    14  )
    15  
    16  func BSONFlatStructDecoding(_ context.Context, tm TimerManager, iters int) error {
    17  	r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	tm.ResetTimer()
    23  
    24  	for i := 0; i < iters; i++ {
    25  		out := flatBSON{}
    26  		err := bson.Unmarshal(r, &out)
    27  		if err != nil {
    28  			return err
    29  		}
    30  	}
    31  	return nil
    32  }
    33  
    34  func BSONFlatStructEncoding(_ context.Context, tm TimerManager, iters int) error {
    35  	r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	doc := flatBSON{}
    41  	err = bson.Unmarshal(r, &doc)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	var buf []byte
    47  
    48  	tm.ResetTimer()
    49  	for i := 0; i < iters; i++ {
    50  		buf, err = bson.Marshal(doc)
    51  		if err != nil {
    52  			return err
    53  		}
    54  		if len(buf) == 0 {
    55  			return errors.New("encoding failed")
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func BSONFlatStructTagsEncoding(_ context.Context, tm TimerManager, iters int) error {
    62  	r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	doc := flatBSONTags{}
    68  	err = bson.Unmarshal(r, &doc)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	var buf []byte
    74  
    75  	tm.ResetTimer()
    76  	for i := 0; i < iters; i++ {
    77  		buf, err = bson.MarshalAppend(buf[:0], doc)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		if len(buf) == 0 {
    82  			return errors.New("encoding failed")
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  func BSONFlatStructTagsDecoding(_ context.Context, tm TimerManager, iters int) error {
    89  	r, err := loadSourceRaw(getProjectRoot(), perfDataDir, bsonDataDir, flatBSONData)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	tm.ResetTimer()
    95  	for i := 0; i < iters; i++ {
    96  		out := flatBSONTags{}
    97  		err := bson.Unmarshal(r, &out)
    98  		if err != nil {
    99  			return err
   100  		}
   101  	}
   102  	return nil
   103  }
   104  

View as plain text