cdr.dev/grip@v0.0.0-20220214200134-863240d654ee/send/benchmark/harness_test.go (about)

     1  package send
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  func BenchmarkAllSenders(b *testing.B) {
    13  	outputFileName := getProjectRoot() + string(os.PathSeparator) + "build" + string(os.PathSeparator) + "perf.json"
    14  
    15  	ctx := context.Background()
    16  	output := []interface{}{}
    17  	for _, res := range runAllCases(ctx) {
    18  		evg, err := res.evergreenPerfFormat()
    19  		if err != nil {
    20  			continue
    21  		}
    22  
    23  		output = append(output, evg...)
    24  	}
    25  
    26  	evgOutput, err := json.MarshalIndent(map[string]interface{}{"results": output}, "", "   ")
    27  	if err != nil {
    28  		return
    29  	}
    30  	evgOutput = append(evgOutput, []byte("\n")...)
    31  
    32  	if outputFileName == "" {
    33  		fmt.Println(string(evgOutput))
    34  	} else if err := ioutil.WriteFile(outputFileName, evgOutput, 0644); err != nil {
    35  		fmt.Fprintf(os.Stderr, "problem writing file '%s': %s", outputFileName, err.Error())
    36  		return
    37  	}
    38  
    39  	return
    40  }
    41  
    42  func runAllCases(ctx context.Context) []*benchResult {
    43  	cases := getAllCases()
    44  
    45  	results := []*benchResult{}
    46  	for _, bc := range cases {
    47  		results = append(results, bc.Run(ctx))
    48  	}
    49  
    50  	return results
    51  }