feat add a go tool

This commit is contained in:
bing 2022-06-23 16:25:01 +08:00
parent c11342092b
commit 43a9ee0038
2 changed files with 139 additions and 1 deletions

1
.gitignore vendored
View File

@ -16,3 +16,4 @@
# vendor/
.vscode
*.log

137
go/readdelay/main.go Normal file
View File

@ -0,0 +1,137 @@
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
const (
Send = "二进制消息 2.6 kB"
Recieve = "二进制消息 3.9 kB"
Broadcast = "二进制消息 16 B"
)
type message struct {
Type int
Size int
Time int64
}
func time2int64(t string)int64{
strs := strings.Split(t, ":")
if len(strs) != 3 {
return 0
}
hour := strs[0]
minute := strs[1]
seconds := strings.Split(strs[2], ".")
if len(seconds) != 2 {
return 0
}
second := seconds[0]
mill := seconds[1]
ms, err := strconv.ParseInt(mill, 10, 64)
if err != nil {
log.Println(err)
return 0
}
h, err := strconv.ParseInt(hour, 10, 64)
if err != nil {
log.Println(err)
return 0
}
ms = ms + h *3600*1000
m, err := strconv.ParseInt(minute, 10, 64)
if err != nil {
log.Println(err)
return 0
}
ms = ms + m * 60 *1000
s, err := strconv.ParseInt(second, 10, 64)
if err != nil {
log.Println(err)
return 0
}
ms = ms + s *1000
return ms
}
func readdelay(file string)([]message, error){
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
reader := bufio.NewReader(f)
res := []message{}
t := 0
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
return res, nil
}
return res, err
}
line = strings.Trim(strings.Trim(strings.Trim(line, " "), "\t"), "\n")
switch {
case strings.Contains(line, Send):
t = 1
case strings.Contains(line, Recieve):
t = 2
case strings.Contains(line, Broadcast):
t = 3
default:
msg := message{Type: t, Time: time2int64(line)}
switch t {
case 1 :
res = append(res, msg)
case 2:
res = append(res, msg)
}
t = 0
}
}
}
func avg(delays ...int64)int64{
if len(delays) == 0 {
return 0
}
var total int64
for _, d := range delays {
total = total + d
}
return total/int64(len(delays))
}
func main(){
var file string
flag.StringVar(&file, "f", "delay.log", "ssss")
flag.Parse()
messages, err := readdelay(file)
if err != nil {
log.Fatal(err)
}
log.Println("got ", len(messages), " messages")
delays := []int64{}
var start int64
for _, msg := range messages {
if msg.Type == 1 {
start = msg.Time
}
if start != 0 && msg.Type == 2 {
delay := msg.Time - start
start = 0
delays = append(delays, delay)
}
}
fmt.Println(avg(delays...))
}