github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/imaging/utils_bench_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package imaging
     5  
     6  import (
     7  	"image"
     8  	"image/color"
     9  	"image/draw"
    10  	"testing"
    11  )
    12  
    13  func fillImageTransparencyOld(img image.Image, c color.Color) {
    14  	dst := image.NewRGBA(img.Bounds())
    15  	draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
    16  	draw.Draw(dst, dst.Bounds(), img, img.Bounds().Min, draw.Over)
    17  }
    18  
    19  func fullyOpaqueGen(w, h int) func() image.Image {
    20  	return func() image.Image {
    21  		dst := image.NewRGBA(image.Rect(0, 0, w, h))
    22  		draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
    23  		return dst
    24  	}
    25  }
    26  
    27  func partiallyOpaqueGen(w, h int) func() image.Image {
    28  	return func() image.Image {
    29  		dst := image.NewRGBA(image.Rect(0, 0, w, h))
    30  		draw.Draw(dst, image.Rect(0, 0, w/2, h/2), image.NewUniform(color.White), image.Point{}, draw.Src)
    31  		return dst
    32  	}
    33  }
    34  
    35  func fullyTransparentGen(w, h int) func() image.Image {
    36  	return func() image.Image {
    37  		return image.NewRGBA(image.Rect(0, 0, w, h))
    38  	}
    39  }
    40  
    41  func fullyOpaquePaletteGen(w, h int) func() image.Image {
    42  	return func() image.Image {
    43  		return image.NewPaletted(image.Rect(0, 0, w, h), []color.Color{image.White})
    44  	}
    45  }
    46  
    47  func fullyTransparentPaletteGen(w, h int) func() image.Image {
    48  	return func() image.Image {
    49  		return image.NewPaletted(image.Rect(0, 0, w, h), []color.Color{image.Transparent})
    50  	}
    51  }
    52  
    53  var tcs = []struct {
    54  	name   string
    55  	imgGen func() image.Image
    56  }{
    57  	{
    58  		"10MPx fully transparent RGBA",
    59  		fullyTransparentGen(1000, 1000),
    60  	},
    61  	{
    62  		"10MPx partially opaque RGBA",
    63  		partiallyOpaqueGen(1000, 1000),
    64  	},
    65  	{
    66  		"10MPx fully opaque RGBA",
    67  		fullyOpaqueGen(1000, 1000),
    68  	},
    69  	{
    70  		"10MPx fully opaque palette",
    71  		fullyOpaquePaletteGen(1000, 1000),
    72  	},
    73  	{
    74  		"10MPx fully transparent palette",
    75  		fullyTransparentPaletteGen(1000, 1000),
    76  	},
    77  }
    78  
    79  func BenchmarkFillImageTransparency(b *testing.B) {
    80  	for _, tc := range tcs {
    81  		b.Run(tc.name, func(b *testing.B) {
    82  			for i := 0; i < b.N; i++ {
    83  				b.StopTimer()
    84  				img := tc.imgGen()
    85  				b.StartTimer()
    86  				FillImageTransparency(img, image.White)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func BenchmarkFillImageTransparencyOld(b *testing.B) {
    93  	for _, tc := range tcs {
    94  		b.Run(tc.name, func(b *testing.B) {
    95  			for i := 0; i < b.N; i++ {
    96  				b.StopTimer()
    97  				img := tc.imgGen()
    98  				b.StartTimer()
    99  				fillImageTransparencyOld(img, image.White)
   100  			}
   101  		})
   102  	}
   103  }