...

Source file src/go.mongodb.org/mongo-driver/benchmark/bson.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  	"errors"
    11  	"io/ioutil"
    12  	"path/filepath"
    13  
    14  	"go.mongodb.org/mongo-driver/bson"
    15  )
    16  
    17  const (
    18  	perfDataDir  = "perf"
    19  	bsonDataDir  = "extended_bson"
    20  	flatBSONData = "flat_bson.json"
    21  	deepBSONData = "deep_bson.json"
    22  	fullBSONData = "full_bson.json"
    23  )
    24  
    25  // utility functions for the bson benchmarks
    26  
    27  func loadSourceDocument(pathParts ...string) (bson.D, error) {
    28  	data, err := ioutil.ReadFile(filepath.Join(pathParts...))
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	var doc bson.D
    33  	err = bson.UnmarshalExtJSON(data, true, &doc)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	if len(doc) == 0 {
    39  		return nil, errors.New("empty bson document")
    40  	}
    41  
    42  	return doc, nil
    43  }
    44  
    45  func loadSourceRaw(pathParts ...string) (bson.Raw, error) {
    46  	doc, err := loadSourceDocument(pathParts...)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	raw, err := bson.Marshal(doc)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return bson.Raw(raw), nil
    56  }
    57  

View as plain text