generated from bing/readnotes
137 lines
2.3 KiB
Go
137 lines
2.3 KiB
Go
|
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...))
|
||
|
}
|