feat(lab5): time pipeline stages and centralize variant constants

This commit is contained in:
Mikhail Verkovykh
2026-11-20 19:05:27 +03:00
parent 1ce93185af
commit d0f533b371
5 changed files with 121 additions and 36 deletions
+66 -30
View File
@@ -1,4 +1,4 @@
// Package pipeline turns one validated original image into the required lab5
// Package pipeline turns one validated original image into the required
// variants. It is deterministic and side-effect free so a broker redelivery can
// simply overwrite the same keys with the same bytes.
package pipeline
@@ -15,23 +15,16 @@ import (
"image/jpeg"
_ "image/png"
"os"
"time"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/config"
"github.com/gen2brain/webp"
"golang.org/x/image/draw"
"golang.org/x/sync/semaphore"
)
const (
ThumbnailMax = 256
CropSize = 128
OptimizedMax = 1280
// Re-encoding drops EXIF/XMP/ICC payloads from variants. The original is
// copied byte-for-byte and keeps its metadata and checksum.
JPEGQuality = 85
WebPQuality = 82
WebPMethod = 4
)
// Re-encoding drops EXIF/XMP/ICC payloads from variants. The original is
// copied byte-for-byte and keeps its metadata and checksum.
type Processor struct {
maxPixels int64
@@ -83,29 +76,72 @@ func transient(code string, err error) *Failure {
return &Failure{Permanent: false, Code: code, Err: err}
}
// Stats separates decode, pixel scaling and the two encoders. A single
// processing_ms number cannot answer whether a large image costs more because
// of resampling or because of JPEG/WebP compression.
type Stats struct {
Decode time.Duration
Scale time.Duration
JPEGEncode time.Duration
WebPEncode time.Duration
}
func (s Stats) Total() time.Duration {
return s.Decode + s.Scale + s.JPEGEncode + s.WebPEncode
}
// Process decodes the file once and returns all variants in memory. Worker
// concurrency and IMAGE_DECODE_MEMORY_MB bound the number of simultaneous
// decoded images, which is the memory-heavy part of the experiment.
func (p *Processor) Process(ctx context.Context, file *os.File) ([]Encoded, error) {
// decoded images, which is the memory-heavy part of the pipeline.
func (p *Processor) Process(ctx context.Context, file *os.File) ([]Encoded, Stats, error) {
var stats Stats
started := time.Now()
img, err := p.decode(ctx, file)
stats.Decode = time.Since(started)
if err != nil {
return nil, err
return nil, stats, err
}
thumbnail, err := renderJPEG("thumbnail", fitOnWhite(img, ThumbnailMax, ThumbnailMax))
started = time.Now()
thumbnailPixels := fitOnWhite(img, config.ThumbnailMax, config.ThumbnailMax)
stats.Scale += time.Since(started)
thumbnail, encode, err := renderJPEGTimed("thumbnail", thumbnailPixels)
stats.JPEGEncode += encode
if err != nil {
return nil, permanent("processing_failed", err)
}
crop, err := renderJPEG("crop", centerSquare(img, CropSize))
if err != nil {
return nil, permanent("processing_failed", err)
}
optimized, err := renderWebP(fitOnWhite(img, OptimizedMax, OptimizedMax))
if err != nil {
return nil, permanent("processing_failed", err)
return nil, stats, permanent("processing_failed", err)
}
return []Encoded{thumbnail, crop, optimized}, nil
started = time.Now()
cropPixels := centerSquare(img, config.CropSize)
stats.Scale += time.Since(started)
crop, encode, err := renderJPEGTimed("crop", cropPixels)
stats.JPEGEncode += encode
if err != nil {
return nil, stats, permanent("processing_failed", err)
}
started = time.Now()
optimizedPixels := fitOnWhite(img, config.OptimizedMax, config.OptimizedMax)
stats.Scale += time.Since(started)
optimized, encode, err := renderWebPTimed(optimizedPixels)
stats.WebPEncode += encode
if err != nil {
return nil, stats, permanent("processing_failed", err)
}
return []Encoded{thumbnail, crop, optimized}, stats, nil
}
func renderJPEGTimed(name string, img image.Image) (Encoded, time.Duration, error) {
started := time.Now()
encoded, err := renderJPEG(name, img)
return encoded, time.Since(started), err
}
func renderWebPTimed(img image.Image) (Encoded, time.Duration, error) {
started := time.Now()
encoded, err := renderWebP(img)
return encoded, time.Since(started), err
}
func (p *Processor) decode(ctx context.Context, file *os.File) (image.Image, error) {
@@ -127,7 +163,7 @@ func (p *Processor) decode(ctx context.Context, file *os.File) (image.Image, err
return nil, permanent("invalid_image", errors.New("image exceeds MAX_IMAGE_PIXELS"))
}
// AGENT: the semaphore is acquired before Decode, not after. A decoder
// The semaphore is acquired before Decode, not after. A decoder
// allocates roughly width*height*4 bytes; without this gate several 40 MP
// files can OOM the worker even with a small HTTP concurrency.
weight := pixels * 4
@@ -176,7 +212,7 @@ func fitOnWhite(src image.Image, maxWidth, maxHeight int) *image.RGBA {
return dst
}
// centerSquare always produces CropSize x CropSize. Unlike thumbnails, the
// centerSquare always produces config.CropSize x config.CropSize. Unlike thumbnails, the
// contract allows upscaling tiny sources for the crop variant.
func centerSquare(src image.Image, size int) *image.RGBA {
bounds := src.Bounds()
@@ -216,7 +252,7 @@ func fitSize(width, height, maxWidth, maxHeight int) (int, int) {
func renderJPEG(name string, img image.Image) (Encoded, error) {
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: JPEGQuality}); err != nil {
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: config.JPEGQuality}); err != nil {
return Encoded{}, err
}
bounds := img.Bounds()
@@ -232,7 +268,7 @@ func renderJPEG(name string, img image.Image) (Encoded, error) {
func renderWebP(img image.Image) (Encoded, error) {
var buf bytes.Buffer
if err := webp.Encode(&buf, img, webp.Options{Quality: WebPQuality, Method: WebPMethod}); err != nil {
if err := webp.Encode(&buf, img, webp.Options{Quality: config.WebPQuality, Method: config.WebPMethod}); err != nil {
return Encoded{}, err
}
bounds := img.Bounds()