File: testasyncspeed.lua

package info (click to toggle)
lua-copas 4.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: makefile: 62; sh: 43
file content (58 lines) | stat: -rw-r--r-- 1,210 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local copas = require("copas")
local synchttp = require("socket.http").request
local asynchttp = copas.http.request
local gettime = require("socket").gettime

local targets = {
  "http://www.google.com",
  "http://www.microsoft.com",
  "http://www.apple.com",
  "http://www.facebook.com",
  "http://www.yahoo.com",
}

local function sync(list)
  for _, host in ipairs(list) do
    local res = synchttp(host)
    if not res then
      print("Error sync: "..host.." failed, rerun test!")
    else
      print("Sync host done: "..host)
    end
  end
end

local handler = function(host)
  local res = asynchttp(host)
  if not res then
    print("Error async: "..host.." failed, rerun test!")
  else
    print("Async host done: "..host)
  end
end

local function async(list)
  for _, host in ipairs(list) do copas.addthread(handler, host) end
  copas.loop()
end

-- three times to remove caching differences
async(targets)
async(targets)
async(targets)
print("\nNow starting the real test...\n")

local t1 = gettime()
print("Sync:")
sync(targets)
local t2 = gettime()
print("Async:")
async(targets)
local t3 = gettime()

print("\nResults:")
print("========")
print("   Sync : ", t2-t1)
print("   Async: ", t3-t2)