Beispiel #1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	stat = actionstat.New()
	var listenFrom = flag.String("listenFrom", ":6666", "server ip/port")
	var connCount = flag.Int("count", 1000, "connection count")
	var connThrottle = flag.Int("throttle", 10, "connection throttle")
	var rundur = flag.Int("rundur", 3600, "run sec")
	var profilefilename = flag.String("pfilename", "", "profile filename")
	flag.Parse()

	if *profilefilename != "" {
		f, err := os.Create(*profilefilename)
		if err != nil {
			log.Fatalf("profile %v", err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	go gogueserver.TCPServer(*listenFrom, *connCount, *connThrottle, servergo)

	go func() {
		timerInfoCh := time.Tick(time.Duration(1000) * time.Millisecond)
		for {
			select {
			case <-timerInfoCh:
				log.Info("%v", stat)
				stat.UpdateLap()
			}
		}
	}()
	time.Sleep(time.Duration(*rundur) * time.Second)
}
Beispiel #2
0
func TCPServer(listenString string, connNum int, connThrottle int, runConn ServerGoFn) {
	log.Info("Start server %v", listenString)
	// concurrent connection count control
	clientQueue := make(chan bool, connNum)

	listener, err := net.Listen("tcp", listenString)
	if err != nil {
		log.Error("%v", err)
		return
	}
	defer listener.Close()

	for {
		time.Sleep(time.Duration(connThrottle) * time.Millisecond)
		clientQueue <- true
		conn, err := listener.Accept()
		if err != nil {
			log.Error("%v", err)
		} else {
			go runConn(gogueconn.New(conn), clientQueue)
		}
	}
}