feat(#15): add benchmark tests

master
Tomas Aparicio 11 years ago
parent 16576f49c9
commit bfe0e700ce

@ -56,7 +56,19 @@ Here you can see some performance test comparisons for multiple scenarios:
- [libvips speed and memory usage](http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use) - [libvips speed and memory usage](http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use)
- [sharp performance tests](https://github.com/lovell/sharp#the-task) - [sharp performance tests](https://github.com/lovell/sharp#the-task)
bimg performance tests coming soon! #### bimg performance tests
Tested using Go 1.4 and libvips-7.42.3 in OSX i7 2.7Ghz
```
PASS
BenchmarkResizeLargeJpeg 30 46652408 ns/op
BenchmarkResizePng 20 57387902 ns/op
BenchmarkResizeWebP 500 2453220 ns/op
BenchmarkConvertToJpeg 30 35556414 ns/op
BenchmarkCrop 30 51768475 ns/op
BenchmarkExtract 30 50866406 ns/op
ok 9.424s
```
## API ## API
@ -193,6 +205,12 @@ func Resize(buf []byte, o Options) ([]byte, error)
func Shutdown() func Shutdown()
``` ```
#### func Write
```go
func Write(path string, buf []byte) error
```
#### type Angle #### type Angle
```go ```go
@ -263,13 +281,13 @@ func (i *Image) Convert(t ImageType) ([]byte, error)
#### func (*Image) Crop #### func (*Image) Crop
```go ```go
func (i *Image) Crop(width int, height int) ([]byte, error) func (i *Image) Crop(width, height int) ([]byte, error)
``` ```
#### func (*Image) Extract #### func (*Image) Extract
```go ```go
func (i *Image) Extract(top int, left int, width int, height int) ([]byte, error) func (i *Image) Extract(top, left, width, height int) ([]byte, error)
``` ```
#### func (*Image) Flip #### func (*Image) Flip
@ -278,12 +296,6 @@ func (i *Image) Extract(top int, left int, width int, height int) ([]byte, error
func (i *Image) Flip() ([]byte, error) func (i *Image) Flip() ([]byte, error)
``` ```
#### func (*Image) Flop
```go
func (i *Image) Flop() ([]byte, error)
```
#### func (*Image) Metadata #### func (*Image) Metadata
```go ```go
@ -299,7 +311,7 @@ func (i *Image) Process(o Options) ([]byte, error)
#### func (*Image) Resize #### func (*Image) Resize
```go ```go
func (i *Image) Resize(width int, height int) ([]byte, error) func (i *Image) Resize(width, height int) ([]byte, error)
``` ```
#### func (*Image) Rotate #### func (*Image) Rotate
@ -314,6 +326,12 @@ func (i *Image) Rotate(a Angle) ([]byte, error)
func (i *Image) Size() (ImageSize, error) func (i *Image) Size() (ImageSize, error)
``` ```
#### func (*Image) Thumbnail
```go
func (i *Image) Thumbnail(pixels int) ([]byte, error)
```
#### func (*Image) Type #### func (*Image) Type
```go ```go
@ -325,10 +343,11 @@ func (i *Image) Type() string
```go ```go
type ImageMetadata struct { type ImageMetadata struct {
Orientation int Orientation int
Channels int
Alpha bool Alpha bool
Profile bool Profile bool
Space int
Type string Type string
Space string
Size ImageSize Size ImageSize
} }
``` ```
@ -407,6 +426,8 @@ func (i Interpolator) String() string
type Options struct { type Options struct {
Height int Height int
Width int Width int
AreaHeight int
AreaWidth int
Top int Top int
Left int Left int
Crop bool Crop bool
@ -423,6 +444,7 @@ type Options struct {
} }
``` ```
## License ## License
MIT - Tomas Aparicio MIT - Tomas Aparicio

@ -21,6 +21,7 @@ type ImageMetadata struct {
Size ImageSize Size ImageSize
} }
// Get the image size by width and height pixels
func Size(buf []byte) (ImageSize, error) { func Size(buf []byte) (ImageSize, error) {
metadata, err := Metadata(buf) metadata, err := Metadata(buf)
if err != nil { if err != nil {
@ -33,6 +34,7 @@ func Size(buf []byte) (ImageSize, error) {
}, nil }, nil
} }
// Extract the image metadata (size, type, alpha channel, profile, EXIF orientation...)
func Metadata(buf []byte) (ImageMetadata, error) { func Metadata(buf []byte) (ImageMetadata, error) {
defer C.vips_thread_shutdown() defer C.vips_thread_shutdown()

@ -134,7 +134,7 @@ func TestResizePngWithTransparency(t *testing.T) {
} }
} }
func benchmarkResize(file string, o Options, b *testing.B) { func runBenchmarkResize(file string, o Options, b *testing.B) {
buf, _ := Read(path.Join("fixtures", file)) buf, _ := Read(path.Join("fixtures", file))
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@ -147,7 +147,7 @@ func BenchmarkResizeLargeJpeg(b *testing.B) {
Width: 800, Width: 800,
Height: 600, Height: 600,
} }
benchmarkResize("test.jpg", options, b) runBenchmarkResize("test.jpg", options, b)
} }
func BenchmarkResizePng(b *testing.B) { func BenchmarkResizePng(b *testing.B) {
@ -155,7 +155,7 @@ func BenchmarkResizePng(b *testing.B) {
Width: 200, Width: 200,
Height: 200, Height: 200,
} }
benchmarkResize("test.png", options, b) runBenchmarkResize("test.png", options, b)
} }
func BenchmarkResizeWebP(b *testing.B) { func BenchmarkResizeWebP(b *testing.B) {
@ -163,5 +163,29 @@ func BenchmarkResizeWebP(b *testing.B) {
Width: 200, Width: 200,
Height: 200, Height: 200,
} }
benchmarkResize("test.webp", options, b) runBenchmarkResize("test.webp", options, b)
}
func BenchmarkConvertToJpeg(b *testing.B) {
options := Options{Type: JPEG}
runBenchmarkResize("test.png", options, b)
}
func BenchmarkCrop(b *testing.B) {
options := Options{
Width: 800,
Height: 600,
Crop: true,
}
runBenchmarkResize("test.jpg", options, b)
}
func BenchmarkExtract(b *testing.B) {
options := Options{
Top: 100,
Left: 50,
AreaWidth: 600,
AreaHeight: 480,
}
runBenchmarkResize("test.jpg", options, b)
} }

@ -11,14 +11,27 @@ const (
MAGICK MAGICK
) )
// Determines the image type format (jpeg, png, webp or tiff)
func DetermineImageType(buf []byte) ImageType { func DetermineImageType(buf []byte) ImageType {
return vipsImageType(buf) return vipsImageType(buf)
} }
// Determines the image type format by name (jpeg, png, webp or tiff)
func DetermineImageTypeName(buf []byte) string { func DetermineImageTypeName(buf []byte) string {
return getImageTypeName(vipsImageType(buf)) return getImageTypeName(vipsImageType(buf))
} }
// Check if a given image type is supported
func IsTypeSupported(t ImageType) bool {
return t == JPEG || t == PNG || t == WEBP
}
// Check if a given image type name is supported
func IsTypeNameSupported(t string) bool {
return t == "jpeg" || t == "jpg" ||
t == "png" || t == "webp"
}
func getImageTypeName(code ImageType) string { func getImageTypeName(code ImageType) string {
imageType := "unknown" imageType := "unknown"
@ -42,12 +55,3 @@ func getImageTypeName(code ImageType) string {
return imageType return imageType
} }
func IsTypeSupported(t ImageType) bool {
return t == JPEG || t == PNG || t == WEBP
}
func IsTypeNameSupported(t string) bool {
return t == "jpeg" || t == "jpg" ||
t == "png" || t == "webp"
}

@ -11,12 +11,6 @@ enum types {
MAGICK MAGICK
}; };
void
vips_malloc_cb(VipsObject *object, char *buf)
{
g_free(buf);
};
int int
vips_affine_interpolator(VipsImage *in, VipsImage **out, double a, double b, double c, double d, VipsInterpolate *interpolator) vips_affine_interpolator(VipsImage *in, VipsImage **out, double a, double b, double c, double d, VipsInterpolate *interpolator)
{ {
@ -157,10 +151,5 @@ vips_init_image(void *buf, size_t len, int imageType, VipsImage **out) {
#endif #endif
} }
// Listen for "postclose" signal to delete input buffer
//if (out != NULL) {
//g_signal_connect(out, "postclose", G_CALLBACK(vips_malloc_cb), buf);
//}
return code; return code;
}; };

Loading…
Cancel
Save