This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Improve performance of SetTags #1158
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7387d22
Add tests and benchmarks for SetTags
shanson7 832ccf3
SetTags - Replace SplitN calls with explicit tokenization
shanson7 b637429
Minor cleanup of SetTags
shanson7 35b1e5d
Add flag for SetTags reusability
shanson7 b6a848d
Use += to build test Target
shanson7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -2,6 +2,8 @@ package models | |
|
||
import ( | ||
"encoding/json" | ||
"math/rand" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/raintank/schema" | ||
|
@@ -90,3 +92,106 @@ func TestJsonMarshal(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
func TestSetTags(t *testing.T) { | ||
cases := []struct { | ||
in Series | ||
out map[string]string | ||
}{ | ||
{ | ||
in: Series{}, | ||
out: map[string]string{ | ||
"name": "", | ||
}, | ||
}, | ||
{ | ||
in: Series{ | ||
Target: "a", | ||
}, | ||
out: map[string]string{ | ||
"name": "a", | ||
}, | ||
}, | ||
{ | ||
in: Series{ | ||
Target: `a\b`, | ||
}, | ||
out: map[string]string{ | ||
"name": `a\b`, | ||
}, | ||
}, | ||
{ | ||
in: Series{ | ||
Target: "a;b=c;c=d", | ||
}, | ||
out: map[string]string{ | ||
"name": "a", | ||
"b": "c", | ||
"c": "d", | ||
}, | ||
}, | ||
{ | ||
in: Series{ | ||
Target: "a;biglongtagkeyhere=andithasabiglongtagvaluetoo;c=d", | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
out: map[string]string{ | ||
"name": "a", | ||
"biglongtagkeyhere": "andithasabiglongtagvaluetoo", | ||
"c": "d", | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c.in.SetTags() | ||
if !reflect.DeepEqual(c.out, c.in.Tags) { | ||
t.Fatalf("SetTags incorrect\nexpected:%v\ngot: %v\n", c.out, c.in.Tags) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkSetTags_00tags_00chars(b *testing.B) { | ||
benchmarkSetTags(b, 0, 0, 0, true) | ||
} | ||
|
||
func BenchmarkSetTags_20tags_32chars(b *testing.B) { | ||
benchmarkSetTags(b, 20, 32, 32, true) | ||
} | ||
|
||
func BenchmarkSetTags_20tags_32chars_reused(b *testing.B) { | ||
benchmarkSetTags(b, 20, 32, 32, false) | ||
} | ||
|
||
func benchmarkSetTags(b *testing.B, numTags, tagKeyLength, tagValueLength int, resetTags bool) { | ||
in := Series{ | ||
Target: "my.metric.name", | ||
} | ||
|
||
for i := 0; i < numTags; i++ { | ||
in.Target += ";" + randString(tagKeyLength) + "=" + randString(tagValueLength) | ||
} | ||
|
||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
in.SetTags() | ||
if len(in.Tags) != numTags+1 { | ||
b.Fatalf("Expected %d tags, got %d, target = %s, tags = %v", numTags+1, len(in.Tags), in.Target, in.Tags) | ||
} | ||
if resetTags { | ||
// Reset so as to not game the allocations | ||
in.Tags = nil | ||
} | ||
} | ||
b.SetBytes(int64(len(in.Target))) | ||
} | ||
|
||
func randString(n int) string { | ||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))] | ||
} | ||
return string(b) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean with invalid? one specified by the user? maybe we should refer to those as 'illegal' or even clarify as
overwrite any "name" tag that may have been illegally specified in the series tags
. or actually drop the 'illegal' because i don't think it's written anywhere that it's illegal.