コード例 #1
0
ファイル: runner.go プロジェクト: glestaris/clique
func ListenAndServe(cfg ServerConfig) (transfer.TransferResults, error) {
	var res transfer.TransferResults

	pipeR, pipeW, err := os.Pipe()
	if err != nil {
		return res, fmt.Errorf("creating pipe: %s", err)
	}
	defer pipeR.Close()
	defer pipeW.Close()

	result := C.ir_listen_and_serve(
		cfg.ToIRServerConfig(), C.int(pipeW.Fd()),
	)
	if result != C.IR_RESULT_SUCCESS {
		return res, errors.New(C.GoString(C.ir_strerror(C.IRResult(result))))
	}

	rep := report{}
	if err := json.NewDecoder(pipeR).Decode(&rep); err != nil {
		return res, fmt.Errorf("decoding iperf response: %s", err)
	}

	res.BytesSent = uint32(rep.End.SumReceived.Bytes)
	durStr := fmt.Sprintf("%ss", strconv.FormatFloat(
		rep.End.SumSent.Seconds, 'f', -1, 64,
	))
	res.Duration, err = time.ParseDuration(durStr)
	if err != nil {
		return res, fmt.Errorf("parsing duration: %s", err)
	}

	return res, nil
}
コード例 #2
0
ファイル: runner.go プロジェクト: glestaris/clique
func RunTest(cfg ClientConfig) (transfer.TransferResults, error) {
	var res transfer.TransferResults

	pipeR, pipeW, err := os.Pipe()
	if err != nil {
		return res, fmt.Errorf("creating pipe: %s", err)
	}
	defer pipeR.Close()
	defer pipeW.Close()

	result := C.ir_run_test(
		cfg.ToIRClientConfig(), C.int(pipeW.Fd()),
	)
	if result != C.IR_RESULT_SUCCESS {
		return res, errors.New(C.GoString(C.ir_strerror(C.IRResult(result))))
	}

	rep := report{}
	if err := json.NewDecoder(pipeR).Decode(&rep); err != nil {
		return res, fmt.Errorf("decoding iperf response: %s", err)
	}

	res.BytesSent = uint32(rep.End.SumSent.Bytes)
	durStr := fmt.Sprintf("%ss", strconv.FormatFloat(
		rep.End.SumSent.Seconds, 'f', -1, 64,
	))
	res.Duration, err = time.ParseDuration(durStr)
	if err != nil {
		return res, fmt.Errorf("parsing duration: %s", err)
	}
	if len(rep.End.Streams) != 0 {
		meanRTT := rep.End.Streams[0].Sender.MeanRTT
		res.RTT = time.Microsecond * time.Duration(meanRTT)
	}

	return res, nil
}