mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-29 20:52:24 -05:00
[performance] add benchmarks for native Go imaging code, small tweaks to reduce nil and boundary checks, some loop unrolling (#4482)
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4482 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
b012a81f66
commit
baf2c54730
3 changed files with 689 additions and 304 deletions
157
internal/media/imaging_test.go
Normal file
157
internal/media/imaging_test.go
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
// GoToSocial
|
||||
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package media
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
func BenchmarkFlipH(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = flipH(img)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkFlipV(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = flipV(img)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkRotate90(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = rotate90(img)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkRotate180(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = rotate180(img)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkRotate270(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = rotate270(img)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkTranspose(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = transpose(img)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkTransverse(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = transverse(img)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkResizeHorizontalLinear(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = resizeHorizontalLinear(img, 64)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkResizeVerticalLinear(b *testing.B) {
|
||||
benchmarkFunc(b, func(img image.Image) {
|
||||
_ = resizeVerticalLinear(img, 64)
|
||||
})
|
||||
}
|
||||
|
||||
func benchmarkFunc(b *testing.B, fn func(image.Image)) {
|
||||
b.Helper()
|
||||
for _, testcase := range []struct {
|
||||
Path string
|
||||
Decode func(io.Reader) (image.Image, error)
|
||||
}{
|
||||
{
|
||||
Path: "./test/big-panda.gif",
|
||||
Decode: gif.Decode,
|
||||
},
|
||||
{
|
||||
Path: "./test/clock-original.gif",
|
||||
Decode: gif.Decode,
|
||||
},
|
||||
{
|
||||
Path: "./test/test-jpeg.jpg",
|
||||
Decode: jpeg.Decode,
|
||||
},
|
||||
{
|
||||
Path: "./test/test-png-noalphachannel.png",
|
||||
Decode: png.Decode,
|
||||
},
|
||||
{
|
||||
Path: "./test/test-png-alphachannel.png",
|
||||
Decode: png.Decode,
|
||||
},
|
||||
{
|
||||
Path: "./test/rainbow-original.png",
|
||||
Decode: png.Decode,
|
||||
},
|
||||
{
|
||||
Path: "./test/nb-flag-original.webp",
|
||||
Decode: webp.Decode,
|
||||
},
|
||||
} {
|
||||
file, err := openRead(testcase.Path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
img, err := testcase.Decode(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
file.Close()
|
||||
|
||||
testname := fmt.Sprintf("ext=%s type=%s size=%d",
|
||||
strings.TrimPrefix(path.Ext(testcase.Path), "."),
|
||||
strings.TrimPrefix(reflect.TypeOf(img).String(), "*image."),
|
||||
info.Size(),
|
||||
)
|
||||
|
||||
b.Run(testname, func(b *testing.B) {
|
||||
b.Helper()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
fn(img)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue