From e0da8308e2e9adf9c326b41e91ea1a1437b42629 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Wed, 16 Jan 2019 16:15:50 +0100 Subject: [PATCH] worker --- mapblockrenderer/renderer_test.go | 48 ++++++++++++------------------- mapblockrenderer/worker.go | 29 +++++++++---------- 2 files changed, 33 insertions(+), 44 deletions(-) diff --git a/mapblockrenderer/renderer_test.go b/mapblockrenderer/renderer_test.go index a177002..b35eafa 100644 --- a/mapblockrenderer/renderer_test.go +++ b/mapblockrenderer/renderer_test.go @@ -2,7 +2,7 @@ package mapblockrenderer import ( "fmt" - "image/png" + "github.com/sirupsen/logrus" "io/ioutil" "mapserver/colormapping" "mapserver/coords" @@ -11,32 +11,8 @@ import ( "mapserver/testutils" "os" "testing" - "time" - - "github.com/sirupsen/logrus" ) -type JobData struct { - pos1, pos2 coords.MapBlockCoords - x, z int -} - -func worker(r *MapBlockRenderer, jobs <-chan JobData) { - for d := range jobs { - img, _ := r.Render(d.pos1, d.pos2) - - if img != nil { - f, _ := os.Create(fmt.Sprintf("../output/image_%d_%d.png", d.x, d.z)) - start := time.Now() - png.Encode(f, img) - f.Close() - t := time.Now() - elapsed := t.Sub(start) - logrus.WithFields(logrus.Fields{"elapsed": elapsed}).Debug("Encoding completed") - } - } -} - func TestSimpleRender(t *testing.T) { logrus.SetLevel(logrus.InfoLevel) @@ -67,10 +43,23 @@ func TestSimpleRender(t *testing.T) { r := NewMapBlockRenderer(cache, c) os.Mkdir("../output", 0755) + results := make(chan JobResult, 100) jobs := make(chan JobData, 100) - go worker(&r, jobs) - go worker(&r, jobs) - go worker(&r, jobs) + + go Worker(&r, jobs, results) + go Worker(&r, jobs, results) + go Worker(&r, jobs, results) + + go func() { + for result := range results { + if result.Data.Len() == 0 { + continue + } + f, _ := os.Create(fmt.Sprintf("../output/image_%d_%d.png", result.Job.X, result.Job.Z)) + result.Data.WriteTo(f) + f.Close() + } + }() from := -10 to := 10 @@ -80,10 +69,11 @@ func TestSimpleRender(t *testing.T) { pos1 := coords.NewMapBlockCoords(x, 10, z) pos2 := coords.NewMapBlockCoords(x, -1, z) - jobs <- JobData{pos1: pos1, pos2: pos2, x: x, z: z} + jobs <- JobData{Pos1: pos1, Pos2: pos2, X: x, Z: z} } } close(jobs) + close(results) } diff --git a/mapblockrenderer/worker.go b/mapblockrenderer/worker.go index 1f01cfb..c68d3b5 100644 --- a/mapblockrenderer/worker.go +++ b/mapblockrenderer/worker.go @@ -1,11 +1,10 @@ package mapblockrenderer import ( - "time" - "bytes" - "mapserver/coords" - "image/png" - + "bytes" + "image/png" + "mapserver/coords" + "time" ) type JobData struct { @@ -14,27 +13,27 @@ type JobData struct { } type JobResult struct { - Data *bytes.Buffer - Duration time.Duration - Job JobData + Data *bytes.Buffer + Duration time.Duration + Job JobData } -func worker(r *MapBlockRenderer, jobs <-chan JobData, results chan<- JobResult) { +func Worker(r *MapBlockRenderer, jobs <-chan JobData, results chan<- JobResult) { for d := range jobs { img, _ := r.Render(d.Pos1, d.Pos2) - w := new(bytes.Buffer) - start := time.Now() + w := new(bytes.Buffer) + start := time.Now() if img != nil { png.Encode(w, img) } - t := time.Now() - elapsed := t.Sub(start) + t := time.Now() + elapsed := t.Sub(start) - res := JobResult{Data: w, Duration: elapsed, Job: d} - results <- res + res := JobResult{Data: w, Duration: elapsed, Job: d} + results <- res } }