vue中测试ping的时延和丢包率

42 篇文章 2 订阅

在这里插入图片描述
显示效果如上图所示,点击开始测试后,会默认测30次连接当前网址,然后根据状态给出时延和丢包结果(平均速度,最大速度,最小速度也可以得出),

<template>
  <div class="speed-test">
    <el-dialog title="测试网络环境" :visible.sync="speedOpen" width="30%" :close-on-click-modal="false">
      <p class="test-speed1">{{title}}</p>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="speedOpen=false">取 消</el-button> -->
        <el-button type="primary" @click="handStart">开始测试</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      speedOpen: false,
      nowTime: null,
      strURL:'',
      bolIsRunning:false,
      arrDelays:[],
      intSent: 0,
      intLost:0,
      intTimerID:null,
      bolIsTimeout:false,
      objIMG:{},
      title:''
    }
  },
  watch: {
    speedOpen: {
      immediate: true,
      handler: function(newVal) {
        if (newVal) {
            this.setStart()
            this.title = '请点击下方按钮开始测试!'
        }
      }
    }
  },
  methods: {
    changeVisible(visible = false) {
      this.speedOpen = visible
    },
    handEnd(){
        this.intRecv = this.arrDelays.length;
        this.intLost = this.intSent - this.intRecv;
        var sum = 0;
        for (var i = 0; i < this.intRecv; i++)
            sum += this.arrDelays[i];
        this.bolIsRunning = false;

        /*  * 统计结果  */
        // console.log("Ping的统计信息 for " + this.strURL + ":");
        window.console.log("包:发送="+this.intSent+",接收="+this.intRecv+",丢包="+this.intLost+"("+Math.floor(this.intLost/this.intSent*100)+"% 丢包率),");
        // console.log("最小速度="+Math.min.apply(this, this.arrDelays)+"ms, 最大速度 = "+Math.max.apply(this, this.arrDelays)+"ms, 平均速度 = " +Math.floor(sum / this.intRecv)+"ms");
        // document.querySelector('.test-speed1').innerText = `发送:${this.intSent},接收:${this.intRecv},丢包:${this.intLost},丢包率:${Math.floor(this.intLost/this.intSent*100)}`
        // document.querySelector('.test-speed2').innerText = `最小速度:${Math.min.apply(this, this.arrDelays)}ms,最大速度:${Math.max.apply(this, this.arrDelays)}ms,平均速度:${Math.floor(sum / this.intRecv)}ms`
        let avg = Math.floor(sum / this.intRecv)
        if(this.intLost>0){
            this.title = "网络环境不稳定"
        }else if(avg>0 && avg<=30){
            this.title = "网络环境非常好"
        }else if(avg>30 && avg<=50) {
            this.title = "网络环境良好"
        }else if(avg>50 && avg<=100) {
            this.title = "网络环境普通"
        }else if(avg>100) {
            this.title = "网络环境差"
        }
        if (this.intRecv == 0) return;
    },
    handStart(){
        this.title = '测试过程大概需要30秒,请耐心等待!'
        this.strURL = window.location.href
        if (this.strURL.length == 0)
            return;
        if (this.strURL.substring(0, 7).toLowerCase() != "http://")
            this.strURL = "http://" + this.strURL;
        this.intTimeout = 1000;
        this.bolIsRunning = true;
        this.arrDelays = [];
        this.intSent = 0;
        this.ping();
        setTimeout(()=>{this.handEnd()},30000)
    },
    setStart() {
      this.objIMG = new Image()
      this.objIMG.onload = this.objIMG.onerror = () => {
        /* * 有回应,取消超时计时 */
        clearTimeout(this.intTimerID)
        if (!this.bolIsRunning || this.bolIsTimeout) return
        var delay = new Date() - this.intStartTime
        window.console.log('连接成功 ' + this.strURL + ' time' + (delay < 1 ? '<1' : '=' + delay) + 'ms')
        this.arrDelays.push(delay)
        /* * 每次请求间隔限制在1秒以上 */
        setTimeout(()=>{this.ping()}, delay < 1000 ? (1000 - delay) : 1000)
      }
    },
    ping() {
        this.intStartTime = +new Date()
        this.intSent++
        this.objIMG.src = this.strURL + '/' + this.intStartTime
        this.bolIsTimeout = false
        /* * 超时计时 */
        this.intTimerID = setTimeout(()=>{this.timeout}, this.intTimeout)
    },
    timeout() {
        if (!this.bolIsRunning) return
        this.bolIsTimeout = true
        this.objIMG.src = 'X:\\'
        window.console.log('请求超时.')
        this.ping()
      }
  }
}
</script>

建议根据自己需求,选择性copy

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值