davyxu / gocoroutine Goto Github PK
View Code? Open in Web Editor NEW为游戏服务器设计的coroutine实现
License: MIT License
为游戏服务器设计的coroutine实现
License: MIT License
benchmark_test.go
package gocoroutine
import (
// "fmt"
"sync"
"testing"
)
var k int = 0
var m int = 0
func msgProc_benchmark(fc FlowControl) {
//fmt.Println("msg 1")
fc.Yield(dbProc_benchmark)
}
func dbProc_benchmark(fc FlowControl) {
//fmt.Println("db 1")
// msgid := fc.Params().(int)
k++
}
func recvProc_benchmark(sch *Scheduler) {
for i := 0; i < 100000; i++ {
sch.AddTask(msgProc_benchmark, i)
}
}
func TestBenchmark(t *testing.T) {
sch := NewScheduler()
sch.Start()
recvProc_benchmark(sch)
sch.Exit()
println("final k:\n", k)
}
func traditionalLogic(wg *sync.WaitGroup) {
wg.Add(1)
m++
go func() {
wg.Done()
}()
}
// go test -bench=Benchmark -cpuprofile=cprof
func TestTraditional(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100000; i++ {
traditionalLogic(&wg)
}
wg.Wait()
println("final m:\n", m)
}
Scheduler 并不是单线程执行存在并发问题,执行结果为 99109 与 10000不符合
=== RUN TestBenchmark
final k:
99096
--- PASS: TestBenchmark (0.17s)
=== RUN TestTraditional
final m:
100000
--- PASS: TestTraditional (0.02s)
PASS
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.