-
Notifications
You must be signed in to change notification settings - Fork 617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a StringList type to be used for commandline flags. #583
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2020 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package driver | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestString(t *testing.T) { | ||
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. It seems a bit different (at least to the best of my knowledge and in the context of golang) to test functions on a private type in golang. Testing the public interface would probably be preferable. But, I know that there are no tests for flag.go right now; will leave it up to @aalexand to decide what sort of testing is sufficient. 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. Ping to @nilayvaish and @aalexand. I'm not sure about this testing. It may be better than not having tests, but I'm a bit skeptical about testing only a private type, rather than the publicly available functions. 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. AFAICT, it is not possible to test the public interface without creating a separate process. Would you be OK with the _test.go file creating a new process with flags? Otherwise, I think we need to drop the tests all together. |
||
s1 := "test1" | ||
s2a := "test2a" | ||
s2b := "test2b" | ||
for _, tc := range []struct { | ||
sl stringList | ||
want string | ||
}{ | ||
{stringList([]*string{}), "[]"}, | ||
{stringList([]*string{&s1}), "[test1]"}, | ||
{stringList([]*string{&s2a, &s2b}), "[test2a test2b]"}, | ||
} { | ||
got := tc.sl.String() | ||
if got != tc.want { | ||
t.Errorf("want %s, got %s", tc.want, got) | ||
} | ||
} | ||
} | ||
|
||
func TestSet(t *testing.T) { | ||
s1 := "test1" | ||
s2a := "test2a" | ||
s2b := "test2b" | ||
for _, tc := range []struct { | ||
in stringList | ||
want []string | ||
add string | ||
}{ | ||
{stringList([]*string{}), []string{s1}, s1}, | ||
{stringList([]*string{&s2a}), []string{s2a, s2b}, s2b}, | ||
} { | ||
ok := tc.in.Set(tc.add) | ||
if ok != nil { | ||
t.Errorf("%v", ok) | ||
} | ||
if len(tc.in) != len(tc.want) { | ||
t.Errorf("want len %d, got len %d", len(tc.want), len(tc.in)) | ||
} | ||
for i, s := range tc.in { | ||
if *s != tc.want[i] { | ||
t.Errorf("want %s, got %s", tc.want[i], *s) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2020 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package profile | ||
|
||
// Implements methods to add pseudo locations to samples. | ||
|
||
import "fmt" | ||
|
||
func findUniqueLoc(p *Profile) uint64 { | ||
ids := make(map[uint64]bool) | ||
for _, loc := range p.Location { | ||
ids[loc.ID] = true | ||
} | ||
for id := uint64(1); ; id++ { | ||
if ok := ids[id]; !ok { | ||
return id | ||
} | ||
} | ||
} | ||
|
||
func findUniqueFun(p *Profile) uint64 { | ||
ids := make(map[uint64]bool) | ||
for _, fun := range p.Function { | ||
ids[fun.ID] = true | ||
} | ||
for id := uint64(1); ; id++ { | ||
if ok := ids[id]; !ok { | ||
return id | ||
} | ||
} | ||
} | ||
|
||
func addStrFun(p *Profile, tag string, sl []string) *Function { | ||
fun := new(Function) | ||
fun.ID = findUniqueFun(p) | ||
fun.Name = fmt.Sprint(tag, sl) | ||
p.Function = append(p.Function, fun) | ||
return fun | ||
} | ||
|
||
func addNumFun(p *Profile, tag string, nums []int64, units []string) *Function { | ||
fun := new(Function) | ||
fun.ID = findUniqueFun(p) | ||
for i, n := range nums { | ||
fun.Name = fun.Name + fmt.Sprintf(" %d %s", n, units[i]) | ||
} | ||
p.Function = append(p.Function, fun) | ||
return fun | ||
} | ||
|
||
func addStringLoc(p *Profile, tag string, sl []string) *Location { | ||
loc := new(Location) | ||
loc.ID = findUniqueLoc(p) | ||
var line Line | ||
line.Function = addStrFun(p, tag, sl) | ||
loc.Line = append(loc.Line, line) | ||
loc.IsFolded = false | ||
p.Location = append(p.Location, loc) | ||
return loc | ||
} | ||
|
||
func addNumLoc(p *Profile, tag string, nl []int64, nu []string) *Location { | ||
loc := new(Location) | ||
loc.ID = findUniqueLoc(p) | ||
var line Line | ||
line.Function = addNumFun(p, tag, nl, nu) | ||
loc.Line = append(loc.Line, line) | ||
loc.IsFolded = false | ||
p.Location = append(p.Location, loc) | ||
return loc | ||
} | ||
|
||
// AddPseudoLocs adds root and leaf locations for tags in a profile as | ||
// requested in the provided options. | ||
func (p *Profile) AddPseudoLocs(rootTags, leafTags []*string) (bool, bool) { | ||
addedRootLoc := false | ||
addedLeafLoc := false | ||
for _, s := range p.Sample { | ||
rootLocs := make([]*Location, 0) | ||
for _, tag := range rootTags { | ||
l1, ok1 := s.Label[*tag] | ||
l2, ok2 := s.NumLabel[*tag] | ||
l3 := s.NumUnit[*tag] | ||
if ok1 || ok2 { | ||
addedRootLoc = true | ||
var loc *Location | ||
if ok1 { | ||
loc = addStringLoc(p, *tag, l1) | ||
} else { | ||
loc = addNumLoc(p, *tag, l2, l3) | ||
} | ||
rootLocs = append(rootLocs, loc) | ||
} | ||
} | ||
s.Location = append(rootLocs, s.Location...) | ||
for _, tag := range leafTags { | ||
l1, ok1 := s.Label[*tag] | ||
l2, ok2 := s.NumLabel[*tag] | ||
l3 := s.NumUnit[*tag] | ||
if ok1 || ok2 { | ||
addedLeafLoc = true | ||
var loc *Location | ||
if ok1 { | ||
loc = addStringLoc(p, *tag, l1) | ||
} else { | ||
loc = addNumLoc(p, *tag, l2, l3) | ||
} | ||
s.Location = append(s.Location, loc) | ||
} | ||
} | ||
} | ||
return addedRootLoc, addedLeafLoc | ||
} |
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.
Couple of questions:
The option / flag is of type []string / multi-value. The examples are of the form "foo=val1,val2" What exactly the multi-value here? Is it the comma-separated multi-values as in example, i.e. "foo=val1" and "val2" as two values which is weird? Or is that there is ability to have multiple occurrences of each of these like "-flag=foo=val1,val2 -flag=bar=val3,val4" and the documentation needs to explain this?
How are numeric tags handled, including with units like memory? Can we reuse the same value format as in tagfocus / taghide etc.? I feel that we are introducing a new way to specify a set of values and I don't see why different tag* flags can't unify on a single way to do that.