Beispiel #1
0
func doTransport() {
	var wg sync.WaitGroup

	ver := testVersion(1)
	n_trans := make([]*gofast.Transport, 0, 16)
	for i := 0; i < options.conns; i++ {
		wg.Add(1)
		config := newconfig("client", 3000, 4000)
		config["tags"] = options.tags
		conn, err := net.Dial("tcp", options.addr)
		if err != nil {
			panic(err)
		}
		trans, err := gofast.NewTransport(conn, &ver, nil, config)
		if err != nil {
			panic(err)
		}
		n_trans = append(n_trans, trans)

		go func(trans *gofast.Transport) {
			trans.FlushPeriod(100 * time.Millisecond)
			trans.SubscribeMessage(&msgPost{}, nil).Handshake()
			msgpost := &msgPost{data: make([]byte, options.payload)}
			for i := 0; i < options.payload; i++ {
				msgpost.data[i] = 'a'
			}
			trans.SubscribeMessage(&msgReqsp{}, nil)
			msgreq := &msgReqsp{data: make([]byte, options.payload)}
			for i := 0; i < options.payload; i++ {
				msgreq.data[i] = 'a'
			}
			trans.SubscribeMessage(&msgStream{}, nil)
			msgstrm := &msgStream{data: make([]byte, options.payload)}
			for i := 0; i < options.payload; i++ {
				msgstrm.data[i] = 'a'
			}

			switch options.do {
			case "post":
				doPost(trans, options.routines, msgpost)
			case "request":
				doRequest(trans, options.routines, msgreq)
			case "stream":
				doStream(trans, options.routines, msgstrm)
			case "random":
				d, r := options.routines/3, options.routines%3
				doPost(trans, d, msgpost)
				doRequest(trans, d, msgreq)
				doStream(trans, d+r, msgstrm)
			}
			wg.Done()
			trans.Close()
		}(trans)
	}
	wg.Wait()
	printCounts(addCounts(n_trans...))
	fmsg := "request stats: n:%v mean:%v var:%v sd:%v\n"
	n, m := av.Count(), time.Duration(av.Mean())
	v, s := time.Duration(av.Variance()), time.Duration(av.Sd())
	fmt.Printf(fmsg, n, m, v, s)
}
Beispiel #2
0
func runserver(lis net.Listener) {
	ver := testVersion(1)
	config := newconfig("server", 1000, 2000)
	config["tags"] = options.tags
	for {
		if conn, err := lis.Accept(); err == nil {
			fmt.Println("new transport", conn.RemoteAddr(), conn.LocalAddr())
			trans, err := gofast.NewTransport(conn, &ver, nil, config)
			if err != nil {
				panic("NewTransport server failed")
			}
			mu.Lock()
			n_trans = append(n_trans, trans)
			mu.Unlock()
			go func(trans *gofast.Transport) {
				trans.FlushPeriod(100 * time.Millisecond)
				trans.SubscribeMessage(
					&msgPost{},
					func(s *gofast.Stream,
						msg gofast.Message) chan gofast.Message {

						trans.Free(msg)
						return nil
					})
				trans.SubscribeMessage(
					&msgReqsp{},
					func(s *gofast.Stream,
						msg gofast.Message) chan gofast.Message {

						if err := s.Response(msg, false); err != nil {
							log.Fatal(err)
						}
						trans.Free(msg)
						return nil
					})
				trans.SubscribeMessage(
					&msgStream{},
					func(s *gofast.Stream,
						msg gofast.Message) chan gofast.Message {

						ch := make(chan gofast.Message, 100)
						if err := s.Stream(msg, false); err != nil {
							log.Fatal(err)
						}
						trans.Free(msg)
						//fmt.Println("started", msg)
						go func() {
							for msg := range ch {
								//fmt.Println("count", msg)
								if err := s.Stream(msg, false); err != nil {
									log.Fatal(err)
								}
								trans.Free(msg)
							}
							s.Close()
						}()
						return ch
					})
				trans.Handshake()
				tick := time.Tick(1 * time.Second)
				for {
					<-tick
					if options.log == "debug" {
						printCounts(trans.Counts())
					}
				}
			}(trans)
		}
	}
}