async function pingLine(line, method) { return new Promise(function (resolve) { let result = {} let xhr = new XMLHttpRequest; result.start_time = (new Date()).getTime(); xhr.onreadystatechange = function () { if (xhr.readyState == 1) { } else if (xhr.readyState === 4) { if (xhr.status == 200) { result.cost = ((new Date()).getTime() - result.start_time) / 1000; } else { result.cost = 9999; } resolve(result); } } if (method.method == 'POST') { xhr.open("POST", line.url + '?line=' + method.post_size, true); } else { xhr.open("GET", line.url, true); } xhr.timeout = 60000; xhr.send(method.array); }); } function initTable(lines, methods) { let t = document.createElement('table'); t.className = 'table'; t.style = 'table-layout:fixed; width:80%; margin: 20px auto;'; let thead = document.createElement('thead'); let tr = document.createElement('tr'); let head_texts = ['序号', '线路']; methods.forEach(function(method) { head_texts.push(method.method === 'GET' ? 'Get' : 'Post ' + method.post_size + ' MB'); }); head_texts.forEach(function(text){ let th = document.createElement('th'); th.appendChild(document.createTextNode(text)); tr.appendChild(th); }); thead.appendChild(tr); t.appendChild(thead); let tbdy = document.createElement('tbody'); for (i = 0; i < lines.length; i++) { let tr = document.createElement('tr'); let td1 = document.createElement('td'); td1.appendChild(document.createTextNode(i + 1)); tr.appendChild(td1); let td2 = document.createElement('td'); let a = document.createElement('a'); a.href = 'https://member.bilibili.com/v2#/upload/video/frame/?' + lines[i].query; a.innerText = lines[i].bref; td2.appendChild(a); tr.appendChild(td2); for (j = 0; j < methods.length; j++) { let td = document.createElement('td'); tr.appendChild(td); } tbdy.appendChild(tr); } t.appendChild(tbdy) let div = document.getElementById('ping'); div.appendChild(t); } function showResult(lineIdx, methodIdx, ret) { let div = document.getElementById('ping'); let t = div.getElementsByTagName('table')[0]; let tbdy = t.getElementsByTagName('tbody')[0]; let tr = tbdy.getElementsByTagName('tr')[lineIdx]; let td = tr.getElementsByTagName('td')[2 + methodIdx]; td.appendChild(document.createTextNode(ret.cost.toFixed(3) + ' 秒')); } async function hideloading() { document.getElementById('loading').style.display = 'none'; } var lines = [{ bref: 'cs-bldsa', query: 'os=upos&zone=cs&upcdn=bldsa', url: '//upos-cs-upcdnbldsa.bilivideo.com/OK', }, { bref: 'cs-bda2', query: 'os=upos&zone=cs&upcdn=bda2', url: '//upos-cs-upcdnbda2.bilivideo.com/OK', }, { bref: 'cs-tx', query: 'os=upos&zone=cs&upcdn=tx', url: '//upos-cs-upcdntx.bilivideo.com/OK', }, { bref: 'cs-txa', query: 'os=upos&zone=cs&upcdn=txa', url: '//upos-cs-upcdntxa.bilivideo.com/OK', }, { bref: 'cs-alia', query: 'os=upos&zone=cs&upcdn=alia', url: '//upos-cs-upcdnalia.bilivideo.com/OK', }, { bref: 'jd-bldsa', query: 'os=upos&zone=jd&upcdn=bldsa', url: '//upos-jd-upcdnbldsa.bilivideo.com/OK', }, { bref: 'jd-bd', query: 'os=upos&zone=jd&upcdn=bd', url: '//upos-jd-upcdnbd.bilivideo.com/OK', }, { bref: 'jd-tx', query: 'os=upos&zone=jd&upcdn=tx', url: '//upos-jd-upcdntx.bilivideo.com/OK', }, { bref: 'jd-txa', query: 'os=upos&zone=jd&upcdn=txa', url: '//upos-jd-upcdntxa.bilivideo.com/OK', }, { bref: 'jd-alia', query: 'os=upos&zone=jd&upcdn=alia', url: '//upos-jd-upcdnalia.bilivideo.com/OK', }]; let methods = [] let size_array = [0.1, 1, 10]; for (let i = 0; i < size_array.length; i++) { let array = new ArrayBuffer(size_array[i] * 1024 * 1024); var longInt8View = new Uint8Array(array); for (let j = 0; j < longInt8View.length; j++) { longInt8View[j] = i % 255; } methods[i] = { post_size: size_array[i], array: array, method: 'POST', } } methods[methods.length] = { post_size: 0, method: 'GET', } async function allMethod() { initTable(lines, methods) for (let i = 0; i < methods.length; i++) { for (let j = 0; j < lines.length; j++) { let ret = await pingLine(lines[j], methods[i]); showResult(j, i, ret); } } await hideloading(); } allMethod();