libf 2 rokov pred
commit
5eb6e6c32b
11 zmenil súbory, kde vykonal 419 pridanie a 0 odobranie
  1. 49 0
      atomic/main.go
  2. 40 0
      compare/compare_test.go
  3. BIN
      etcd/etcd
  4. 49 0
      etcd/etcd.go
  5. 28 0
      file/file_test.go
  6. 1 0
      file/test.txt
  7. 71 0
      floyd/floyd.go
  8. 8 0
      go.mod
  9. 4 0
      go.sum
  10. 85 0
      hashmap/hashmap_test.go
  11. 84 0
      lock/lock_test.go

+ 49 - 0
atomic/main.go

@@ -0,0 +1,49 @@
+package main
+
+import (
+	"fmt"
+	"sync/atomic"
+	"time"
+)
+
+func main() {
+	data := []int32{}
+	tcount := int32(1000000000)
+	counta := int32(0)
+	countb := int32(0)
+	countc := int32(0)
+	countd := int32(0)
+	go func() {
+		for i := int32(0); i < tcount; i++ {
+			go func() {
+				go func() {
+					//	time.Sleep(10 * time.Microsecond)
+					data = append(data, atomic.AddInt32(&counta, 1))
+				}()
+				go func() {
+					//	time.Sleep(10 * time.Microsecond)
+					data = append(data, atomic.AddInt32(&countb, 1))
+				}()
+				go func() {
+					//	time.Sleep(10 * time.Microsecond)
+					data = append(data, atomic.AddInt32(&countc, 1))
+				}()
+				go func() {
+					//	time.Sleep(10 * time.Microsecond)
+					data = append(data, atomic.AddInt32(&countd, 1))
+				}()
+			}()
+			time.Sleep(1 * time.Microsecond)
+		}
+	}()
+	to := time.Now()
+	for counta < tcount && countb < tcount && countc < tcount && countd < tcount {
+		if time.Since(to) > 1000*time.Millisecond {
+			println("......")
+			go fmt.Println(time.Now().String()[:23], "      ", counta, "  ", countb, "  ", countc, "  ", countd)
+			to = time.Now()
+		}
+		time.Sleep(1 * time.Microsecond)
+	}
+	fmt.Println(time.Now().String()[:23], "      ", counta, "  ", countb, "  ", countc, "  ", countd)
+}

+ 40 - 0
compare/compare_test.go

@@ -0,0 +1,40 @@
+package compare_test
+
+import (
+	"sort"
+	"testing"
+	"time"
+)
+
+func BenchmarkTimeCompare(t *testing.B) {
+	for i := 0; i < 1000000; i++ {
+		b := time.Now().After(time.Now())
+		if b {
+			print("!")
+		}
+	}
+}
+
+func BenchmarkTimeNSCompare(t *testing.B) {
+	for i := 0; i < 1000000; i++ {
+		b := time.Now().UnixNano() > time.Now().UnixNano()
+		if b {
+			print("!")
+		}
+	}
+}
+
+func TestSortSearch(t *testing.T) {
+	ta := []int{}
+	for i := 0; i < 100; i++ {
+		ta = append(ta, i*2)
+	}
+	from := sort.Search(len(ta)-1, func(i int) bool {
+		return ta[i] >= -23
+	})
+	to := sort.Search(len(ta)-1, func(i int) bool {
+		return ta[i] >= 200
+	})
+	println("[23,36]=", "index[", from, ",", to, "]")
+	println(ta[from], ",", ta[to])
+}

BIN
etcd/etcd


+ 49 - 0
etcd/etcd.go

@@ -0,0 +1,49 @@
+package main
+
+import (
+	"context"
+	"fmt"
+	"time"
+
+	"git.wecise.com/wecise/common/etcd"
+)
+
+func main() {
+	etcdPath := "47.92.151.165:2379"
+	etcdUser := ""
+	etcdPass := ""
+	etcdFile := "/matrix/etc/omdb"
+	etcdURL := "etcd://" + etcdUser + "@" + etcdPath + "" + etcdFile
+	fmt.Println(etcdURL)
+	cli, err := etcd.NewClient(etcdPath, etcdUser, etcdPass)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	val, err := cli.Get(etcdFile)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	fmt.Println(fmt.Sprintf("load config from %s", etcdURL))
+	cache_value := []string{""}
+	on_change := func(value string) {
+		if cache_value[0] != value {
+			fmt.Println("config changed:\r\n", value)
+		}
+	}
+	on_change(val)
+	ch := cli.Watch(context.Background(), etcdFile, false)
+	go func() {
+		fmt.Println(fmt.Sprintf("watching %s", etcdURL))
+		for {
+			select {
+			case evt := <-ch:
+				on_change(evt.Node.Value)
+			}
+		}
+	}()
+	for {
+		time.Sleep(1 * time.Minute)
+	}
+}

