initial import

This commit is contained in:
kevin
2020-07-26 17:09:05 +08:00
commit 7e3a369a8f
647 changed files with 54754 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
package main
import (
"flag"
"fmt"
"log"
"runtime"
"strconv"
"time"
"zero/core/cmdline"
"zero/core/collection"
"zero/core/proc"
)
const numItems = 1000000
var round = flag.Int("r", 3, "rounds to go")
func main() {
defer proc.StartProfile().Stop()
flag.Parse()
fmt.Println(getMemUsage())
for i := 0; i < *round; i++ {
do()
}
cmdline.EnterToContinue()
}
func do() {
tw, err := collection.NewTimingWheel(time.Second, 100, execute)
if err != nil {
log.Fatal(err)
}
for i := 0; i < numItems; i++ {
key := strconv.Itoa(i)
tw.SetTimer(key, key, time.Second*5)
}
fmt.Println(getMemUsage())
}
func execute(k, v interface{}) {
}
func getMemUsage() string {
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For more info, see: https://golang.org/pkg/runtime/#MemStats
return fmt.Sprintf("Heap Alloc = %dMiB", toMiB(m.HeapAlloc))
}
func toMiB(b uint64) uint64 {
return b / 1024 / 1024
}

View File

@@ -0,0 +1,78 @@
package main
import (
"flag"
"fmt"
"log"
"runtime"
"sync/atomic"
"time"
"zero/core/collection"
)
const interval = time.Minute
var traditional = flag.Bool("traditional", false, "enable traditional mode")
func main() {
flag.Parse()
go func() {
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fmt.Printf("goroutines: %d\n", runtime.NumGoroutine())
}
}
}()
if *traditional {
traditionalMode()
} else {
timingWheelMode()
}
}
func timingWheelMode() {
var count uint64
tw, err := collection.NewTimingWheel(time.Second, 600, func(key, value interface{}) {
job(&count)
})
if err != nil {
log.Fatal(err)
}
defer tw.Stop()
for i := 0; ; i++ {
tw.SetTimer(i, i, interval)
time.Sleep(time.Millisecond)
}
}
func traditionalMode() {
var count uint64
for {
go func() {
timer := time.NewTimer(interval)
defer timer.Stop()
select {
case <-timer.C:
job(&count)
}
}()
time.Sleep(time.Millisecond)
}
}
func job(count *uint64) {
v := atomic.AddUint64(count, 1)
if v%1000 == 0 {
fmt.Println(v)
}
}