File: rpc2-fail.lua

package info (click to toggle)
rpc2 2.7%2Bdebian-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,852 kB
  • ctags: 2,661
  • sloc: ansic: 19,928; sh: 9,110; lex: 437; yacc: 416; makefile: 126; asm: 35
file content (42 lines) | stat: -rw-r--r-- 972 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
--
-- Artificially introduce packet delays or failures to simulate a slow and/or
-- unreliable network.
--

print "enabling packet delay/failure functionality"

local packet_loss = 0.1		-- % chance of packet loss
local bandwidth = 64000		-- desired bandwidth (bits/s)
local latency_min = 0.10	-- network latency
local latency_max = 0.15

bandwidth = bandwidth / 8
lat_range = latency_max - latency_min

function slow(queue)
    local drained = time()
    return function (addr, size, color)
	-- packet loss
	if math.random() < packet_loss then return nil end

	-- bandwidth delay
	local now = time()
	local delay = size / bandwidth
	if drained < now then drained = now end
	drained = drained + delay

	-- network delay
	local latency = latency_min
	if lat_range then
	    latency = latency + math.random() * lat_range
	end

	delay = drained - now + latency
	print("delaying", queue, delay)
	return delay
    end
end

fail_delay_tx = slow("tx")
fail_delay_rx = slow("rx")