Ver código fonte

Added benchmark/profiling tests.

Bugz 1 ano atrás
pai
commit
5dc31002d3

+ 27 - 10
door/color.go

@@ -1,8 +1,8 @@
 package door
 
 import (
-	"fmt"
 	"log"
+	"strconv"
 	"strings"
 )
 
@@ -13,14 +13,30 @@ import (
 // output.
 // https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
 // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
-func Color(arg ...int) string {
-	var result string = "\x1b["
-	for i := range arg {
-		result += fmt.Sprintf("%d;", arg[i])
+
+/*
+	func Color(arg ...int) string {
+		var result string = "\x1b["
+		for i := range arg {
+			result += fmt.Sprintf("%d;", arg[i])
+		}
+		result = result[:len(result)-1]
+		result += "m"
+		return result
+	}
+*/
+
+func Color(arg []int) string {
+	var result strings.Builder
+	result.WriteString("\x1b[")
+	for idx, i := range arg {
+		if idx != 0 {
+			result.WriteString(";")
+		}
+		result.WriteString(strconv.Itoa(i))
 	}
-	result = result[:len(result)-1]
-	result += "m"
-	return result
+	result.WriteString("m")
+	return result.String()
 }
 
 // BUG(bugz) INVERSE does not work.
@@ -38,7 +54,8 @@ func Color(arg ...int) string {
 // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
 func ColorText(color string) string {
 	// split on spaces, uppercase, match first 3 letter
-	var result []int
+	var res [5]int
+	var result []int = res[0:0]
 	var bg bool
 
 	result = append(result, 0)
@@ -124,5 +141,5 @@ func ColorText(color string) string {
 		}
 
 	}
-	return Color(result...)
+	return Color(result)
 }

+ 2 - 2
door/color_test.go

@@ -12,7 +12,7 @@ func TestColor(t *testing.T) {
 	}
 
 	for text, code := range ColorMap {
-		ct := Color(code...)
+		ct := Color(code)
 		if text != ct {
 			t.Errorf("Color: Expected %#v (%#v), got %#v", text, code, ct)
 		}
@@ -43,7 +43,7 @@ func TestColorText(t *testing.T) {
 
 	for text, code := range ColorMap {
 		ct := ColorText(text)
-		cc := Color(code...)
+		cc := Color(code)
 		if ct != cc {
 			t.Errorf("ColorText: Expected %#v (%#v), got %#v", cc, code, ct)
 		}

+ 69 - 1
door/line_test.go

@@ -2,7 +2,9 @@ package door
 
 import (
 	"fmt"
+	"strings"
 	"testing"
+	"unicode"
 )
 
 func TestLine(t *testing.T) {
@@ -18,7 +20,7 @@ func TestLine(t *testing.T) {
 		t.Error("Line: No updater, should return false")
 	}
 
-	line.DefaultColor = Color(0)
+	line.DefaultColor = Color([]int{0})
 	line.Width = 8
 	output = line.Output()
 	expect = "\x1b[0mTest Me "
@@ -111,3 +113,69 @@ func TestLineCP437(t *testing.T) {
 		}
 	}
 }
+
+// Benchmarks / profiling
+
+func BenchmarkLine(b *testing.B) {
+
+	var render = func(text string) string {
+		var output strings.Builder
+		var last *string
+		// var r Render = Render{Line: text}
+
+		var Up string = ColorText("BLUE")
+		var Down string = ColorText("BOLD BLUE")
+		var Num string = ColorText("BRI GREEN")
+		var Sym string = ColorText("CYAN")
+
+		for _, letter := range text {
+			if unicode.IsUpper(letter) {
+				if last != &Up {
+					output.WriteString(Up)
+					last = &Up
+				}
+				// r.Append(Up, 1)
+			} else if unicode.IsLower(letter) {
+				if last != &Down {
+					output.WriteString(Down)
+					last = &Down
+				}
+				// r.Append(Down, 1)
+			} else if unicode.IsDigit(letter) {
+				if last != &Num {
+					output.WriteString(Num)
+					last = &Num
+				}
+				// r.Append(Num, 1)
+			} else {
+				if last != &Sym {
+					output.WriteString(Sym)
+					last = &Sym
+				}
+				//r.Append(Sym, 1)
+			}
+			output.WriteRune(letter)
+			// output.WriteString(string(letter))
+		}
+		return output.String()
+		// return r.Result
+	}
+
+	for i := 0; i < b.N; i++ {
+		var up = 0
+		var updater = func() string {
+			up++
+			return fmt.Sprintf("The Value: %d", up)
+		}
+
+		var line Line = Line{Text: updater(),
+			UpdateF: updater,
+			RenderF: render,
+			Width:   18}
+		line.Output()
+		for l := 0; l < 10; l++ {
+			line.Update()
+
+		}
+	}
+}

+ 2 - 2
door/nomoresecrets.go

@@ -186,7 +186,7 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 					// Yes, don't store in work.  Process code.
 
 					Door.UpdateLastColor(currentANSI, &lastcolor)
-					colormap[workpos] = Color(lastcolor...)
+					colormap[workpos] = Color(lastcolor)
 					coloridx = append(coloridx, workpos)
 					// log.Printf("Added %d with %s\n", workpos, colormap[workpos][1:])
 				} else {
@@ -391,7 +391,7 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 				if ANSIchar == 'm' {
 					// Yes, don't store in work.  Process code.
 					Door.UpdateLastColor(currentANSI, &lastcolor)
-					colormap[workpos] = Color(lastcolor...)
+					colormap[workpos] = Color(lastcolor)
 					coloridx = append(coloridx, workpos)
 				} else {
 					// Not a color code.  Add to work.

+ 14 - 0
door/nomoresecrets_test.go

@@ -23,3 +23,17 @@ func TestRandom(t *testing.T) {
 func TestNoMoreSecrets(t *testing.T) {
 
 }
+func BenchmarkNoMoreSecrets(b *testing.B) {
+	Unicode = true
+	var d *Door = &Door{}
+
+	for i := 0; i < b.N; i++ {
+		var output = Goto(5, 5) + ColorText("WHITE ON BLUE") + "Secret Text Revealed!"
+		var better NoMoreSecretsConfig = NoMoreSecretsDefault
+		better.Jumble_Loop_Speed = 75  // 35
+		better.Reveal_Loop_Speed = 100 // 50
+		better.Color = ColorText("BRI CYAN ON BLUE")
+
+		NoMoreSecrets(output, d, &better)
+	}
+}

+ 1 - 1
door/panel_test.go

@@ -92,7 +92,7 @@ func TestPanelUpdate(t *testing.T) {
 	}
 
 	got = p.GotoEnd()
-	expected = Goto(p.X+p.Width+2, p.Y+p.Length()+2)
+	expected = Goto(p.X+p.Width+2, p.Y+p.Length()+1)
 	if got != expected {
 		t.Errorf("Panel GotoEnd expected %#v, got %#v", expected, got)
 	}

+ 17 - 4
door/spinrite.go

@@ -1,5 +1,7 @@
 package door
 
+import "strings"
+
 type SpinRite struct {
 	Width     uint8   // Width of the animation (must be odd)
 	Length    uint8   // Width of the worm (must be odd)
@@ -170,17 +172,28 @@ func (sr *SpinRite) Calculate() {
 }
 
 func (sr *SpinRite) Output() string {
-	var result string
+	// var result string
+	var result strings.Builder
+	// sr.Result.Reset()
+	result.WriteString(sr.Color)
 
 	sr.Calculate()
 	if Unicode {
-		result = string(sr.OutputR)
+		// result = string(sr.OutputR)
+		/*
+			for _, r := range sr.OutputR {
+				result.WriteRune(r)
+			}
+		*/
+		result.WriteString(string(sr.OutputR))
 	} else {
-		result = string(sr.OutputB)
+		//result = string(sr.OutputB)
+		result.WriteString(string(sr.OutputB))
 	}
 	sr.Index++
 
-	return sr.Color + result
+	// return sr.Color + result
+	return result.String()
 }
 
 type SpinRiteMsg struct {

+ 17 - 0
door/spinrite_test.go

@@ -0,0 +1,17 @@
+package door
+
+import "testing"
+
+func BenchmarkSpinRite(b *testing.B) {
+	Unicode = true
+
+	for i := 0; i < b.N; i++ {
+		var spin SpinRiteMsg = SpinRiteMsgInit(15, 5,
+			ColorText("RED ON GREEN"),
+			[]string{"RED", "GREEN", "SOFTWARE"})
+
+		for x := 0; x < 20; x++ {
+			spin.Output()
+		}
+	}
+}

+ 1 - 1
door/write_linux.go

@@ -30,7 +30,7 @@ func Writer(d *Door) {
 		}
 
 		if strings.HasSuffix(output, RestorePos) {
-			output += Color(d.LastColor...)
+			output += Color(d.LastColor)
 
 		} else {
 			d.UpdateLastColor(output, &d.LastColor)

+ 1 - 1
door/write_windows.go

@@ -56,7 +56,7 @@ func Writer(d *Door) {
 		}
 
 		if strings.HasSuffix(output, RestorePos) {
-			output += Color(d.LastColor...)
+			output += Color(d.LastColor)
 		} else {
 			d.UpdateLastColor(output, &d.LastColor)
 		}