Advertisement
LDDestroier

ComputerCraft Ping

Oct 23rd, 2015
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.09 KB | None | 0 0
  1. -- pastebin get R4DKXbzm ping
  2. -- std pb R4DKXbzm ping
  3.  
  4. function displayHelp()
  5.     print("ping <url> <times>")
  6. end
  7.  
  8. function averageTable(tab)
  9.     temp = 0
  10.     for a = 1, #tab do
  11.         temp = temp + tab[a]
  12.     end
  13.     temp = temp / #tab
  14.     return temp
  15. end
  16.  
  17. tArg = {...}
  18. url = tArg[1]
  19. times = tonumber(tArg[2])
  20. if not url then
  21.     displayHelp()
  22.     return false
  23. end
  24. if not times then times = 4 end
  25.  
  26. if times <= 0 then
  27.     error("must be greater than 0.")
  28. elseif times >= 200 then
  29.     error("Seriously?")
  30. end
  31.  
  32. if not string.find(url, "://") then
  33.     url = "http://"..url
  34. end
  35.  
  36. print("Pinging "..url.."...")
  37. completeRatio = {}
  38. pingList = {}
  39. for a = 1, times do
  40.     sleep(0)
  41.     oldtime = os.time()
  42.     b = http.get(url)
  43.     newtime = os.time()
  44.     diff = math.ceil((newtime - oldtime)*1000)
  45.     table.insert(pingList, diff)
  46.     write(" delay: "..diff.." ticks...("..a..")")
  47.     if b then
  48.         table.insert(completeRatio,1)
  49.         print("")
  50.     else
  51.         table.insert(completeRatio,0)
  52.         print("fail")
  53.     end
  54. end
  55. print("done with "..(averageTable(completeRatio)*100).."% success")
  56. print("average ping is "..(averageTable(pingList)).." ticks")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement