harness_main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. package benchmark
  7. import (
  8. "context"
  9. "encoding/json"
  10. "flag"
  11. "fmt"
  12. "io/ioutil"
  13. "os"
  14. )
  15. func DriverBenchmarkMain() int {
  16. var hasErrors bool
  17. var outputFileName string
  18. flag.StringVar(&outputFileName, "output", "perf.json", "path to write the 'perf.json' file")
  19. flag.Parse()
  20. ctx := context.Background()
  21. output := []interface{}{}
  22. for _, res := range runDriverCases(ctx) {
  23. if res.HasErrors() {
  24. hasErrors = true
  25. }
  26. evg, err := res.EvergreenPerfFormat()
  27. if err != nil {
  28. hasErrors = true
  29. continue
  30. }
  31. output = append(output, evg...)
  32. }
  33. evgOutput, err := json.MarshalIndent(map[string]interface{}{"results": output}, "", " ")
  34. if err != nil {
  35. return 1
  36. }
  37. evgOutput = append(evgOutput, []byte("\n")...)
  38. if outputFileName == "" {
  39. fmt.Println(string(evgOutput))
  40. } else if err := ioutil.WriteFile(outputFileName, evgOutput, 0644); err != nil {
  41. fmt.Fprintf(os.Stderr, "problem writing file '%s': %s", outputFileName, err.Error())
  42. return 1
  43. }
  44. if hasErrors {
  45. return 1
  46. }
  47. return 0
  48. }
  49. func runDriverCases(ctx context.Context) []*BenchResult {
  50. cases := getAllCases()
  51. results := []*BenchResult{}
  52. for _, bc := range cases {
  53. results = append(results, bc.Run(ctx))
  54. }
  55. return results
  56. }