Skip to content

Commit

Permalink
Merge pull request #175 from asteris-llc/feature/optimize-human-printer
Browse files Browse the repository at this point in the history
Feature/optimize human printer
  • Loading branch information
rebeccaskinner authored Aug 24, 2016
2 parents 78a0e8e + 524bc5e commit 03bd683
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func getPrinter() prettyprinters.Printer {

printer := human.NewFiltered(filter)
printer.Color = UseColor()
printer.InitColors()

return prettyprinters.New(printer)
}
Expand Down
43 changes: 28 additions & 15 deletions prettyprinters/human/human.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"strings"
"sync"
"text/template"

"github.com/asteris-llc/converge/graph"
Expand All @@ -32,6 +33,11 @@ type Printer struct {
Filter FilterFunc
}

var (
funcs = map[string]interface{}{}
funcsMu sync.Mutex
)

// New returns a base version of Printer
func New() *Printer {
return NewFiltered(ShowEverything)
Expand All @@ -43,6 +49,17 @@ func NewFiltered(f FilterFunc) *Printer {
return &Printer{Filter: f}
}

// InitColors initializes the colors used by the human printer
func (p *Printer) InitColors() {
p.funcsMapWrite("black", p.styled(chalk.Black.NewStyle().WithBackground(chalk.ResetColor)))
p.funcsMapWrite("red", p.styled(chalk.Red.NewStyle().WithBackground(chalk.ResetColor)))
p.funcsMapWrite("green", p.styled(chalk.Green.NewStyle().WithBackground(chalk.ResetColor)))
p.funcsMapWrite("yellow", p.styled(chalk.Yellow.NewStyle().WithBackground(chalk.ResetColor)))
p.funcsMapWrite("magenta", p.styled(chalk.Magenta.NewStyle().WithBackground(chalk.ResetColor)))
p.funcsMapWrite("cyan", p.styled(chalk.Cyan.NewStyle().WithBackground(chalk.ResetColor)))
p.funcsMapWrite("white", p.styled(chalk.White.NewStyle().WithBackground(chalk.ResetColor)))
}

// StartPP does nothing, but is required to satisfy the GraphPrinter interface
func (p *Printer) StartPP(g *graph.Graph) (pp.Renderable, error) {
return pp.HiddenString(), nil
Expand Down Expand Up @@ -120,23 +137,19 @@ func (p *Printer) DrawNode(g *graph.Graph, id string) (pp.Renderable, error) {
return &out, err
}

func (p *Printer) funcsMapWrite(key string, value interface{}) {
funcsMu.Lock()
defer funcsMu.Unlock()
funcs[key] = value
}

func (p *Printer) template(source string) (*template.Template, error) {
funcs := map[string]interface{}{
// colors
"black": p.styled(chalk.Black.NewStyle().WithBackground(chalk.ResetColor)),
"red": p.styled(chalk.Red.NewStyle().WithBackground(chalk.ResetColor)),
"green": p.styled(chalk.Green.NewStyle().WithBackground(chalk.ResetColor)),
"yellow": p.styled(chalk.Yellow.NewStyle().WithBackground(chalk.ResetColor)),
"magenta": p.styled(chalk.Magenta.NewStyle().WithBackground(chalk.ResetColor)),
"cyan": p.styled(chalk.Cyan.NewStyle().WithBackground(chalk.ResetColor)),
"white": p.styled(chalk.White.NewStyle().WithBackground(chalk.ResetColor)),

// utility
"diff": p.diff,
"indent": p.indent,
"empty": p.empty,
}
p.funcsMapWrite("diff", p.diff)
p.funcsMapWrite("indent", p.indent)
p.funcsMapWrite("empty", p.empty)

funcsMu.Lock()
defer funcsMu.Unlock()
return template.New("").Funcs(funcs).Parse(source)
}

Expand Down
76 changes: 73 additions & 3 deletions prettyprinters/human/human_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (
"github.com/stretchr/testify/require"
)

var (
printer = human.New()
printerOnlyChanged = human.NewFiltered(human.ShowOnlyChanged)
printerHideByKind = human.NewFiltered(human.HideByKind("param"))
)

func TestSatisfiesInterface(t *testing.T) {
t.Parallel()

Expand All @@ -39,6 +45,7 @@ func testFinishPP(t *testing.T, in Printable, out string) {
g.Add("root", in)

printer := human.New()
printer.InitColors()
str, err := printer.FinishPP(g)

require.Nil(t, err)
Expand Down Expand Up @@ -66,9 +73,11 @@ func TestFinishPPError(t *testing.T) {
}

func testDrawNodes(t *testing.T, in Printable, out string) {
printer := human.New()
printer.InitColors()
testDrawNodesCustomPrinter(
t,
human.New(),
printer,
"root",
in,
out,
Expand All @@ -85,6 +94,21 @@ func testDrawNodesCustomPrinter(t *testing.T, h *human.Printer, id string, in Pr
assert.Equal(t, out, str.String())
}

func benchmarkDrawNodes(in Printable) {
benchmarkDrawNodesCustomPrinter(
printer,
"root",
in,
)
}

func benchmarkDrawNodesCustomPrinter(h *human.Printer, id string, in Printable) {
g := graph.New()
g.Add(id, in)

h.DrawNode(g, id)
}

func TestDrawNodeNoChanges(t *testing.T) {
t.Parallel()

Expand All @@ -95,30 +119,60 @@ func TestDrawNodeNoChanges(t *testing.T) {
)
}

func BenchmarkDrawNodeNoChanges(b *testing.B) {
for i := 0; i < b.N; i++ {
benchmarkDrawNodes(
Printable{},
)
}
}

func TestDrawNodeNoChangesFiltered(t *testing.T) {
t.Parallel()
printerOnlyChanged.InitColors()

testDrawNodesCustomPrinter(
t,
human.NewFiltered(human.ShowOnlyChanged),
printerOnlyChanged,
"root",
Printable{},
"",
)
}

func BenchmarkDrawNodeNoChangesFiltered(b *testing.B) {
for i := 0; i < b.N; i++ {
benchmarkDrawNodesCustomPrinter(
printerOnlyChanged,
"root",
Printable{},
)
}
}

func TestDrawNodeMetaFiltered(t *testing.T) {
t.Parallel()
printerHideByKind.InitColors()

testDrawNodesCustomPrinter(
t,
human.NewFiltered(human.HideByKind("param")),
printerHideByKind,
"param.test",
Printable{},
"",
)
}

func BenchmarkDrawNodeMetaFiltered(b *testing.B) {
for i := 0; i < b.N; i++ {
benchmarkDrawNodesCustomPrinter(
printerHideByKind,
"param.test",
Printable{},
)
}
}

func TestDrawNodeChanges(t *testing.T) {
t.Parallel()

Expand All @@ -129,6 +183,14 @@ func TestDrawNodeChanges(t *testing.T) {
)
}

func BenchmarkDrawNodeChanges(b *testing.B) {
for i := 0; i < b.N; i++ {
benchmarkDrawNodes(
Printable{"a": "b"},
)
}
}

func TestDrawNodeError(t *testing.T) {
t.Parallel()

Expand All @@ -139,6 +201,14 @@ func TestDrawNodeError(t *testing.T) {
)
}

func BenchmarkDrawNodeError(b *testing.B) {
for i := 0; i < b.N; i++ {
benchmarkDrawNodes(
Printable{"error": "x"},
)
}
}

// printable stub

type Printable map[string]string
Expand Down

0 comments on commit 03bd683

Please sign in to comment.