github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/kati/testcase/gen_testcase_parse_benchmark.go (about)

     1  // Copyright 2015 Google Inc. All rights reserved
     2  //
     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  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // gen_testcase_parse_benchmark is a program to generate benchmark tests
    16  // for parsing testcases.
    17  //
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"text/template"
    27  )
    28  
    29  const preamble = `package kati
    30  
    31  import (
    32  	"io/ioutil"
    33  	"testing"
    34  )
    35  
    36  `
    37  
    38  var tmpl = template.Must(template.New("benchmarktest").Parse(`
    39  
    40  func BenchmarkTestcaseParse{{.Name}}(b *testing.B) {
    41  	data, err := ioutil.ReadFile({{.Filename | printf "%q"}})
    42  	if err != nil {
    43  		b.Fatal(err)
    44  	}
    45  	mk := string(data)
    46  	b.ResetTimer()
    47  	for i := 0; i < b.N; i++ {
    48  		parseMakefileString(mk, srcpos{
    49  			filename: {{.Filename | printf "%q"}},
    50  			lineno: 0,
    51  		})
    52  	}
    53  }
    54  `))
    55  
    56  func testName(fname string) string {
    57  	base := filepath.Base(fname)
    58  	i := strings.Index(base, ".")
    59  	if i >= 0 {
    60  		base = base[:i]
    61  	}
    62  	base = strings.Replace(base, "-", "", -1)
    63  	tn := strings.Title(base)
    64  	return tn
    65  }
    66  
    67  func writeBenchmarkTest(w io.Writer, fname string) {
    68  	name := testName(fname)
    69  	if strings.HasPrefix(name, "Err") {
    70  		return
    71  	}
    72  	err := tmpl.Execute(w, struct {
    73  		Name     string
    74  		Filename string
    75  	}{
    76  		Name:     testName(fname),
    77  		Filename: fname,
    78  	})
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  }
    83  
    84  func main() {
    85  	f, err := os.Create("testcase_parse_benchmark_test.go")
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  	defer func() {
    90  		err := f.Close()
    91  		if err != nil {
    92  			panic(err)
    93  		}
    94  	}()
    95  	fmt.Fprint(f, preamble)
    96  	matches, err := filepath.Glob("testcase/*.mk")
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  	for _, tc := range matches {
   101  		writeBenchmarkTest(f, tc)
   102  	}
   103  }