From 6dd90846ba124894ee1206c89021e90e8179f1a4 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Mon, 24 Apr 2017 20:44:32 +0200 Subject: [PATCH] doc: Use w.Write instead of fmt in yaml_docs benchmark old ns/op new ns/op delta BenchmarkGenYamlToFile-4 62488 61622 -1.39% benchmark old allocs new allocs delta BenchmarkGenYamlToFile-4 121 120 -0.83% benchmark old bytes new bytes delta BenchmarkGenYamlToFile-4 26706 26280 -1.60% --- doc/yaml_docs.go | 3 ++- doc/yaml_docs_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/yaml_docs.go b/doc/yaml_docs.go index f5cd5a3..54c244d 100644 --- a/doc/yaml_docs.go +++ b/doc/yaml_docs.go @@ -130,7 +130,8 @@ func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) str fmt.Println(err) os.Exit(1) } - if _, err := fmt.Fprintf(w, string(final)); err != nil { + + if _, err := w.Write(final); err != nil { return err } return nil diff --git a/doc/yaml_docs_test.go b/doc/yaml_docs_test.go index fb969da..29e985e 100644 --- a/doc/yaml_docs_test.go +++ b/doc/yaml_docs_test.go @@ -106,3 +106,20 @@ func TestGenYamlTree(t *testing.T) { t.Fatalf("Expected file 'do.yaml' to exist") } } + +func BenchmarkGenYamlToFile(b *testing.B) { + c := initializeWithRootCmd() + file, err := ioutil.TempFile("", "") + if err != nil { + b.Fatal(err) + } + defer os.Remove(file.Name()) + defer file.Close() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + if err := GenYaml(c, file); err != nil { + b.Fatal(err) + } + } +}