github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgNet/netTester/bigWrite.go (about)

     1  package netTester
     2  
     3  import (
     4  	"bytes"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/bronze1man/kmg/kmgNet"
     9  	"github.com/bronze1man/kmg/kmgTime"
    10  )
    11  
    12  //一次写入大量数据(超过mtu即可)
    13  func bigWrite(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
    14  	kmgTime.MustNotTimeout(func() {
    15  		listener := runEchoServer(listenerNewer)
    16  		defer listener.Close()
    17  
    18  		toWrite := bytes.Repeat([]byte{0}, 1024*100)
    19  
    20  		func() {
    21  			//先写后读
    22  			wg := sync.WaitGroup{}
    23  			conn1, err := Dialer()
    24  			mustNotError(err)
    25  			if debug {
    26  				conn1 = kmgNet.NewDebugConn(conn1, "bigWrite1")
    27  			}
    28  			defer conn1.Close()
    29  			wg.Add(1)
    30  			go func() {
    31  				for i := 0; i < 2; i++ {
    32  					mustReadSame(conn1, toWrite)
    33  				}
    34  				wg.Done()
    35  			}()
    36  			for i := 0; i < 2; i++ {
    37  				_, err = conn1.Write(toWrite)
    38  				mustNotError(err)
    39  				time.Sleep(time.Microsecond)
    40  			}
    41  			wg.Wait()
    42  			conn1.Close()
    43  		}()
    44  
    45  		func() {
    46  			//先读后写
    47  			conn1, err := Dialer()
    48  			mustNotError(err)
    49  			if debug {
    50  				conn1 = kmgNet.NewDebugConn(conn1, "bigWrite2")
    51  			}
    52  			defer conn1.Close()
    53  			go func() {
    54  				for i := 0; i < 2; i++ {
    55  					_, err := conn1.Write(toWrite)
    56  					mustNotError(err)
    57  					time.Sleep(time.Microsecond)
    58  				}
    59  			}()
    60  			for i := 0; i < 2; i++ {
    61  				mustReadSame(conn1, toWrite)
    62  			}
    63  			conn1.Close()
    64  		}()
    65  
    66  		listener.Close()
    67  	}, time.Second)
    68  }