This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change API to use an attr builder instead of a direct map.
- Loading branch information
Showing
18 changed files
with
423 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,87 @@ | ||
package gomol | ||
|
||
import ( | ||
"github.com/spaolacci/murmur3" | ||
|
||
"fmt" | ||
) | ||
|
||
type Attrs struct { | ||
attrs map[uint32]interface{} | ||
} | ||
|
||
func NewAttrs() *Attrs { | ||
return &Attrs{ | ||
attrs: make(map[uint32]interface{}), | ||
} | ||
} | ||
|
||
func (a *Attrs) mergeAttrs(attrs *Attrs) { | ||
if attrs == nil { | ||
return | ||
} | ||
for hash, val := range attrs.attrs { | ||
a.attrs[hash] = val | ||
} | ||
} | ||
|
||
func (a *Attrs) clone() *Attrs { | ||
attrs := NewAttrs() | ||
for hash, val := range a.attrs { | ||
attrs.attrs[hash] = val | ||
} | ||
return attrs | ||
} | ||
|
||
func (a *Attrs) SetAttr(key string, value interface{}) *Attrs { | ||
a.attrs[getAttrHash(key)] = value | ||
return a | ||
} | ||
|
||
func (a *Attrs) GetAttr(key string) interface{} { | ||
return a.attrs[getAttrHash(key)] | ||
} | ||
|
||
func (a *Attrs) RemoveAttr(key string) { | ||
delete(a.attrs, getAttrHash(key)) | ||
} | ||
|
||
func (a *Attrs) Attrs() map[string]interface{} { | ||
attrs := make(map[string]interface{}) | ||
for hash, val := range a.attrs { | ||
key, _ := getHashAttr(hash) | ||
attrs[key] = val | ||
} | ||
return attrs | ||
} | ||
|
||
type logAttr struct { | ||
Name string | ||
Value interface{} | ||
} | ||
|
||
var attrHashes = make(map[string]uint32) | ||
var hashAttrs = make(map[uint32]string) | ||
|
||
func getAttrHash(attr string) uint32 { | ||
if hash, ok := attrHashes[attr]; ok { | ||
return hash | ||
} | ||
|
||
hasher := murmur3.New32() | ||
hasher.Write([]byte(attr)) | ||
|
||
hash := hasher.Sum32() | ||
hashAttrs[hash] = attr | ||
attrHashes[attr] = hash | ||
|
||
return hash | ||
} | ||
|
||
func getHashAttr(hash uint32) (string, error) { | ||
if attr, ok := hashAttrs[hash]; ok { | ||
return attr, nil | ||
} | ||
|
||
return "", fmt.Errorf("Could not find attr for hash %v", hash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,13 @@ | ||
package gomol | ||
|
||
import . "gopkg.in/check.v1" | ||
|
||
func (s *GomolSuite) BenchmarkAttrChaining(c *C) { | ||
for idx := 0; idx < c.N; idx { | ||
NewAttrs(). | ||
SetAttr("attr1", "val1"). | ||
SetAttr("attr2", "val2"). | ||
SetAttr("attr3", 3). | ||
SetAttr("attr4", 4) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,21 @@ | ||
package gomol | ||
|
||
import . "gopkg.in/check.v1" | ||
|
||
func (s *GomolSuite) TestAttrsMergeNilAttrs(c *C) { | ||
attrs := NewAttrs() | ||
attrs.mergeAttrs(nil) | ||
} | ||
|
||
func (s *GomolSuite) TestAttrsChaining(c *C) { | ||
attrs := NewAttrs(). | ||
SetAttr("attr1", "val1"). | ||
SetAttr("attr2", "val2"). | ||
SetAttr("attr3", 3). | ||
SetAttr("attr4", 4) | ||
|
||
c.Check(attrs.attrs[getAttrHash("attr1")], Equals, "val1") | ||
c.Check(attrs.attrs[getAttrHash("attr2")], Equals, "val2") | ||
c.Check(attrs.attrs[getAttrHash("attr3")], Equals, 3) | ||
c.Check(attrs.attrs[getAttrHash("attr4")], Equals, 4) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,103 @@ | ||
package gomol | ||
|
||
import . "gopkg.in/check.v1" | ||
|
||
func (s *GomolSuite) BenchmarkBasicLogInsertion(c *C) { | ||
b := NewBase() | ||
b.SetAttr("attr1", 1234) | ||
|
||
l := newDefaultMemLogger() | ||
b.AddLogger(l) | ||
b.InitLoggers() | ||
|
||
for idx := 0; idx < c.N; idx { | ||
b.log( | ||
LevelDebug, | ||
NewAttrs(). | ||
SetAttr("attr2", 4321). | ||
SetAttr("attr3", "val3"). | ||
SetAttr("attr4", 1234), | ||
"test %v", | ||
1234) | ||
} | ||
b.ShutdownLoggers() | ||
} | ||
|
||
func (s *GomolSuite) BenchmarkBaseDbgm(c *C) { | ||
b := NewBase() | ||
b.SetAttr("attr1", 1234) | ||
|
||
l := newDefaultMemLogger() | ||
b.AddLogger(l) | ||
b.InitLoggers() | ||
|
||
for idx := 0; idx < c.N; idx { | ||
b.Dbgm( | ||
NewAttrs(). | ||
SetAttr("attr2", 4321). | ||
SetAttr("attr3", "val3"). | ||
SetAttr("attr4", 1234), | ||
"test %v", | ||
1234) | ||
} | ||
b.ShutdownLoggers() | ||
} | ||
|
||
func (s *GomolSuite) BenchmarkLogInsertionWithFilename(c *C) { | ||
base := NewBase() | ||
base.config.FilenameAttr = "filename" | ||
base.InitLoggers() | ||
for i := 0; i < c.N; i { | ||
base.log(LevelDebug, | ||
NewAttrs(). | ||
SetAttr("attr1", "val1"). | ||
SetAttr("attr2", "val2"). | ||
SetAttr("attr3", 3). | ||
SetAttr("attr4", 4), | ||
"msg %v %v", "string", 1234) | ||
} | ||
base.ShutdownLoggers() | ||
} | ||
func (s *GomolSuite) BenchmarkLogInsertionWithLineNumber(c *C) { | ||
base := NewBase() | ||
base.config.LineNumberAttr = "line" | ||
base.InitLoggers() | ||
for i := 0; i < c.N; i { | ||
base.log(LevelDebug, | ||
NewAttrs(). | ||
SetAttr("attr1", "val1"). | ||
SetAttr("attr2", "val2"). | ||
SetAttr("attr3", 3). | ||
SetAttr("attr4", 4), | ||
"msg %v %v", "string", 1234) | ||
} | ||
base.ShutdownLoggers() | ||
} | ||
func (s *GomolSuite) BenchmarkLogInsertionWithFilenameAndLineNumber(c *C) { | ||
base := NewBase() | ||
base.config.FilenameAttr = "filename" | ||
base.config.LineNumberAttr = "line" | ||
base.InitLoggers() | ||
for i := 0; i < c.N; i { | ||
base.log(LevelDebug, | ||
NewAttrs(). | ||
SetAttr("attr1", "val1"). | ||
SetAttr("attr2", "val2"). | ||
SetAttr("attr3", 3). | ||
SetAttr("attr4", 4), | ||
"msg %v %v", "string", 1234) | ||
} | ||
base.ShutdownLoggers() | ||
} | ||
|
||
func (s *GomolSuite) BenchmarkIsGomolCaller(c *C) { | ||
for i := 0; i < c.N; i { | ||
isGomolCaller("/home/gomoltest/some/sub/dir/that/is/long/filename.go") | ||
} | ||
} | ||
|
||
func (s *GomolSuite) BenchmarkGetCallerInfo(c *C) { | ||
for i := 0; i < c.N; i { | ||
getCallerInfo() | ||
} | ||
} |
Oops, something went wrong.