main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. benchparse "golang.org/x/tools/benchmark/parse"
  21. "k8s.io/kubernetes/test/e2e/perftype"
  22. )
  23. func main() {
  24. err := run()
  25. if err != nil {
  26. panic(err)
  27. }
  28. }
  29. func run() error {
  30. if len(os.Args) < 2 {
  31. return fmt.Errorf("output filename is a required argument")
  32. }
  33. benchmarkSet, err := benchparse.ParseSet(os.Stdin)
  34. if err != nil {
  35. return err
  36. }
  37. data := perftype.PerfData{Version: "v1"}
  38. for _, benchMarks := range benchmarkSet {
  39. for _, benchMark := range benchMarks {
  40. data.DataItems = appendIfMeasured(data.DataItems, benchMark, benchparse.NsPerOp, "time", "μs", benchMark.NsPerOp/1000.0)
  41. data.DataItems = appendIfMeasured(data.DataItems, benchMark, benchparse.MBPerS, "throughput", "MBps", benchMark.MBPerS)
  42. data.DataItems = appendIfMeasured(data.DataItems, benchMark, benchparse.AllocedBytesPerOp, "allocated", "bytes", float64(benchMark.AllocedBytesPerOp))
  43. data.DataItems = appendIfMeasured(data.DataItems, benchMark, benchparse.AllocsPerOp, "allocations", "1", float64(benchMark.AllocsPerOp))
  44. data.DataItems = appendIfMeasured(data.DataItems, benchMark, 0, "iterations", "1", float64(benchMark.N))
  45. }
  46. }
  47. output := &bytes.Buffer{}
  48. if err := json.NewEncoder(output).Encode(data); err != nil {
  49. return err
  50. }
  51. formatted := &bytes.Buffer{}
  52. if err := json.Indent(formatted, output.Bytes(), "", " "); err != nil {
  53. return err
  54. }
  55. return ioutil.WriteFile(os.Args[1], formatted.Bytes(), 0664)
  56. }
  57. func appendIfMeasured(items []perftype.DataItem, benchmark *benchparse.Benchmark, metricType int, metricName string, unit string, value float64) []perftype.DataItem {
  58. if metricType != 0 && (benchmark.Measured&metricType) == 0 {
  59. return items
  60. }
  61. return append(items, perftype.DataItem{
  62. Unit: unit,
  63. Labels: map[string]string{
  64. "benchmark": benchmark.Name,
  65. "metricName": metricName},
  66. Data: map[string]float64{
  67. "value": value}})
  68. }