Skip to content

Commit

Permalink
bench for object package
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Nov 13, 2017
1 parent 61ea2bd commit 106fe63
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
19 changes: 9 additions & 10 deletions object/file_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type FileObject struct {
func NewFileObject(uri string, mortConfig *config.Config) (*FileObject, error) {
obj := FileObject{}
obj.Uri = uri
//obj.uriBytes = []byte(uri)
obj.CheckParent = false

err := obj.decode(mortConfig)
Expand All @@ -99,16 +100,17 @@ func NewFileObject(uri string, mortConfig *config.Config) (*FileObject, error) {
}

func (self *FileObject) decode(mortConfig *config.Config) error {
elements := strings.Split(self.Uri, "/")
elements := strings.SplitN(self.Uri, "/", 3)

self.Bucket = elements[1]
if len(elements) > 2 {
self.Key = "/" + strings.Join(elements[2:], "/")
self.Key = "/" + elements[2]
}


if bucket, ok := mortConfig.Buckets[self.Bucket]; ok {
err := self.decodeKey(bucket, mortConfig)
if self.HasTransform() {
if self.Transforms.NotEmpty {
self.Storage = bucket.Storages.Transform()
} else {
self.Storage = bucket.Storages.Basic()
Expand All @@ -129,10 +131,9 @@ func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Confi

trans := bucket.Transform
matches := trans.PathRegexp.FindStringSubmatch(self.Key)
if len(matches) < 3 {
if matches == nil {
return nil

}
}

subMatchMap := make(map[string]string, 2)

Expand All @@ -142,7 +143,7 @@ func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Confi
}
}
presetName := subMatchMap["presetName"] //string(matches[trans.Order.PresetName+1])
parent := "/" + subMatchMap["parent"] // "/" + string(matches[trans.Order.Parent+1])
parent := subMatchMap["parent"] // "/" + string(matches[trans.Order.Parent+1])

if _, ok := bucket.Transform.Presets[presetName]; !ok {
log.Log().Warnw("FileObject decodeKey unknown preset", "obj.Key", self.Key, "parent", parent, "presetName", presetName, "regexp", trans.Path)
Expand All @@ -156,9 +157,7 @@ func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Confi
}


if bucket.Transform.ParentBucket != "" {
parent = "/" + path.Join(bucket.Transform.ParentBucket, parent)
}
parent = "/" + path.Join(bucket.Transform.ParentBucket, parent)

parentObj, err := NewFileObject(parent, mortConfig)
parentObj.Storage = bucket.Storages.Get(bucket.Transform.ParentStorage)
Expand Down
39 changes: 27 additions & 12 deletions object/file_object_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package object

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"

"mort/config"
"mort/log"
"mort/transforms"
)

var imageInfo = transforms.ImageInfo{}

func TestMain(m *testing.M) {
logger, _ := zap.NewDevelopment()
zap.ReplaceGlobals(logger)
log.RegisterLogger(logger.Sugar())
code := m.Run()
defer logger.Sync()
os.Exit(code)
}

func TestNewFileObjectWhenUnknowBucket(t *testing.T) {
mortConfig := config.GetInstance()
_, err := NewFileObject("/bucket/path", mortConfig)
Expand Down Expand Up @@ -166,3 +154,30 @@ func TestNewFileObjecWithNestedParent(t *testing.T) {

assert.Equal(t, "/parent.jpg", parent.Parent.Key, "parent of parent should have correct path")
}

func BenchmarkNewFileObject(b *testing.B) {

benchmarks := []struct{
path string
configPath string
} {
{"/bucket/width/thumb_121332.jpg", "testdata/bucket-transform-parent-storage.yml"},
{"/bucket/parent.jpg", "testdata/bucket-transform.yml"},
}

b.ReportAllocs()
for _, bm := range benchmarks {
config := config.Config{}
err := config.Load(bm.configPath)
if err != nil {
panic(err)
}

b.Run(bm.path, func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewFileObject(bm.path, &config)
}
})
}

}
1 change: 1 addition & 0 deletions object/testdata/bucket-transform-parent-storage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ buckets:
kind: "presets"
parentStorage: "other"
parentBucket: "bucket"
resultKey: "hash"
presets:
blog_small:
quality: 75
Expand Down
2 changes: 1 addition & 1 deletion processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type RequestProcessor struct {
func (r *RequestProcessor) Init(max int, l lock.Lock) {
r.queue = make(chan requestMessage, max)
r.collapse = l
r.throttler = throttler.New(10)
r.throttler = throttler.New(max)
}

func (r *RequestProcessor) Process(req *http.Request, obj *object.FileObject) *response.Response{
Expand Down

0 comments on commit 106fe63

Please sign in to comment.