pprof have not finished yet, and flag is finish

main
Your Name 2024-07-15 21:36:11 +08:00
parent 641b73c56c
commit ed2d2c6466
10 changed files with 182 additions and 5 deletions

22
flag/flag_type.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"flag"
"fmt"
"time"
)
// flag获取命令行参数
func main() {
//创建一个标志位参数
name := flag.String("name", "wangao", "define name")
age := flag.Int("age", 9000, "define age")
married := flag.Bool("married", false, "define married")
mTime := flag.Duration("mtime", time.Second, "define mTime") //命令行接受的参数需要带单位例如1000h:main.exe -mtime 1000h
//使用flag
flag.Parse()
fmt.Println(*name, *age, *married, mTime)
fmt.Println(flag.Args()) //返回命令行参数后的其他参数,以[]string类型返回
fmt.Println(flag.NArg()) // 返回命令行的其他参数个数
fmt.Println(flag.NFlag()) //返回使用的命令行参数个数
}

14
flag/flag_typevar.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"flag"
"fmt"
)
func main() {
var name string
flag.StringVar(&name, "name", "wangao", "define name")
flag.Parse()
fmt.Println(name)
}

11
flag/os.args.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"fmt"
"os"
)
// os.Args 获取命令行参数
func main() {
fmt.Printf("%#v", os.Args) //获取当前的执行文件和参数
}

View File

@ -7,7 +7,7 @@ import (
"course/networkProgram/protocol"
)
// socket_stick/client/main.go
// socket_stick/client/os.args.go
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:30000")

View File

@ -8,7 +8,7 @@ import (
"net"
)
// socket_stick/server/main.go
// socket_stick/server/os.args.go
func proc(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)

55
pprof/pprof.go Normal file
View File

@ -0,0 +1,55 @@
// runtime_pprof/os.args.go
package main
import (
"flag"
"fmt"
"os"
"runtime/pprof"
"time"
)
// 一段有问题的代码
func logicCode() {
var c chan int //nil 未初始化
for {
select {
case v := <-c: //阻塞
fmt.Printf("recv from chan, value:%v\n", v)
default:
}
}
}
func main() {
var isCPUPprof bool
var isMemPprof bool
flag.BoolVar(&isCPUPprof, "cpu", false, "turn cpu pprof on")
flag.BoolVar(&isMemPprof, "mem", false, "turn mem pprof on")
flag.Parse()
if isCPUPprof {
file, err := os.Create("./cpu.pprof")
if err != nil {
fmt.Printf("create cpu pprof failed, err:%v\n", err)
return
}
pprof.StartCPUProfile(file)
defer pprof.StopCPUProfile()
}
for i := 0; i < 8; i++ {
go logicCode()
}
time.Sleep(20 * time.Second)
if isMemPprof {
file, err := os.Create("./mem.pprof")
if err != nil {
fmt.Printf("create mem pprof failed, err:%v\n", err)
return
}
pprof.WriteHeapProfile(file)
file.Close()
}
}

View File

@ -0,0 +1,27 @@
package Splitstring
import (
"reflect"
"testing"
)
//测试组
func TestGroupSplit(t *testing.T) {
type testCase struct {
str string
sep string
want []string
}
testGroup := []testCase{
{str: "babcbef", sep: "b", want: []string{"", "a", "c", "ef"}},
{str: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
{str: "abcef", sep: "bc", want: []string{"a", "ef"}},
{str: "沙河有沙又有河", sep: "有", want: []string{"沙河", "沙又", "河"}},
}
for i, testcase := range testGroup {
if !reflect.DeepEqual(Split(testcase.str, testcase.sep), testcase.want) {
t.Errorf("testcase %d failed,want: %v got: %v", i+1, testcase.want, Split(testcase.str, testcase.sep))
}
}
}

View File

@ -0,0 +1,30 @@
package Splitstring
import (
"reflect"
"testing"
)
//子测试
func TestGroupChildSplit(t *testing.T) {
type testCase struct {
str string
sep string
want []string
}
testGroup := map[string]testCase{
"case1": {str: "babcbef", sep: "b", want: []string{"", "a", "c", "ef"}},
"case2": {str: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
"case3": {str: "abcef", sep: "bc", want: []string{"a", "ef"}},
"case4": {str: "沙河有沙又有河", sep: "有", want: []string{"沙河", "沙又", "河"}},
}
for name, testcase := range testGroup {
t.Run(name, func(t *testing.T) {
got := Split(testcase.str, testcase.sep)
if !reflect.DeepEqual(got, testcase.want) {
t.Errorf("got:%v, want:%v", got, testcase.want)
}
})
}
}

View File

@ -1,6 +1,8 @@
package Splitstring
import "strings"
import (
"strings"
)
//切割字符串
//example
@ -21,11 +23,11 @@ import "strings"
// abc,b =>[a,c]
func Split(str string, sep string) []string {
ret := make([]string, 0)
var ret = make([]string, 0, strings.Count(str, sep)+1)
index := strings.Index(str, sep)
for index >= 0 {
ret = append(ret, str[:index])
str = str[index+1:]
str = str[index+len(sep):]
index = strings.Index(str, sep)
}
ret = append(ret, str)

View File

@ -22,3 +22,19 @@ func TestSplit2(t *testing.T) {
t.Errorf("want:%v, but got:%v\n", want, got)
}
}
func TestSplit3(t *testing.T) {
got := Split("abcef", "bc")
want := []string{"a", "ef"}
if !reflect.DeepEqual(want, got) {
t.Fatalf("want:%v, but got:%v\n", want, got)
}
}
//基准测试
func BenchmarkSplit(b *testing.B) {
for i := 0; i < b.N; i++ {
Split("a:b:c:d:e:f", ":")
}
}