Add benchmarks.

This commit is contained in:
Nigel Tao committed 2017-06-29 14:13:23 +10:00
1 parent 04e78ad4e7
commit 480c509214
5 files changed
+241 -43

No files matched your search

+19 -3
View File
@@ -1,7 +1,7 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
// puffs-test-c runs C test programs.
// puffs-test-c runs C benchmark and test programs.
package main
import (
@@ -13,6 +13,10 @@ import (
"path/filepath"
)
var (
bench = flag.Bool("b", false, "whether to run benchmarks instead of regular tests")
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
@@ -47,14 +51,26 @@ func do(filename string) (failed bool, err error) {
in := filename + ".c"
out := filepath.Join(workDir, cc+".out")
ccCmd := exec.Command(cc, "-std=c99", "-Wall", "-Werror", "-o", out, in)
ccArgs := []string(nil)
if *bench {
ccArgs = append(ccArgs, "-O3")
} else {
// TODO: set these flags even if we pass -O3.
ccArgs = append(ccArgs, "-Wall", "-Werror")
}
ccArgs = append(ccArgs, "-std=c99", "-o", out, in)
ccCmd := exec.Command(cc, ccArgs...)
ccCmd.Stdout = os.Stdout
ccCmd.Stderr = os.Stderr
if err := ccCmd.Run(); err != nil {
return false, err
}
outCmd := exec.Command(out)
outArgs := []string(nil)
if *bench {
outArgs = append(outArgs, "-b")
}
outCmd := exec.Command(out, outArgs...)
outCmd.Stdout = os.Stdout
outCmd.Stderr = os.Stderr
outCmd.Dir = filepath.Dir(filename)
+1
View File
@@ -19,6 +19,7 @@ var commands = []struct {
name string
do func(puffsRoot string, args []string) error
}{
{"bench", doBench},
{"gen", doGen},
{"test", doTest},
}
+16 -8
View File
@@ -13,7 +13,10 @@ import (
"strings"
)
func doTest(puffsRoot string, args []string) error {
func doBench(puffsRoot string, args []string) error { return doBenchTest(puffsRoot, args, true) }
func doTest(puffsRoot string, args []string) error { return doBenchTest(puffsRoot, args, false) }
func doBenchTest(puffsRoot string, args []string, bench bool) error {
flags := flag.NewFlagSet("test", flag.ExitOnError)
langsFlag := flags.String("langs", langsDefault, langsUsage)
if err := flags.Parse(args); err != nil {
@@ -40,8 +43,8 @@ func doTest(puffsRoot string, args []string) error {
return err
}
// Proceed with testing the generated code.
f, err := test(puffsRoot, arg, langs, recursive)
// Proceed with benching / testing the generated code.
f, err := benchTest(puffsRoot, arg, langs, bench, recursive)
if err != nil {
return err
}
@@ -53,13 +56,13 @@ func doTest(puffsRoot string, args []string) error {
return nil
}
func test(puffsRoot, dirname string, langs []string, recursive bool) (failed bool, err error) {
func benchTest(puffsRoot, dirname string, langs []string, bench bool, recursive bool) (failed bool, err error) {
filenames, dirnames, err := listDir(puffsRoot, dirname, recursive)
if err != nil {
return false, err
}
if len(filenames) > 0 {
f, err := testDir(puffsRoot, dirname, langs)
f, err := benchTestDir(puffsRoot, dirname, langs, bench)
if err != nil {
return false, err
}
@@ -67,7 +70,7 @@ func test(puffsRoot, dirname string, langs []string, recursive bool) (failed boo
}
if len(dirnames) > 0 {
for _, d := range dirnames {
f, err := test(puffsRoot, filepath.Join(dirname, d), langs, recursive)
f, err := benchTest(puffsRoot, filepath.Join(dirname, d), langs, bench, recursive)
if err != nil {
return false, err
}
@@ -77,14 +80,19 @@ func test(puffsRoot, dirname string, langs []string, recursive bool) (failed boo
return failed, nil
}
func testDir(puffsRoot string, dirname string, langs []string) (failed bool, err error) {
func benchTestDir(puffsRoot string, dirname string, langs []string, bench bool) (failed bool, err error) {
if packageName := filepath.Base(dirname); !validName(packageName) {
return false, fmt.Errorf(`invalid package %q, not in [a-z0-9]+`, packageName)
}
for _, lang := range langs {
command := "puffs-test-" + lang
cmd := exec.Command(command, filepath.Join(puffsRoot, "test", lang, filepath.FromSlash(dirname)))
args := []string(nil)
if bench {
args = append(args, "-b")
}
args = append(args, filepath.Join(puffsRoot, "test", lang, filepath.FromSlash(dirname)))
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err == nil {