libf 10 months ago
parent
commit
bd7d04c18c
9 changed files with 333 additions and 0 deletions
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 25 0
      gorun/gorun_test.go
  4. 19 0
      loongtest/cpunumber/cpunumber.go
  5. 19 0
      map/map_test.go
  6. 124 0
      msgpack/msgpack_test.go
  7. 45 0
      rand/rand_test.go
  8. 34 0
      regexp/regexp_test.go
  9. 64 0
      timer/timer_test.go

+ 1 - 0
go.mod

@@ -74,6 +74,7 @@ require (
 	github.com/google/flatbuffers v2.0.0+incompatible // indirect
 	github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 // indirect
 	github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
+	github.com/issue9/assert v1.5.0 // indirect
 	github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 // indirect
 	github.com/jfcg/sorty v1.0.2 // indirect
 	github.com/jinzhu/now v1.1.1 // indirect

+ 2 - 0
go.sum

@@ -211,6 +211,8 @@ github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc
 github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
+github.com/issue9/assert v1.5.0 h1:9N71cQh6lt9/3y/WnB2lP0AuCxnTwunTToajoWnVD8M=
+github.com/issue9/assert v1.5.0/go.mod h1:Yktk83hAVl1SPSYtd9kjhBizuiBIqUQyj+D5SE2yjVY=
 github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
 github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 h1:g0fAGBisHaEQ0TRq1iBvemFRf+8AEWEmBESSiWB3Vsc=
 github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=

+ 25 - 0
gorun/gorun_test.go

@@ -0,0 +1,25 @@
+package gorun_test
+
+import "testing"
+
+func BenchmarkRun(b *testing.B) {
+	/*
+	   goos: darwin
+	   goarch: amd64
+	   pkg: trial/gorun
+	   cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	   BenchmarkRun-8   	1000000000	         0.0000002 ns/op ~ 0.0000003 ns/op
+	*/
+	func() {}()
+}
+
+func BenchmarkGoRun(b *testing.B) {
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/gorun
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkGoRun-8   	1000000000	         0.0000012 ns/op ~ 0.0000030 ns/op ~ 0.0000092 ns/op
+	*/
+	go func() {}()
+}

+ 19 - 0
loongtest/cpunumber/cpunumber.go

@@ -0,0 +1,19 @@
+package main
+
+import (
+	"runtime"
+	"time"
+)
+
+func main() {
+	println(runtime.NumCPU())
+	for i := 0; i < 1000000000; i++ {
+		go func() {
+			m := map[int]int{}
+			for i := 0; i < 1000000000; i++ {
+				m[i] = i
+				time.Sleep(1)
+			}
+		}()
+	}
+}

+ 19 - 0
map/map_test.go

@@ -2,9 +2,15 @@ package map_test
 
 import (
 	"encoding/json"
+	"fmt"
 	"testing"
 )
 
+func TestKey(t *testing.T) {
+	// type ss []string
+	// mm := map[*ss]string{}
+}
+
 func TestNoOrderMap(t *testing.T) {
 	mm := map[string]string{}
 
@@ -24,3 +30,16 @@ func TestNoOrderMap(t *testing.T) {
 	bs, _ := json.Marshal(mm)
 	println(string(bs))
 }
+
+func TestConcurDeleteNotExistKey(t *testing.T) {
+	mm := map[string]string{}
+	for i := 0; i < 10000; i++ {
+		mm[fmt.Sprint(i)] = fmt.Sprint(i)
+	}
+	println("删除不存在的key")
+	for i := 0; i < 10000; i++ {
+		go func() {
+			delete(mm, "")
+		}()
+	}
+}

+ 124 - 0
msgpack/msgpack_test.go

@@ -0,0 +1,124 @@
+package msgpack_test
+
+import (
+	"bytes"
+	"encoding/gob"
+	"fmt"
+	"reflect"
+	"testing"
+
+	"github.com/vmihailenco/msgpack/v5"
+)
+
+func Encode(v interface{}) ([]byte, error) {
+	enc := msgpack.GetEncoder()
+
+	var buf bytes.Buffer
+	enc.Reset(&buf)
+
+	err := enc.Encode(v)
+	b := buf.Bytes()
+
+	msgpack.PutEncoder(enc)
+
+	if err != nil {
+		return nil, err
+	}
+	return b, err
+}
+
+func Decode(data []byte, v interface{}) error {
+	dec := msgpack.GetDecoder()
+
+	dec.Reset(bytes.NewReader(data))
+	err := dec.Decode(v)
+
+	msgpack.PutDecoder(dec)
+
+	return err
+}
+
+func TestA(t *testing.T) {
+	v := int(0)
+	in := map[string]interface{}{"v": v}
+
+	bs, e := Encode(in)
+	if e != nil {
+		fmt.Println(e)
+	}
+
+	out := map[string]interface{}{}
+
+	e = Decode(bs, &out)
+	if e != nil {
+		fmt.Println(e)
+	}
+
+	fmt.Println(fmt.Sprintf("input: %v %v", reflect.TypeOf(in["v"]), in["v"]))
+	fmt.Println(fmt.Sprintf("output: %v %v", reflect.TypeOf(out["v"]), out["v"]))
+
+	fmt.Println()
+}
+
+func TestB(t *testing.T) {
+	in := map[string]interface{}{
+		"int":   int(0),
+		"int8":  int8(0),
+		"int16": int16(0),
+		"int32": int32(0),
+		"int64": int64(0),
+		// "struct": struct{ A string }{A: "a"}, // gob: type not registered for interface: struct { A string }
+	}
+
+	var buf bytes.Buffer
+	e := gob.NewEncoder(&buf).Encode(in)
+	if e != nil {
+		fmt.Println(e)
+	}
+
+	out := map[string]interface{}{}
+
+	e = gob.NewDecoder(&buf).Decode(&out)
+	if e != nil {
+		fmt.Println(e)
+	}
+
+	for k, v := range in {
+		fmt.Println(fmt.Sprintf("input: %s = %v(%v)", k, reflect.TypeOf(v), v))
+	}
+	for k, v := range out {
+		fmt.Println(fmt.Sprintf("output: %s = %v(%v)", k, reflect.TypeOf(v), v))
+	}
+	fmt.Println()
+}
+func TestC(t *testing.T) {
+	in := map[string]interface{}{
+		"int":    int(0),
+		"int8":   int8(0),
+		"int16":  int16(0),
+		"int32":  int32(0),
+		"int64":  int64(0),
+		"struct": struct{ A string }{A: "a"},
+	}
+
+	var buf bytes.Buffer
+	e := msgpack.NewEncoder(&buf).Encode(in)
+	if e != nil {
+		fmt.Println(e)
+	}
+
+	out := map[string]interface{}{}
+
+	e = msgpack.NewDecoder(&buf).Decode(&out)
+	if e != nil {
+		fmt.Println(e)
+	}
+
+	for k, v := range in {
+		fmt.Println(fmt.Sprintf("input: %s = %v(%v)", k, reflect.TypeOf(v), v))
+	}
+	for k, v := range out {
+		fmt.Println(fmt.Sprintf("output: %s = %v(%v)", k, reflect.TypeOf(v), v))
+	}
+	fmt.Println()
+}

+ 45 - 0
rand/rand_test.go

@@ -0,0 +1,45 @@
+package rand_test
+
+import (
+	"math"
+	"math/rand"
+	"testing"
+)
+
+func BenchmarkRand(b *testing.B) {
+	for i := 0; i < 100000000; i++ {
+		rand.Int63n(math.MaxInt64)
+	}
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/rand
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkRand-8   	       1	4164423941 ns/op
+	*/
+}
+
+func BenchmarkNop(b *testing.B) {
+	for i := 0; i < 1000000000; i++ {
+	}
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/rand
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkNop-8   	       1	2048992111 ns/op
+	*/
+}
+
+func BenchmarkDiv(b *testing.B) {
+	for i := 0; i < 1000000000; i++ {
+		func() {}()
+	}
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/rand
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkDiv-8   	       1	2693673826 ns/op
+	*/
+}

+ 34 - 0
regexp/regexp_test.go

@@ -0,0 +1,34 @@
+package regexp_test
+
+import (
+	"fmt"
+	"regexp"
+	"testing"
+
+	"github.com/issue9/assert"
+)
+
+func TestAll(t *testing.T) {
+	fmt.Println("\ufffc")
+	regxTest(t, ``)
+	regxTest(t, `中文`)
+	regxTest(t, "\\\\")
+	regxTest(t, `\\u00ff`)
+	regxTest(t, "\uffff")
+	regxTest(t, "\u0100")
+	for i := 0; i < 65536; i++ {
+		// fmt.Println(fmt.Sprintf("%c", i))
+		regxTest(t, fmt.Sprintf("%c", i))
+	}
+	fmt.Println()
+}
+
+func regxTest(t *testing.T, s string) {
+	re, e := regexp.Compile(s)
+	if re != nil {
+		assert.True(t, re.MatchString(s))
+	} else {
+		fmt.Print(s, " ")
+	}
+	assert.Nil(t, e)
+}

+ 64 - 0
timer/timer_test.go

@@ -19,3 +19,67 @@ func TestTimer(t *testing.T) {
 		}
 	}
 }
