initial import
This commit is contained in:
60
example/timingwheel/leak/leak.go
Normal file
60
example/timingwheel/leak/leak.go
Normal 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
|
||||
}
|
||||
78
example/timingwheel/main.go
Normal file
78
example/timingwheel/main.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user