Exemple #1
0
func rx_stream(client net.Conn, proto string, msglen int) {
	buffer := make([]byte, msglen)
	server, e := net.Listen("tcp", ":0")
	checke(e)

	if verbose > 0 {
		fmt.Println("stream listen on", server.Addr())
	}
	ports := strings.SplitAfter((server.Addr().String()), ":")
	client.Write([]byte(ports[len(ports)-1]))
	p, e := server.Accept()
	checke(e)
	length, e := p.Read(buffer)
	startns := time.Now().UnixNano()
	cpu_before, e := utilization.Read_cpu()
	checke(e)
	bytes := int64(0)
	messages := int64(0)
	for e == nil {
		bytes = bytes + int64(length)
		messages++
		length, e = p.Read(buffer)
	}
	if e != io.EOF {
		log.Fatal(e)
	}
	elapsedns := time.Now().UnixNano() - startns
	cpu_after, e := utilization.Read_cpu()
	checke(e)
	bandwidth := float64(bytes) / (float64(elapsedns) / float64(1000*1000*1000))
	bandwidth = bandwidth * 8.0 / (1000.0 * 1000.0)
	if verbose > 0 {
		fmt.Println("Elapsted time is ", elapsedns)
		fmt.Println("Read ", messages, " messages and ", bytes, " bytes")
		fmt.Println("Bandwidth is", bandwidth, "Mb/s")
	}
	u, ncpu := utilization.Calc_cpu(string(cpu_before), string(cpu_after))
	results := fmt.Sprintf("%s:%d:%f:%d:%d:%d:eof\n", "goodbye", ncpu, u, elapsedns, bytes, messages)
	_, e = client.Write([]byte(results))
	checke(e)
}
Exemple #2
0
func rx_rr(client net.Conn, proto string, msglen int, txlen int) {
	buffer := make([]byte, msglen)
	var rlen int
	var curlen int
	txbuffer := make([]byte, txlen)
	server, e := net.Listen("tcp", ":0")
	checke(e)

	if verbose > 1 {
		fmt.Println("rr listen on", server.Addr())
	}
	ports := strings.SplitAfter((server.Addr().String()), ":")
	client.Write([]byte(ports[len(ports)-1]))
	p, e := server.Accept()
	length := 0
	//	length, e  := p.Read(buffer)
	checke(e)
	cpu_before, e := utilization.Read_cpu()
	startns := time.Now().UnixNano()
	bytes := int64(0)
	messages := int64(0)
	for e == nil {
		length, e = p.Write(txbuffer)
		if e != nil {
			break
		}
		if length != txlen {
			fmt.Println("Short write! ", length)
		}
		if verbose > 3 {
			fmt.Println("message ", messages, "wrote=, ", length)
		}
		bytes = bytes + int64(length)
		messages++
		rlen = 0
		for rlen != msglen {
			curlen, e = p.Read(buffer)
			if verbose > 3 {
				fmt.Println("message ", messages, "curlen=, ", curlen)
			}
			rlen += curlen
			if rlen > msglen {
				fmt.Println("message ", messages, "after read, ", curlen, rlen)
				log.Fatal(e)
			}
			if e != nil {
				break
			}
		}
		if e != nil {
			break
		}
	}
	if e != io.EOF {
		fmt.Println("err =", e)
		log.Fatal(e)
	}
	elapsedns := time.Now().UnixNano() - startns
	cpu_after, e := utilization.Read_cpu()
	checke(e)
	bandwidth := float64(bytes) / (float64(elapsedns) / float64(1000*1000*1000))
	bandwidth = bandwidth * 8.0 / (1000.0 * 1000.0)
	if verbose > 0 {
		fmt.Println("Elapsted time is ", elapsedns)
		fmt.Println("Read ", messages, " messages and ", bytes, " bytes")
		fmt.Println("Bandwidth is", bandwidth, "Mb/s")
	}
	u, ncpu := utilization.Calc_cpu(string(cpu_before), string(cpu_after))
	results := fmt.Sprintf("%s:%d:%f:%d:%d:%d:eof\n", "goodbye", ncpu, u, elapsedns, bytes, messages)
	_, e = client.Write([]byte(results))
	checke(e)
}