Exemplo n.º 1
0
Arquivo: brute.go Projeto: mc0/okq
func main() {
	flag.Parse()
	agg.CreateInterrupt(1)
	n := runtime.NumCPU()
	runtime.GOMAXPROCS(n)
	okq.Debug = true

	queueCh := make(chan string)
	qs := make([]string, 10)
	for i := range qs {
		qs[i] = randString()
	}
	go func() {
		for {
			for i := range qs {
				queueCh <- qs[i]
			}
		}
	}()

	fn := func(e *okq.Event) bool {
		return true
	}

	pushFlag := okq.Normal
	if *noBlock {
		log.Println("using NOBLOCK")
		pushFlag = okq.NoBlock
	}
	for i := 0; i < n; i++ {
		go func() {
			cl := okq.New(*addr)
			for {
				err := cl.Push(<-queueCh, randString(), pushFlag)
				if err != nil {
					log.Fatal(err)
				}
			}
		}()
	}

	for i := 0; i < n; i++ {
		go func() {
			cl := okq.New(*addr)
			for {
				err := cl.Consumer(fn, nil, qs...)
				if err != nil {
					log.Printf("got error consuming: %s", err)
				}
			}
		}()
	}

	select {}
}
Exemplo n.º 2
0
Arquivo: constant.go Projeto: mc0/okq
func main() {
	flag.Parse()
	agg.CreateInterrupt(1)
	n := runtime.NumCPU()
	runtime.GOMAXPROCS(n)
	okq.Debug = true

	queueCh := make(chan string)
	qs := make([]string, 10)
	for i := range qs {
		qs[i] = randString()
	}
	go func() {
		for {
			for i := range qs {
				queueCh <- qs[i]
			}
		}
	}()

	triggerJobCh := make(chan bool, n*10)
	for i := 0; i < n*10; i++ {
		triggerJobCh <- true
	}

	pushFlag := okq.Normal
	if *noBlock {
		log.Println("using NOBLOCK")
		pushFlag = okq.NoBlock
	}
	for i := 0; i < n; i++ {
		go func() {
			cl := okq.New(*addr)
			for range triggerJobCh {
				eventB, err := time.Now().MarshalBinary()
				if err != nil {
					log.Fatal(err)
				}
				err = cl.Push(<-queueCh, string(eventB), pushFlag)
				if err != nil {
					log.Fatal(err)
				}
			}
		}()
	}

	fn := func(e *okq.Event) bool {
		eventB := []byte(e.Contents)
		var then time.Time
		if err := then.UnmarshalBinary(eventB); err != nil {
			log.Fatal(err)
		}
		agg.Agg("event", time.Since(then).Seconds())
		triggerJobCh <- true
		return true
	}

	var chwg sync.WaitGroup
	for i := 0; i < n; i++ {
		chwg.Add(1)
		go func() {
			cl := okq.New(*addr)
			err := cl.Consumer(fn, stopCh, qs...)
			if err != nil {
				log.Fatalf("got error consuming: %s", err)
			}
		}()
	}
	chwg.Wait()
}