+ 28 - 0
file/file_test.go

@@ -0,0 +1,28 @@
+package file_test
+
+import (
+	"fmt"
+	"os"
+	"strings"
+	"testing"
+	"time"
+)
+
+func TestBuffer(m *testing.T) {
+	fn := "test.txt"
+	f, e := os.OpenFile(fn, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
+	if e != nil {
+		fmt.Println(e)
+		return
+	}
+	f.Truncate(0)
+	for i := 0; i < 1000; i++ {
+		f.Write([]byte(strings.Repeat("+", 1)))
+		// cmd := exec.Command("/bin/bash", "-c", "ls -l "+fn)
+		// cmd.Stderr = os.Stderr
+		// cmd.Stdout = os.Stdout
+		// cmd.Stdin = os.Stdin
+		// cmd.Run()
+		time.Sleep(1 * time.Second)
+	}
+}

+ 1 - 0
file/test.txt

@@ -0,0 +1 @@
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

+ 71 - 0
floyd/floyd.go

@@ -0,0 +1,71 @@
+package main
+
+import (
+	"fmt"
+	"math"
+)
+
+type graph struct {
+	to int
+	wt float64
+}
+
+func floydWarshall(g [][]graph) ([][]float64, [][][]int) {
+	path := make([][][]int, len(g))
+	dist := make([][]float64, len(g))
+	for i := range dist {
+		pi := make([][]int, len(g))
+		di := make([]float64, len(g))
+		for j := range di {
+			di[j] = math.Inf(1)
+			pi[j] = []int{}
+		}
+		di[i] = 0
+		dist[i] = di
+		path[i] = pi
+	}
+	for u, graphs := range g {
+		for _, v := range graphs {
+			dist[u][v.to] = v.wt
+			path[u][v.to] = []int{u, v.to}
+		}
+	}
+	for k, dk := range dist {
+		pk := path[k]
+		for i, di := range dist {
+			pi := path[i]
+			for j, dij := range di {
+				if d := di[k] + dk[j]; dij > d {
+					di[j] = d
+					pi[j] = append(pi[k], pk[j][1:]...)
+				}
+			}
+		}
+	}
+	return dist, path
+}
+
+func main() {
+	gra := [][]graph{
+		1: {{2, 3}, {3, 8}, {5, -4}},
+		2: {{4, 1}, {5, 7}},
+		3: {{2, 4}},
+		4: {{1, 2}, {3, -5}},
+		5: {{4, 6}},
+	}
+
+	dist, path := floydWarshall(gra)
+	//dist[][] will be the output matrix that will finally
+	//have the shortest distances between every pair of vertices
+	for i, di := range dist {
+		pi := path[i]
+		s := ""
+		for j, pij := range pi {
+			if j > 0 {
+				s += " "
+			}
+			s += fmt.Sprintf("%4g %-12s", di[j], fmt.Sprint(pij))
+		}
+		fmt.Printf("[%s]\n", s)
+	}
+}

+ 8 - 0
go.mod

@@ -0,0 +1,8 @@
+module test
+
+go 1.19
+
+require (
+	github.com/alphadose/haxmap v1.0.0 // indirect
+	github.com/cornelk/hashmap v1.0.8 // indirect
+)

+ 4 - 0
go.sum

@@ -0,0 +1,4 @@
+github.com/alphadose/haxmap v1.0.0 h1:fap5Y4ksllFZ5ZG7ffWG4QJ8lh/oLaqfXWVp7Aj4TOo=
+github.com/alphadose/haxmap v1.0.0/go.mod h1:Fu37Wlmj7cR++vSLgRTu3fGy8wpjHGmMypM2aclkc1A=
+github.com/cornelk/hashmap v1.0.8 h1:nv0AWgw02n+iDcawr5It4CjQIAcdMMKRrs10HOJYlrc=
+github.com/cornelk/hashmap v1.0.8/go.mod h1:RfZb7JO3RviW/rT6emczVuC/oxpdz4UsSB2LJSclR1k=

+ 85 - 0
hashmap/hashmap_test.go

@@ -0,0 +1,85 @@
+package hashmap_test
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/alphadose/haxmap"
+	"github.com/cornelk/hashmap"
+)
+
+func BenchmarkGoMap(t *testing.B) {
+	// 1000000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: test/hashmap
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkGoMap
+	// BenchmarkGoMap-8   	1000000000	         0.5068 ns/op	       0 B/op	       0 allocs/op
+	// PASS
+	// ok  	test/hashmap	14.880s
+	// 100000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: test/hashmap
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkGoMap
+	// BenchmarkGoMap-8   	1000000000	         0.02783 ns/op	       0 B/op	       0 allocs/op
+	// PASS
+	// ok  	test/hashmap	0.721s
+	m := map[string]int{}
+	t.StartTimer()
+	for i := 0; i < 100000; i++ {
+		s := fmt.Sprint(i)
+		m[s] = i
+	}
+	t.StopTimer()
+}
+
+func BenchmarkHaxMap(t *testing.B) {
+	// 1000000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: test/hashmap
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkHaxMap
+	// BenchmarkHaxMap-8   	1000000000	         0.8453 ns/op	       0 B/op	       0 allocs/op
+	// PASS
+	// ok  	test/hashmap	65.175s
+	// 100000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: test/hashmap
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkHaxMap
+	// BenchmarkHaxMap-8   	1000000000	         0.05282 ns/op	       0 B/op	       0 allocs/op
+	// PASS
+	// ok  	test/hashmap	1.154s
+	m := haxmap.New[string, int]()
+	t.StartTimer()
+	for i := 0; i < 100000; i++ {
+		s := fmt.Sprint(i)
+		m.Set(s, i)
+	}
+	t.StopTimer()
+}
+
+func BenchmarkHashMap(t *testing.B) {
+	// 1000000 loop Crash
+	// 100000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: test/hashmap
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkHashMap
+	// BenchmarkHashMap-8   	       1	33365603372 ns/op	11402896 B/op	  399822 allocs/op
+	// PASS
+	// ok  	test/hashmap	33.493s
+	m := hashmap.New[string, int]()
+	t.StartTimer()
+	for i := 0; i < 100000; i++ {
+		s := fmt.Sprint(i)
+		m.Set(s, i)
+	}
+	t.StopTimer()
+}

+ 84 - 0
lock/lock_test.go

@@ -0,0 +1,84 @@
+package lock_test
+
+import (
+	"sync"
+	"testing"
+)
+
+func BenchmarkSyncMutex(t *testing.B) {
+	// 100000000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: git.wecise.com/wecise/odbserver/mring/z/lock
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkSyncMutex
+	// BenchmarkSyncMutex-8   	       1	33905064348 ns/op	2407287568 B/op	100034140 allocs/op
+	// PASS
+	// ok  	git.wecise.com/wecise/odbserver/mring/z/lock	34.056s
+	//
+	// 10000000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: git.wecise.com/wecise/odbserver/mring/z/lock
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkSyncMutex
+	// BenchmarkSyncMutex-8   	       1	3215950432 ns/op	243519072 B/op	10016404 allocs/op
+	// PASS
+	// ok  	git.wecise.com/wecise/odbserver/mring/z/lock	3.980s
+	//
+	// 1000000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: git.wecise.com/wecise/odbserver/mring/z/lock
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkSyncMutex
+	// BenchmarkSyncMutex-8   	1000000000	         0.3031 ns/op	       0 B/op	       0 allocs/op
+	// PASS
+	// ok  	git.wecise.com/wecise/odbserver/mring/z/lock	5.979s
+
+	var mutex sync.Mutex
+	t.StartTimer()
+	for i := 0; i < 10000000; {
+		go func() {
+			mutex.Lock()
+			i++
+			mutex.Unlock()
+		}()
+	}
+	t.StopTimer()
+}
+
+func BenchmarkChan(t *testing.B) {
+	// 100000000 loop Crash
+	//
+	// 10000000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: git.wecise.com/wecise/odbserver/mring/z/lock
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkChan
+	// BenchmarkChan-8   	       1	49887892128 ns/op	5246415680 B/op	35689879 allocs/op
+	// PASS
+	// ok  	git.wecise.com/wecise/odbserver/mring/z/lock	52.270s
+	//
+	// 1000000 loop
+	// goos: darwin
+	// goarch: amd64
+	// pkg: git.wecise.com/wecise/odbserver/mring/z/lock
+	// cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
+	// BenchmarkChan
+	// BenchmarkChan-8   	       1	1749597455 ns/op	313448576 B/op	 2504514 allocs/op
+	// PASS
+	// ok  	git.wecise.com/wecise/odbserver/mring/z/lock	2.472s
+
+	ch := make(chan struct{}, 1)
+	t.StartTimer()
+	for i := 0; i < 10000000; {
+		go func() {
+			ch <- struct{}{}
+			i++
+			<-ch
+		}()
+	}
+	t.StopTimer()
+}