+
+func BenchmarkReset(b *testing.B) {
+	tc := time.NewTimer(1 * time.Nanosecond)
+	for i := 0; i < 1000000; i++ {
+		select {
+		case <-tc.C:
+			tc.Reset(1 * time.Nanosecond)
+		}
+	}
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/timer
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkReset-8   	       1	1082548694 ns/op
+	*/
+}
+func BenchmarkNewTimer(b *testing.B) {
+	tc := time.NewTimer(1 * time.Nanosecond)
+	for i := 0; i < 1000000; i++ {
+		select {
+		case <-tc.C:
+			tc = time.NewTimer(1 * time.Nanosecond)
+		}
+	}
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/timer
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkNewTimer-8   	       1	1570331115 ns/op
+	*/
+}
+
+func BenchmarkStop(b *testing.B) {
+	tc := time.NewTimer(1 * time.Nanosecond)
+	for i := 0; i < 1000000; i++ {
+		tc.Stop()
+		tc.Reset(1 * time.Nanosecond)
+	}
+	tc.Stop()
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/timer
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkStop-8   	1000000000	         0.1683 ns/op
+	*/
+}
+
+func BenchmarkReset1(b *testing.B) {
+	tc := time.NewTimer(1 * time.Nanosecond)
+	for i := 0; i < 1000000; i++ {
+		tc = time.NewTimer(1 * time.Nanosecond)
+	}
+	/*
+		goos: darwin
+		goarch: amd64
+		pkg: trial/timer
+		cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+		BenchmarkReset1-8   	1000000000	         0.5614 ns/op
+	*/
+	tc.Stop()
+}