Example #1
0
// test unsafe set
func TestAsyncStrDictŠ”ompareSetSpeed(t *testing.T) {
	dictPtr := helpers.NewAsyncStrDict()
	var n, wCount, doneCount int
	n = 1000
	wCount = 50
	doneChannel := make(chan bool, wCount)
	testWorker := func(index int, dict *helpers.AsyncStrDict, done *chan bool) {
		for i := 0; i < n; i++ {
			dict.SetUnsafe(
				fmt.Sprintf("%d-%d", index+1, i+1),
				fmt.Sprintf("%d.%d", index, i))
		}
		*done <- true
	}
	testWorkerAnalog := func(index int, dict *simpleAnalogDict, done *chan bool) {
		for i := 0; i < n; i++ {
			dict.Set(
				fmt.Sprintf("%d-%d", index+1, i+1),
				fmt.Sprintf("%d.%d", index, i))
		}
		*done <- true
	}
	// run workers
	var startTime time.Time
	startTime = time.Now()
	for i := 0; i < wCount; i++ {
		go testWorker(i, dictPtr, &doneChannel)
	}
	for doneCount < wCount {
		done := <-doneChannel
		if done {
			doneCount += 1
		}
	}
	rtime := float32(time.Since(startTime).Seconds())
	s := dictPtr.Size()
	if n*wCount != s {
		t.Errorf("Dict size: %d", s)
	}
	doneCount = 0
	dictAnalog := simpleAnalogDict{
		content:         make(map[string]string),
		AsyncSafeObject: *(helpers.NewAsyncSafeObject())}

	startTime = time.Now()
	for i := 0; i < wCount; i++ {
		go testWorkerAnalog(i, &dictAnalog, &doneChannel)
	}
	for doneCount < wCount {
		done := <-doneChannel
		if done {
			doneCount += 1
		}
	}
	atime := float32(time.Since(startTime).Seconds())
	t.Logf("real: %f analog: %f\n", rtime, atime)
	if rtime > atime {
		t.Error("broken improvement")
	}
}
Example #2
0
func TestAsyncStrDictFillingWithDuplication(t *testing.T) {
	dictPtr := helpers.NewAsyncStrDict()
	var n, wCount, doneCount int
	n = 1000
	wCount = 10
	doneChannel := make(chan bool, wCount)
	testWorker := func(index int, dict *helpers.AsyncStrDict, done *chan bool) {
		for i := 0; i < n; i++ {
			dict.Set(
				fmt.Sprintf("%d-%d", 1, i+1),
				fmt.Sprintf("%d.%d", index, i))
		}
		*done <- true
	}
	// run workers
	for i := 0; i < wCount; i++ {
		go testWorker(i, dictPtr, &doneChannel)
	}
	for doneCount < wCount {
		done := <-doneChannel
		if done {
			doneCount += 1
		}
	}
	s := dictPtr.Size()
	if n != s {
		t.Errorf("Dict size: %d", s)
	}
}
Example #3
0
func TestAsyncStrDictGetSetCheck(t *testing.T) {
	dictPtr := helpers.NewAsyncStrDict()
	var n, wCount, doneCount int
	n = 1000
	wCount = 10
	resultChannel := make(chan int, wCount)
	// 1 - done
	// 0 - error
	testWorker := func(read bool, index int, dict *helpers.AsyncStrDict, done *chan int) {
		result := 1
		for i := 0; i < n; i++ {
			if read {
				res := dict.Get(fmt.Sprintf("%d-%d", index+1, i+1))
				if res != nil {
					if *res != fmt.Sprintf("%d.%d", index, i) {
						result = 0
						break
					}
				} else {
					result = 0
					break
				}
			} else {
				dict.Set(
					fmt.Sprintf("%d-%d", index+1, i+1),
					fmt.Sprintf("%d.%d", index, i))
			}
		}
		*done <- result
	}
	// run workers
	for i := 0; i < wCount; i++ {
		go testWorker(false, i, dictPtr, &resultChannel)
	}
	time.Sleep(time.Duration(250) * time.Millisecond)
	for i := 0; i < wCount; i++ {
		go testWorker(true, i, dictPtr, &resultChannel)
	}
	hasError := false
	for doneCount < wCount*2 {
		done := <-resultChannel
		if done == 1 {
			doneCount += 1
		}
		if done == 0 {
			hasError = true
			doneCount += 1
		}
	}
	s := dictPtr.Size()
	if n*wCount != s {
		t.Errorf("Dict size: %d", s)
	}
	if hasError {
		t.Error("Incorrect value")
	}
}
Example #4
0
func TestAsyncStrDictFilling(t *testing.T) {
	dictPtr := helpers.NewAsyncStrDict()
	var n, wCount, doneCount int
	n = 1000
	wCount = 10
	doneChannel := make(chan bool, wCount)
	testWorker := func(index int, dict *helpers.AsyncStrDict, done *chan bool, clear bool) {
		for i := 0; i < n; i++ {
			if clear {
				dict.Delete(fmt.Sprintf("%d-%d", index+1, i+1))
			} else {
				dict.Set(
					fmt.Sprintf("%d-%d", index+1, i+1),
					fmt.Sprintf("%d.%d", index, i))
			}
		}
		*done <- true
	}
	// run workers
	for i := 0; i < wCount; i++ {
		go testWorker(i, dictPtr, &doneChannel, false)
	}
	for doneCount < wCount {
		done := <-doneChannel
		if done {
			doneCount += 1
		}
	}
	s := dictPtr.Size()
	if n*wCount != s {
		t.Errorf("Dict size: %d", s)
	}
	// clear
	doneCount = 0
	for i := 0; i < wCount; i++ {
		go testWorker(i, dictPtr, &doneChannel, true)
	}
	for doneCount < wCount {
		done := <-doneChannel
		if done {
			doneCount += 1
		}
	}
	if dictPtr.Size() != 0 {
		t.Error("Dict size must be 0")
	}
}
Example #5
0
		size := set.Size()
		if size > 0 {
			result = make([]string, size)
			index := 0
			for cid, _ := range set.set {
				result[index] = cid
				index++
			}
		}
	}
	return result
}

var onceRpcServerManager = RpcServerManager{
	AsyncSafeObject:     *(helpers.NewAsyncSafeObject()),
	ResultDirectionDict: helpers.NewAsyncStrDict(),
	ResultBufferDict:    helpers.NewAsyncStrDict(),
	methods:             make(map[string]*CidSet)}

func NewRpcServerManager() *RpcServerManager {
	// use as singltone
	return &onceRpcServerManager
}

// main instruction commnad router
type MethodInstructionDict struct {
	helpers.AsyncSafeObject
	content map[string]int
}

var onceMethodInstructionDict MethodInstructionDict = MethodInstructionDict{