...

Source file src/github.com/logrusorgru/aurora/v3/bench_test.go

Documentation: github.com/logrusorgru/aurora/v3

     1  //
     2  // Copyright (c) 2016-2020 The Aurora Authors. All rights reserved.
     3  // This program is free software. It comes without any warranty,
     4  // to the extent permitted by applicable law. You can redistribute
     5  // it and/or modify it under the terms of the Unlicense. See LICENSE
     6  // file for more details or see below.
     7  //
     8  
     9  //
    10  // This is free and unencumbered software released into the public domain.
    11  //
    12  // Anyone is free to copy, modify, publish, use, compile, sell, or
    13  // distribute this software, either in source code form or as a compiled
    14  // binary, for any purpose, commercial or non-commercial, and by any
    15  // means.
    16  //
    17  // In jurisdictions that recognize copyright laws, the author or authors
    18  // of this software dedicate any and all copyright interest in the
    19  // software to the public domain. We make this dedication for the benefit
    20  // of the public at large and to the detriment of our heirs and
    21  // successors. We intend this dedication to be an overt act of
    22  // relinquishment in perpetuity of all present and future rights to this
    23  // software under copyright law.
    24  //
    25  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    26  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    27  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    28  // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    29  // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    30  // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    31  // OTHER DEALINGS IN THE SOFTWARE.
    32  //
    33  // For more information, please refer to <http://unlicense.org/>
    34  //
    35  
    36  package aurora
    37  
    38  import (
    39  	"testing"
    40  )
    41  
    42  var (
    43  	gVal  Value
    44  	gVals []Value
    45  	gStr  string
    46  )
    47  
    48  var (
    49  	short  = "x"
    50  	long   = "some long and complicated string to colorize"
    51  	shortf = "%s"
    52  	longf  = long + " %s"
    53  	s18    = "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s"
    54  )
    55  
    56  // 18 values
    57  func simpleValues(a Aurora, x string) []Value {
    58  	return []Value{
    59  		a.Reset("x"),
    60  
    61  		a.Bold("x"),
    62  		a.Faint("x"),
    63  		a.DoublyUnderline("x"),
    64  		a.Fraktur("x"),
    65  		a.Italic("x"),
    66  		a.Underline("x"),
    67  		a.SlowBlink("x"),
    68  		a.RapidBlink("x"),
    69  		a.Blink("x"),
    70  		a.Reverse("x"),
    71  		a.Inverse("x"),
    72  		a.Conceal("x"),
    73  		a.Hidden("x"),
    74  		a.CrossedOut("x"),
    75  		a.StrikeThrough("x"),
    76  		a.Framed("x"),
    77  		a.Encircled("x"),
    78  		a.Overlined("x"),
    79  
    80  		a.Black("x"),
    81  		a.Red("x"),
    82  		a.Green("x"),
    83  		a.Yellow("x"),
    84  		a.Brown("x"),
    85  		a.Blue("x"),
    86  		a.Magenta("x"),
    87  		a.Cyan("x"),
    88  		a.White("x"),
    89  		a.BrightBlack("x"),
    90  		a.BrightRed("x"),
    91  		a.BrightGreen("x"),
    92  		a.BrightYellow("x"),
    93  		a.BrightBlue("x"),
    94  		a.BrightMagenta("x"),
    95  		a.BrightCyan("x"),
    96  		a.BrightWhite("x"),
    97  		a.Index(178, "x"),
    98  		a.Gray(14, "x"),
    99  
   100  		a.BgBlack("x"),
   101  		a.BgRed("x"),
   102  		a.BgGreen("x"),
   103  		a.BgYellow("x"),
   104  		a.BgBrown("x"),
   105  		a.BgBlue("x"),
   106  		a.BgMagenta("x"),
   107  		a.BgCyan("x"),
   108  		a.BgWhite("x"),
   109  		a.BgBrightBlack("x"),
   110  		a.BgBrightRed("x"),
   111  		a.BgBrightGreen("x"),
   112  		a.BgBrightYellow("x"),
   113  		a.BgBrightBlue("x"),
   114  		a.BgBrightMagenta("x"),
   115  		a.BgBrightCyan("x"),
   116  		a.BgBrightWhite("x"),
   117  		a.BgIndex(187, "x"),
   118  		a.BgGray(15, "x"),
   119  	}
   120  }
   121  
   122  // 18 values
   123  func complexValues(a Aurora, x string) []Value {
   124  
   125  	var allFormats = func(val Value) Value {
   126  		return val.Bold().DoublyUnderline().Fraktur().Blink().Italic().
   127  			Underline().Blink().Reverse().Conceal().CrossedOut().Framed().
   128  			Encircled().Overlined()
   129  
   130  	}
   131  
   132  	return []Value{
   133  		allFormats(a.Reset(x).BrightBlack().BgBrightWhite()),
   134  
   135  		allFormats(a.Bold(x).BrightRed().BgBrightMagenta()),
   136  		allFormats(a.Faint(x).Index(128).BgIndex(135)),
   137  		allFormats(a.DoublyUnderline(x).Gray(13).BgRed()),
   138  		allFormats(a.Fraktur(x).BgYellow().Black()),
   139  		allFormats(a.Italic(x).Red().BgBrightMagenta()),
   140  		allFormats(a.Underline(x).BrightBlue().BgYellow()),
   141  		allFormats(a.SlowBlink(x).BrightCyan().BgBrightRed()),
   142  		allFormats(a.RapidBlink(x).Green().BgYellow()),
   143  		allFormats(a.Blink(x).Red().BgBlack()),
   144  		allFormats(a.Reverse(x).BgBlack().White()),
   145  		allFormats(a.Inverse(x).White().BgBlack()),
   146  		allFormats(a.Conceal(x).BrightBlue().BgYellow()),
   147  		allFormats(a.Hidden(x).BrightYellow().BgBrightGreen()),
   148  		allFormats(a.CrossedOut(x).BrightGreen()),
   149  		allFormats(a.StrikeThrough(x).Index(128)),
   150  		allFormats(a.Framed(x).Index(128).BgIndex(55)),
   151  		allFormats(a.Encircled(x).Index(216).BgIndex(20)),
   152  		allFormats(a.Overlined(x).Index(224).BgIndex(15)),
   153  
   154  		allFormats(a.Black(x).BgBrightRed()),
   155  		allFormats(a.Red(x).BgBrightRed()),
   156  		allFormats(a.Green(x).BgBrightRed()),
   157  		allFormats(a.Yellow(x).BgBrightRed()),
   158  		allFormats(a.Brown(x).BgBrightRed()),
   159  		allFormats(a.Blue(x).BgBrightRed()),
   160  		allFormats(a.Magenta(x).BgBrightRed()),
   161  		allFormats(a.Cyan(x).BgBrightRed()),
   162  		allFormats(a.White(x).BgBrightRed()),
   163  		allFormats(a.BrightBlack(x).BgBrightRed()),
   164  		allFormats(a.BrightRed(x).BgBrightRed()),
   165  		allFormats(a.BrightGreen(x).BgBrightRed()),
   166  		allFormats(a.BrightYellow(x).BgWhite()),
   167  		allFormats(a.BrightBlue(x).BgMagenta()),
   168  		allFormats(a.BrightMagenta(x).BgGreen()),
   169  		allFormats(a.BrightCyan(x).BgRed()),
   170  		allFormats(a.BrightWhite(x).BgBlack()),
   171  		allFormats(a.Index(178, x).BgGray(1)),
   172  		allFormats(a.Gray(14, x).BgIndex(89)),
   173  
   174  		allFormats(a.BgBlack(x).BrightBlack()),
   175  		allFormats(a.BgRed(x).BrightRed()),
   176  		allFormats(a.BgGreen(x).BrightGreen()),
   177  		allFormats(a.BgYellow(x).BrightYellow()),
   178  		allFormats(a.BgBrown(x).BrightYellow()),
   179  		allFormats(a.BgBlue(x).BrightBlue()),
   180  		allFormats(a.BgMagenta(x).BrightMagenta()),
   181  		allFormats(a.BgCyan(x).BrightCyan()),
   182  		allFormats(a.BgWhite(x).BrightWhite()),
   183  		allFormats(a.BgBrightBlack(x).Black()),
   184  		allFormats(a.BgBrightRed(x).Red()),
   185  		allFormats(a.BgBrightGreen(x).Green()),
   186  		allFormats(a.BgBrightYellow(x).Yellow()),
   187  		allFormats(a.BgBrightBlue(x).Blue()),
   188  		allFormats(a.BgBrightMagenta(x).Magenta()),
   189  		allFormats(a.BgBrightCyan(x).Cyan()),
   190  		allFormats(a.BgBrightWhite(x).White()),
   191  		allFormats(a.BgIndex(187, x).Index(16)),
   192  		allFormats(a.BgGray(15, x).Gray(0)),
   193  	}
   194  }
   195  
   196  func benchSimpleValue(b *testing.B, a Aurora, x string) {
   197  	b.ResetTimer()
   198  	for i := 0; i < b.N; i++ {
   199  		gVals = simpleValues(a, x)
   200  	}
   201  	b.ReportAllocs()
   202  }
   203  
   204  func benchComplexValue(b *testing.B, a Aurora, x string) {
   205  	b.ResetTimer()
   206  	for i := 0; i < b.N; i++ {
   207  		gVals = complexValues(a, x)
   208  	}
   209  	b.ReportAllocs()
   210  }
   211  
   212  func benchValueString(b *testing.B, vals []Value) {
   213  	b.ResetTimer()
   214  	for i := 0; i < b.N; i++ {
   215  		for j := 0; j < len(vals); j++ { // TODO: range and allocs ?
   216  			gStr = vals[j].String()
   217  		}
   218  	}
   219  	b.ReportAllocs()
   220  }
   221  
   222  func benchSprintf(b *testing.B, a Aurora, format interface{},
   223  	args ...interface{}) {
   224  	b.ResetTimer()
   225  	for i := 0; i < b.N; i++ {
   226  		gStr = a.Sprintf(format, args...)
   227  	}
   228  	b.ReportAllocs()
   229  }
   230  
   231  func toInterfaces(vals []Value) []interface{} {
   232  	r := make([]interface{}, 0, len(vals))
   233  	for _, val := range vals {
   234  		r = append(r, val)
   235  	}
   236  	return r
   237  }
   238  
   239  func auroraBench(a Aurora, b *testing.B) {
   240  	// a.Red("...")
   241  	b.Run("simple value", func(b *testing.B) {
   242  		b.Run("short", func(b *testing.B) { benchSimpleValue(b, a, short) })
   243  		b.Run("long", func(b *testing.B) { benchSimpleValue(b, a, long) })
   244  	})
   245  	// a.Red("...").BgRed().Bold().Inverse()
   246  	b.Run("complex value", func(b *testing.B) {
   247  		b.Run("short", func(b *testing.B) { benchComplexValue(b, a, short) })
   248  		b.Run("long", func(b *testing.B) { benchComplexValue(b, a, long) })
   249  	})
   250  	// a.Red("...").String()
   251  	b.Run("simple value string", func(b *testing.B) {
   252  		b.Run("short", func(b *testing.B) {
   253  			benchValueString(b, simpleValues(a, short))
   254  		})
   255  		b.Run("long", func(b *testing.B) {
   256  			benchValueString(b, simpleValues(a, long))
   257  		})
   258  	})
   259  	// a.Red("...").BgRed().Bold().Inverse().String()
   260  	b.Run("complex value string", func(b *testing.B) {
   261  		b.Run("short", func(b *testing.B) {
   262  			benchValueString(b, complexValues(a, short))
   263  		})
   264  		b.Run("long", func(b *testing.B) {
   265  			benchValueString(b, complexValues(a, long))
   266  		})
   267  	})
   268  
   269  	// Sprintf
   270  
   271  	b.Run("sprintf", func(b *testing.B) {
   272  		// a.Sprintf(noString("... %s"), "x")
   273  		b.Run("no-string", func(b *testing.B) {
   274  			b.Run("short format", func(b *testing.B) {
   275  				benchSprintf(b, a, noString(shortf), "x")
   276  			})
   277  			b.Run("long format", func(b *testing.B) {
   278  				benchSprintf(b, a, noString(longf), "x")
   279  			})
   280  		})
   281  		// a.Sprintf("... %s", "x")
   282  		b.Run("usual", func(b *testing.B) {
   283  			b.Run("short format", func(b *testing.B) {
   284  				benchSprintf(b, a, shortf, "x")
   285  			})
   286  			b.Run("long format", func(b *testing.B) {
   287  				benchSprintf(b, a, longf, "x")
   288  			})
   289  		})
   290  
   291  		//
   292  		// vary arguments
   293  		//
   294  
   295  		b.Run("simple arg", func(b *testing.B) {
   296  			// string %s, a.Red("x") (one-color single argument)
   297  			b.Run("1 arg", func(b *testing.B) {
   298  				benchSprintf(b, a, "%s", a.Red("x"))
   299  			})
   300  			// string %s %s ..., a.Red("x"), ... (one-color many arguments)
   301  			b.Run("18 args", func(b *testing.B) {
   302  				benchSprintf(b, a, s18, toInterfaces(simpleValues(a, "x"))...)
   303  			})
   304  		})
   305  
   306  		b.Run("complex arg", func(b *testing.B) {
   307  			// string %s, a.Red("x").BgRed()... (many colors single argument)
   308  			b.Run("1 arg", func(b *testing.B) {
   309  				benchSprintf(b, a, "%s", a.Red("x").BgGray(5).Bold().Inverse())
   310  			})
   311  			// string %s %s..., a.Red("x").BgRed()..., ... (many colors 18 args)
   312  			b.Run("18 args", func(b *testing.B) {
   313  				benchSprintf(b, a, s18, toInterfaces(complexValues(a, "x"))...)
   314  			})
   315  		})
   316  
   317  		//
   318  		// same with colored format
   319  		//
   320  
   321  		b.Run("simple arg (simple format)", func(b *testing.B) {
   322  			// string %s, a.Red("x") (one-color single argument)
   323  			b.Run("1 arg", func(b *testing.B) {
   324  				benchSprintf(b, a, a.Magenta("%s"), a.Red("x"))
   325  			})
   326  			// string %s %s ..., a.Red("x"), ... (one-color many arguments)
   327  			b.Run("18 args", func(b *testing.B) {
   328  				benchSprintf(b, a, a.Magenta(s18),
   329  					toInterfaces(simpleValues(a, "x"))...)
   330  			})
   331  		})
   332  
   333  		b.Run("complex arg (simple format)", func(b *testing.B) {
   334  			// string %s, a.Red("x").BgRed()... (many colors single argument)
   335  			b.Run("1 arg", func(b *testing.B) {
   336  				benchSprintf(b, a, a.Magenta("%s"),
   337  					a.Red("x").BgGray(5).Bold().Inverse())
   338  			})
   339  			// string %s %s..., a.Red("x").BgRed()..., ... (many colors 18 args)
   340  			b.Run("18 args", func(b *testing.B) {
   341  				benchSprintf(b, a, a.Magenta(s18),
   342  					toInterfaces(complexValues(a, "x"))...)
   343  			})
   344  		})
   345  
   346  		//
   347  		// same with complex format
   348  		//
   349  
   350  		b.Run("simple arg (complex format)", func(b *testing.B) {
   351  			// string %s, a.Red("x") (one-color single argument)
   352  			b.Run("1 arg", func(b *testing.B) {
   353  				benchSprintf(b, a, a.Magenta("%s").BgGray(5).Bold().Inverse(),
   354  					a.Red("x"))
   355  			})
   356  			// string %s %s ..., a.Red("x"), ... (one-color many arguments)
   357  			b.Run("18 args", func(b *testing.B) {
   358  				benchSprintf(b, a, a.Magenta(s18).BgGray(5).Bold().Inverse(),
   359  					toInterfaces(simpleValues(a, "x"))...)
   360  			})
   361  		})
   362  
   363  		b.Run("complex arg (complex format)", func(b *testing.B) {
   364  			// string %s, a.Red("x").BgRed()... (many colors single argument)
   365  			b.Run("1 arg", func(b *testing.B) {
   366  				benchSprintf(b, a, a.Magenta("%s").BgGray(5).Bold().Inverse(),
   367  					a.Red("x").BgGray(5).Bold().Inverse())
   368  			})
   369  			// string %s %s..., a.Red("x").BgRed()..., ... (many colors 18 args)
   370  			b.Run("18 args", func(b *testing.B) {
   371  				benchSprintf(b, a, a.Magenta(s18).BgGray(5).Bold().Inverse(),
   372  					toInterfaces(complexValues(a, "x"))...)
   373  			})
   374  		})
   375  	})
   376  
   377  }
   378  
   379  func Benchmark_auroraClear(b *testing.B) {
   380  	a := NewAurora(false)
   381  	auroraBench(a, b)
   382  }
   383  
   384  // create a value
   385  func Benchmark_aurora(b *testing.B) {
   386  	a := NewAurora(true)
   387  	auroraBench(a, b)
   388  }
   389  
   390  // Wrap functions
   391  
   392  func Benchmark_wrap(b *testing.B) {
   393  	ary := []struct {
   394  		name string
   395  		fn   func(interface{}) Value
   396  	}{
   397  		{"Reset", Reset},
   398  
   399  		{"Bold", Bold},
   400  		{"Faint", Faint},
   401  		{"DoublyUnderline", DoublyUnderline},
   402  		{"Fraktur", Fraktur},
   403  		{"Italic", Italic},
   404  		{"Underline", Underline},
   405  		{"SlowBlink", SlowBlink},
   406  		{"RapidBlink", RapidBlink},
   407  		{"Blink", Blink},
   408  		{"Reverse", Reverse},
   409  		{"Inverse", Inverse},
   410  		{"Conceal", Conceal},
   411  		{"Hidden", Hidden},
   412  		{"CrossedOut", CrossedOut},
   413  		{"StrikeThrough", StrikeThrough},
   414  		{"Framed", Framed},
   415  		{"Encircled", Encircled},
   416  		{"Overlined", Overlined},
   417  
   418  		{"Black", Black},
   419  		{"Red", Red},
   420  		{"Green", Green},
   421  		{"Yellow", Yellow},
   422  		{"Brown", Brown},
   423  		{"Blue", Blue},
   424  		{"Magenta", Magenta},
   425  		{"Cyan", Cyan},
   426  		{"White", White},
   427  		{"BrightBlack", BrightBlack},
   428  		{"BrightRed", BrightRed},
   429  		{"BrightGreen", BrightGreen},
   430  		{"BrightYellow", BrightYellow},
   431  		{"BrightBlue", BrightBlue},
   432  		{"BrightMagenta", BrightMagenta},
   433  		{"BrightCyan", BrightCyan},
   434  		{"BrightWhite", BrightWhite},
   435  
   436  		{"BgBlack", BgBlack},
   437  		{"BgRed", BgRed},
   438  		{"BgGreen", BgGreen},
   439  		{"BgYellow", BgYellow},
   440  		{"BgBrown", BgBrown},
   441  		{"BgBlue", BgBlue},
   442  		{"BgMagenta", BgMagenta},
   443  		{"BgCyan", BgCyan},
   444  		{"BgWhite", BgWhite},
   445  		{"BgBrightBlack", BgBrightBlack},
   446  		{"BgBrightRed", BgBrightRed},
   447  		{"BgBrightGreen", BgBrightGreen},
   448  		{"BgBrightYellow", BgBrightYellow},
   449  		{"BgBrightBlue", BgBrightBlue},
   450  		{"BgBrightMagenta", BgBrightMagenta},
   451  		{"BgBrightCyan", BgBrightCyan},
   452  		{"BgBrightWhite", BgBrightWhite},
   453  	}
   454  	for _, wf := range ary {
   455  		b.Run(wf.name, func(b *testing.B) {
   456  			for i := 0; i < b.N; i++ {
   457  				gVal = wf.fn(0)
   458  			}
   459  			b.ReportAllocs()
   460  		})
   461  	}
   462  
   463  	b.Run("Colorize", func(b *testing.B) {
   464  		for i := 0; i < b.N; i++ {
   465  			gVal = Colorize(0, RedFg)
   466  		}
   467  		b.ReportAllocs()
   468  	})
   469  
   470  	b.Run("Index", func(b *testing.B) {
   471  		for i := 0; i < b.N; i++ {
   472  			gVal = Index(216, 0)
   473  		}
   474  		b.ReportAllocs()
   475  	})
   476  
   477  	b.Run("BgIndex", func(b *testing.B) {
   478  		for i := 0; i < b.N; i++ {
   479  			gVal = Index(216, 0)
   480  		}
   481  		b.ReportAllocs()
   482  	})
   483  
   484  	b.Run("Gray", func(b *testing.B) {
   485  		for i := 0; i < b.N; i++ {
   486  			gVal = Gray(15, 0)
   487  		}
   488  		b.ReportAllocs()
   489  	})
   490  
   491  	b.Run("BgGray", func(b *testing.B) {
   492  		for i := 0; i < b.N; i++ {
   493  			gVal = BgGray(15, 0)
   494  		}
   495  		b.ReportAllocs()
   496  	})
   497  }
   498  

View as plain text