forked from MTSR/mapserver
worker
This commit is contained in:
parent
b090d81f47
commit
e0da8308e2
@ -2,7 +2,7 @@ package mapblockrenderer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/png"
|
"github.com/sirupsen/logrus"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mapserver/colormapping"
|
"mapserver/colormapping"
|
||||||
"mapserver/coords"
|
"mapserver/coords"
|
||||||
@ -11,32 +11,8 @@ import (
|
|||||||
"mapserver/testutils"
|
"mapserver/testutils"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"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) {
|
func TestSimpleRender(t *testing.T) {
|
||||||
logrus.SetLevel(logrus.InfoLevel)
|
logrus.SetLevel(logrus.InfoLevel)
|
||||||
|
|
||||||
@ -67,10 +43,23 @@ func TestSimpleRender(t *testing.T) {
|
|||||||
r := NewMapBlockRenderer(cache, c)
|
r := NewMapBlockRenderer(cache, c)
|
||||||
os.Mkdir("../output", 0755)
|
os.Mkdir("../output", 0755)
|
||||||
|
|
||||||
|
results := make(chan JobResult, 100)
|
||||||
jobs := make(chan JobData, 100)
|
jobs := make(chan JobData, 100)
|
||||||
go worker(&r, jobs)
|
|
||||||
go worker(&r, jobs)
|
go Worker(&r, jobs, results)
|
||||||
go worker(&r, jobs)
|
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
|
from := -10
|
||||||
to := 10
|
to := 10
|
||||||
@ -80,10 +69,11 @@ func TestSimpleRender(t *testing.T) {
|
|||||||
pos1 := coords.NewMapBlockCoords(x, 10, z)
|
pos1 := coords.NewMapBlockCoords(x, 10, z)
|
||||||
pos2 := coords.NewMapBlockCoords(x, -1, 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(jobs)
|
||||||
|
close(results)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package mapblockrenderer
|
package mapblockrenderer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"bytes"
|
||||||
"bytes"
|
"image/png"
|
||||||
"mapserver/coords"
|
"mapserver/coords"
|
||||||
"image/png"
|
"time"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type JobData struct {
|
type JobData struct {
|
||||||
@ -14,27 +13,27 @@ type JobData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type JobResult struct {
|
type JobResult struct {
|
||||||
Data *bytes.Buffer
|
Data *bytes.Buffer
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
Job JobData
|
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 {
|
for d := range jobs {
|
||||||
img, _ := r.Render(d.Pos1, d.Pos2)
|
img, _ := r.Render(d.Pos1, d.Pos2)
|
||||||
|
|
||||||
w := new(bytes.Buffer)
|
w := new(bytes.Buffer)
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
if img != nil {
|
if img != nil {
|
||||||
png.Encode(w, img)
|
png.Encode(w, img)
|
||||||
}
|
}
|
||||||
|
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
elapsed := t.Sub(start)
|
elapsed := t.Sub(start)
|
||||||
|
|
||||||
res := JobResult{Data: w, Duration: elapsed, Job: d}
|
res := JobResult{Data: w, Duration: elapsed, Job: d}
|
||||||
results <- res
|
results <- res
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user