gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/metric/buckettool/buckettool.go (about) 1 // Copyright 2019 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // buckettool prints buckets for distribution metrics. 16 package main 17 18 import ( 19 "fmt" 20 "os" 21 "time" 22 23 "gvisor.dev/gvisor/pkg/log" 24 "gvisor.dev/gvisor/pkg/metric" 25 "gvisor.dev/gvisor/runsc/flag" 26 ) 27 28 var ( 29 typeFlag = flag.String("type", "duration", "Type of the bucketer: 'duration' or 'exponential'") 30 numFiniteBucketsFlag = flag.Int("num_finite_buckets", 8, "Number of finite buckets") 31 minDurationFlag = flag.Duration("min_duration", 5*time.Millisecond, "For -type=duration: Minimum duration") 32 maxDurationFlag = flag.Duration("max_duration", 10*time.Minute, "For -type=duration: Maximum duration") 33 widthFlag = flag.Uint64("exponential_width", 5, "For -type=exponential: Initial bucket width") 34 scaleFlag = flag.Float64("exponential_scale", 10, "For -type=exponential: Scaling factor") 35 growthFlag = flag.Float64("exponential_growth", 4, "For -type=exponential: Exponential growth factor") 36 ) 37 38 func exitf(format string, values ...any) { 39 log.Warningf(format, values...) 40 os.Exit(1) 41 } 42 43 func main() { 44 flag.Parse() 45 var bucketer metric.Bucketer 46 var formatVal func(int64) string 47 switch *typeFlag { 48 case "duration": 49 bucketer = metric.NewDurationBucketer(*numFiniteBucketsFlag, *minDurationFlag, *maxDurationFlag) 50 formatVal = func(val int64) string { 51 return time.Duration(val).String() 52 } 53 case "exponential": 54 bucketer = metric.NewExponentialBucketer(*numFiniteBucketsFlag, *widthFlag, *scaleFlag, *growthFlag) 55 formatVal = func(val int64) string { 56 return fmt.Sprintf("%v", val) 57 } 58 default: 59 exitf("Invalid -type: %s", *typeFlag) 60 } 61 fmt.Printf("Number of finite buckets: %d\n", bucketer.NumFiniteBuckets()) 62 fmt.Printf("Number of total buckets: %d\n", bucketer.NumFiniteBuckets()+2) 63 fmt.Printf("> Underflow bucket: (-inf; %s)\n", formatVal(bucketer.LowerBound(0))) 64 for b := 0; b < bucketer.NumFiniteBuckets(); b++ { 65 fmt.Printf("> Bucket index %d: [%s, %s). (Middle: %s)\n", b, formatVal(bucketer.LowerBound(b)), formatVal(bucketer.LowerBound(b+1)), formatVal((bucketer.LowerBound(b)+bucketer.LowerBound(b+1))/2)) 66 } 67 fmt.Printf("> Overflow bucket: [%s; +inf)\n", formatVal(bucketer.LowerBound(bucketer.NumFiniteBuckets()))) 68 }