From c47680932d916c44022c7059bb84f010caea60bf Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Fri, 26 Oct 2018 23:08:04 +0800 Subject: [PATCH] Add ScaleImage and Image2ASCII benchmark --- convert/convert_test.go | 25 ++++++++++++++++++++++++ convert/resize_test.go | 42 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/convert/convert_test.go b/convert/convert_test.go index 23c5ead..62fdfa3 100644 --- a/convert/convert_test.go +++ b/convert/convert_test.go @@ -92,3 +92,28 @@ func TestImageFile2ASCIIString(t *testing.T) { }) } } + + +func BenchmarkBigImage2ASCIIMatrix(b *testing.B) { + convertOptions := DefaultOptions + convertOptions.FitScreen = false + convertOptions.Colored = false + convertOptions.ExpectedWidth = 200 + convertOptions.ExpectedHeight = 200 + + for i:=0; i< b.N; i++ { + _ = ImageFile2ASCIIMatrix("testdata/cat_2000x1500.jpg", &convertOptions) + } +} + +func BenchmarkSmallImage2ASCIIMatrix(b *testing.B) { + convertOptions := DefaultOptions + convertOptions.FitScreen = false + convertOptions.Colored = false + convertOptions.ExpectedWidth = 200 + convertOptions.ExpectedHeight = 200 + + for i:=0; i< b.N; i++ { + _ = ImageFile2ASCIIMatrix("testdata/husky_200x200.jpg", &convertOptions) + } +} diff --git a/convert/resize_test.go b/convert/resize_test.go index 3c8ae55..aaafa13 100644 --- a/convert/resize_test.go +++ b/convert/resize_test.go @@ -142,13 +142,12 @@ func TestScaleToFitTerminalSize(t *testing.T) { } } - // ExampleScaleImage is scale image example func ExampleScaleImage() { imageFilePath := "testdata/cat_2000x1500.jpg" img, err := OpenImageFile(imageFilePath) if err != nil { - log.Fatal("open image file "+imageFilePath + " failed") + log.Fatal("open image file " + imageFilePath + " failed") } options := DefaultOptions @@ -160,4 +159,41 @@ func ExampleScaleImage() { sz := scaledImage.Bounds() fmt.Print(sz.Max.X, sz.Max.Y) // output: 200 100 -} \ No newline at end of file +} + +// BenchmarkScaleImage benchmark scale image test scale image +func BenchmarkScaleBigImage(b *testing.B) { + imageFilePath := "testdata/cat_2000x1500.jpg" + img, err := OpenImageFile(imageFilePath) + if err != nil { + log.Fatalf("Open image file %s failed", imageFilePath) + } + + options := DefaultOptions + options.Colored = false + options.FitScreen = false + options.ExpectedHeight = 100 + options.ExpectedWidth = 100 + + for i := 0; i < b.N; i++ { + _ = ScaleImage(img, &options) + } +} + +func BenchmarkScaleSmallImage(b *testing.B) { + imageFilePath := "testdata/husky_200x200.jpg" + img, err := OpenImageFile(imageFilePath) + if err != nil { + log.Fatalf("Open image file %s failed : %s", imageFilePath, err.Error()) + } + + options := DefaultOptions + options.Colored = false + options.FitScreen = false + options.ExpectedHeight = 100 + options.ExpectedWidth = 100 + + for i := 0; i < b.N; i++ { + _ = ScaleImage(img, &options) + } +} -- 2.45.2