Skip to content
Snippets Groups Projects
Verified Commit 3ee6b58a authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Measure processing time more precisely

parent 63a74637
No related branches found
No related tags found
No related merge requests found
Pipeline #2718 passed
...@@ -30,10 +30,15 @@ var imageCounter = promauto.NewCounterVec( ...@@ -30,10 +30,15 @@ var imageCounter = promauto.NewCounterVec(
var imageCounterSuccess = imageCounter.WithLabelValues("success") var imageCounterSuccess = imageCounter.WithLabelValues("success")
var imageCounterFailure = imageCounter.WithLabelValues("failure") var imageCounterFailure = imageCounter.WithLabelValues("failure")
var imageProcessDuration = promauto.NewCounter(prometheus.CounterOpts{ var imageProcessDuration = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "imghost_process_duration", Name: "imghost_process_duration",
Help: "The total amount of time spent processing images", Help: "The amount of time spent processing images",
}) }, []string{"task"})
var imageProcessDurationRead = imageProcessDuration.WithLabelValues("read")
var imageProcessDurationClone = imageProcessDuration.WithLabelValues("clone")
var imageProcessDurationCrop = imageProcessDuration.WithLabelValues("crop")
var imageProcessDurationResize = imageProcessDuration.WithLabelValues("resize")
var imageProcessDurationWrite = imageProcessDuration.WithLabelValues("write")
func main() { func main() {
config := NewConfigFromEnv() config := NewConfigFromEnv()
......
...@@ -5,21 +5,21 @@ import ( ...@@ -5,21 +5,21 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
) )
func trackTime(start time.Time, function func(time.Duration)) { func trackTimeSince(counter prometheus.Counter, start time.Time) time.Time {
function(time.Now().UTC().Sub(start.UTC())) now := time.Now().UTC()
counter.Add(float64(now.Sub(start).Milliseconds()) / 1000.0)
return now
} }
func ProcessImage(ctx context.Context, config *Config, client *redis.Client, value string) { func ProcessImage(ctx context.Context, config *Config, client *redis.Client, value string) {
queueGaugeInProgress.Inc() queueGaugeInProgress.Inc()
defer queueGaugeInProgress.Dec() defer queueGaugeInProgress.Dec()
defer trackTime(time.Now(), func(duration time.Duration) {
imageProcessDuration.Add(float64(duration.Milliseconds()))
})
image := Image{} image := Image{}
if err := json.Unmarshal([]byte(value), &image); err != nil { if err := json.Unmarshal([]byte(value), &image); err != nil {
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"github.com/justjanne/imgconv" "github.com/justjanne/imgconv"
"gopkg.in/gographics/imagick.v3/imagick" "gopkg.in/gographics/imagick.v3/imagick"
"path/filepath" "path/filepath"
"time"
) )
func ResizeImage(config *Config, imageId string) []error { func ResizeImage(config *Config, imageId string) []error {
...@@ -13,6 +14,7 @@ func ResizeImage(config *Config, imageId string) []error { ...@@ -13,6 +14,7 @@ func ResizeImage(config *Config, imageId string) []error {
wand := imagick.NewMagickWand() wand := imagick.NewMagickWand()
defer wand.Destroy() defer wand.Destroy()
startRead := time.Now().UTC()
if err = wand.ReadImage(filepath.Join(config.SourceFolder, imageId)); err != nil { if err = wand.ReadImage(filepath.Join(config.SourceFolder, imageId)); err != nil {
return []error{err} return []error{err}
} }
...@@ -20,20 +22,26 @@ func ResizeImage(config *Config, imageId string) []error { ...@@ -20,20 +22,26 @@ func ResizeImage(config *Config, imageId string) []error {
if originalImage, err = imgconv.NewImage(wand); err != nil { if originalImage, err = imgconv.NewImage(wand); err != nil {
return []error{err} return []error{err}
} }
trackTimeSince(imageProcessDurationRead, startRead)
return runMany(len(config.Sizes), func(index int) error { return runMany(len(config.Sizes), func(index int) error {
definition := config.Sizes[index] definition := config.Sizes[index]
path := filepath.Join(config.TargetFolder, fmt.Sprintf("%s%s", imageId, definition.Suffix)) path := filepath.Join(config.TargetFolder, fmt.Sprintf("%s%s", imageId, definition.Suffix))
startClone := time.Now().UTC()
image := originalImage.CloneImage() image := originalImage.CloneImage()
startCrop := trackTimeSince(imageProcessDurationClone, startClone)
if err := image.Crop(definition.Size); err != nil { if err := image.Crop(definition.Size); err != nil {
return err return err
} }
startResize := trackTimeSince(imageProcessDurationCrop, startCrop)
if err := image.Resize(definition.Size); err != nil { if err := image.Resize(definition.Size); err != nil {
return err return err
} }
startWrite := trackTimeSince(imageProcessDurationResize, startResize)
if err := image.Write(config.Quality, path); err != nil { if err := image.Write(config.Quality, path); err != nil {
return err return err
} }
trackTimeSince(imageProcessDurationWrite, startWrite)
return nil return nil
}) })
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment