Beispiel #1
0
func BenchmarkSemAcq(b *testing.B) {
	s := semaphore.New(1)
	for i := 0; i < b.N; i++ {
		s.Acquire()
		s.Release()
	}
}
Beispiel #2
0
func BenchmarkSemLoop5(b *testing.B) {
	for i := 0; i < b.N; i++ {
		s := semaphore.New(5)
		go s.Stop()
		for s.Acquire() {
		}
	}
}
Beispiel #3
0
func BenchmarkSemAcqStop(b *testing.B) {
	for i := 0; i < b.N; i++ {
		s := semaphore.New(1)
		s.Acquire()
		go s.Stop()
		s.Acquire()
	}
}
Beispiel #4
0
func BenchmarkSemSleep(b *testing.B) {
	s := semaphore.New(1)
	s.Acquire()
	for i := 0; i < b.N; i++ {
		go func() {
			time.Sleep(5 * time.Nanosecond)
			s.Release()
		}()
		s.Acquire()
	}
}
Beispiel #5
0
func BenchmarkSemWait(b *testing.B) {
	for i := 0; i < b.N; i++ {
		s := semaphore.New(10)
		for j := 0; j < 100; j++ {
			go func() {
				for s.Acquire() {
					time.Sleep(1 * time.Nanosecond)
					s.Release()
				}
			}()
		}
		time.Sleep(100 * time.Nanosecond)
		s.Stop()
		s.Wait()
	}
}
Beispiel #6
0
// running at any one time.
package main

import (
	"fmt"
	"sync/atomic"
	"time"

	"github.com/carlmjohnson/go-utils/semaphore"
)

// N is the number of active semaphores allowed.
const N = 3

var (
	s = semaphore.New(N)
	w = new(int32)
)

func worker(n int) {
	for s.Acquire() {
		fmt.Printf("%s\t%d\t%*s\n",
			time.Now().Format("15:04:05"), atomic.AddInt32(w, 1), n, "^")
		time.Sleep(1 * time.Second)
		fmt.Printf("%s\t%d\t%*s\n",
			time.Now().Format("15:04:05"), atomic.AddInt32(w, -1), n, "_")
		s.Release()
	}
	fmt.Printf("%s\t%d\t%*s\n", time.Now().Format("15:04:05"), atomic.LoadInt32(w), n, "*")
}