blob: dfb92b728af9c24cf5bf668156e33cdd3320476e [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package golang
import (
"bytes"
_ "embed"
"fmt"
"strings"
"text/template"
"go.fuchsia.dev/fuchsia/tools/fidl/gidl/lib/config"
"go.fuchsia.dev/fuchsia/tools/fidl/gidl/lib/ir"
"go.fuchsia.dev/fuchsia/tools/fidl/gidl/lib/mixer"
"go.fuchsia.dev/fuchsia/tools/fidl/lib/fidlgen"
)
var (
//go:embed benchmarks.tmpl
benchmarkTmplText string
benchmarkTmpl = template.Must(template.New("benchmarkTmpl").Parse(benchmarkTmplText))
)
type benchmarkTmplInput struct {
Benchmarks []benchmark
}
type benchmark struct {
Name, ChromeperfPath, Value, ValueType string
EnableSendEventBenchmark, EnableEchoCallBenchmark bool
}
// GenerateBenchmarks generates Go benchmarks.
func GenerateBenchmarks(gidl ir.All, fidl fidlgen.Root, config config.GeneratorConfig) ([]byte, error) {
schema := mixer.BuildSchema(fidl)
var benchmarks []benchmark
for _, gidlBenchmark := range gidl.Benchmark {
decl, err := schema.ExtractDeclaration(gidlBenchmark.Value, gidlBenchmark.HandleDefs)
if err != nil {
return nil, fmt.Errorf("benchmark %s: %s", gidlBenchmark.Name, err)
}
value := visit(gidlBenchmark.Value, decl)
benchmarks = append(benchmarks, benchmark{
Name: goBenchmarkName(gidlBenchmark.Name),
ChromeperfPath: gidlBenchmark.Name,
Value: value,
ValueType: declName(decl),
EnableSendEventBenchmark: gidlBenchmark.EnableSendEventBenchmark,
EnableEchoCallBenchmark: gidlBenchmark.EnableEchoCallBenchmark,
})
}
input := benchmarkTmplInput{
Benchmarks: benchmarks,
}
var buf bytes.Buffer
err := withGoFmt{benchmarkTmpl}.Execute(&buf, input)
return buf.Bytes(), err
}
func goBenchmarkName(gidlName string) string {
return strings.ReplaceAll(gidlName, "/", "_")
}