Last active
September 15, 2015 02:41
-
-
Save emmaly/e5b0b985d4b9d6d675e4 to your computer and use it in GitHub Desktop.
Timer Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"sync" | |
"time" | |
"github.com/satori/go.uuid" | |
) | |
var wg *sync.WaitGroup | |
// AA is a something | |
type AA struct { | |
Message string | |
Value int | |
} | |
// BB is something | |
type BB struct { | |
Count int | |
Name string | |
} | |
func main() { | |
wg = &sync.WaitGroup{} | |
c := listen() | |
rand.Seed(0) | |
var i = 0 | |
for i = 0; i < 10000000; i++ { | |
var zot interface{} | |
switch rand.Intn(2) { | |
case 0: | |
zot = AA{ | |
Message: uuid.NewV4().String(), | |
Value: i, | |
} | |
case 1: | |
zot = BB{ | |
Count: i, | |
Name: uuid.NewV4().String(), | |
} | |
} | |
timer(zot, c, time.Minute*time.Duration(5)) | |
if i%10000 == 0 { | |
fmt.Printf("Loaded %d in the hopper.\n", i) | |
} | |
} | |
fmt.Printf("Loaded %d in the hopper. Done.\n", i) | |
wg.Wait() | |
} | |
func listen() chan<- interface{} { | |
c := make(chan interface{}) | |
go func() { | |
i := 0 | |
for { | |
zot := <-c | |
if i%10000 == 0 { | |
fmt.Printf("ZOT[%d]: %+v\n", i, zot) | |
} | |
i++ | |
} | |
}() | |
return c | |
} | |
func timer(something interface{}, c chan<- interface{}, x time.Duration) { | |
go func() { | |
wg.Add(1) | |
defer wg.Done() | |
time.Sleep(x) | |
c <- something | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment