Skip to content

Instantly share code, notes, and snippets.

@disksing
Created March 3, 2017 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disksing/34dedbe613b9b4135903df72abaf29e8 to your computer and use it in GitHub Desktop.
Save disksing/34dedbe613b9b4135903df72abaf29e8 to your computer and use it in GitHub Desktop.
pingcap/pd tso atomic test.
package main
import (
"fmt"
"sync/atomic"
"time"
)
type atomicObject struct {
physical time.Time
logical int64
}
func main() {
var ts atomic.Value
go func() {
for {
time.Sleep(time.Second)
ts.Store(&atomicObject{
physical: time.Now(),
})
}
}()
ch := make(chan time.Duration, 10000)
for i := 0; i < 20; i++ {
go func() {
for {
now := time.Now()
current, ok := ts.Load().(*atomicObject)
if ok {
atomic.AddInt64(&current.logical, 100)
}
ch <- time.Since(now)
time.Sleep(time.Microsecond * 10)
}
}()
}
for d := range ch {
if d > time.Millisecond {
fmt.Println(time.Now(), d)
}
}
}
// output:
// $ go run ts.go
// 2017-03-03 10:51:24.415276513 +0800 CST 3.592485ms
// 2017-03-03 10:52:18.112584328 +0800 CST 1.232196ms
// 2017-03-03 10:52:32.096200522 +0800 CST 1.045356ms
// 2017-03-03 10:53:10.423632016 +0800 CST 1.48158ms
// 2017-03-03 10:53:10.423685575 +0800 CST 1.484165ms
// 2017-03-03 10:54:54.41106371 +0800 CST 2.301527ms
// 2017-03-03 10:57:10.429594065 +0800 CST 1.487475ms
// 2017-03-03 10:57:47.281149376 +0800 CST 1.509912ms
// 2017-03-03 10:58:10.637953754 +0800 CST 1.127897ms
// 2017-03-03 10:59:44.257287868 +0800 CST 1.574665ms
// 2017-03-03 11:00:07.836994018 +0800 CST 13.036667ms
// 2017-03-03 11:00:29.088400088 +0800 CST 1.136973ms
// 2017-03-03 11:01:05.431764315 +0800 CST 15.060552ms
// 2017-03-03 11:02:22.490705038 +0800 CST 3.664172ms
// 2017-03-03 11:03:57.988705562 +0800 CST 1.133632ms
// 2017-03-03 11:05:41.043021676 +0800 CST 1.140521ms
// 2017-03-03 11:06:18.327720308 +0800 CST 1.512206ms
// ^Csignal: interrupt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment