PHP Developers Network
http://forums.devnetwork.net/

Network emulator
http://forums.devnetwork.net/viewtopic.php?f=39&t=118933
Page 1 of 2

Author:  VladSun [ Tue Jul 20, 2010 4:27 pm ]
Post subject:  Network emulator

Linux only! (of course)
It will emulate network delay, network packet loss and network bandwidth limits.
Edit the values in the EDIT section.

Syntax: [ Download ] [ Hide ]
#!/bin/bash

#-EDIT BEGIN-----------------------------------------------------------------------------------------#


DELAY=100                       #[ms]
DELAY_VARIATON=10               #[ms]
DELAY_CORRELATION=10            #[%]

PACKET_LOSS=0.5                 #[%]
PACKET_LOSS_CORRELATION=        #[%]

TRAFFIC_RATE=300                #[kbit]

DEV="lo"                        #[ lo | ethX | wlanX | etc. ]
IP="127.0.0.1"

#-EDIT END-------------------------------------------------------------------------------------------#

TC=`which tc`

$TC qdisc del dev $DEV root 2>/dev/null

if [ "$1" = "stop" ];
then
        exit 0
fi

$TC qdisc add dev $DEV handle 1: root htb r2q 1700
$TC class add dev $DEV parent 1:1 classid 1:20 htb rate ${TRAFFIC_RATE}kbit
$TC qdisc add dev $DEV parent 1:20 handle 12: netem delay ${DELAY}ms ${DELAY_VARIATON}ms ${DELAY_CORRELATION}% loss ${PACKET_LOSS} ${PACKET_LOSS_CORRELATION}
$TC filter add dev $DEV protocol ip parent 1: u32 match ip src $IP flowid 1:20


To stop emulation run ./your_file_name stop

Author:  josh [ Tue Jul 20, 2010 6:29 pm ]
Post subject:  Re: Network emulator

What would you test with this in PHP?

Author:  VladSun [ Wed Jul 21, 2010 1:02 am ]
Post subject:  Re: Network emulator

fsockopen :P

It's not PHP specific, it's a web development testing tool.

Author:  jarofgreen [ Sun Aug 29, 2010 11:00 am ]
Post subject:  Re: Network emulator

Progressive loading.

You want to make sure your web-page will look good as it loads slowly over a slow connection. One thing in particular is that people do a form, then do a JS onload event that attaches functionality to the inputs. If you do it badly, what can happen is that a user on a slow connection can get the form, start to use it, then the page finishes and the onload fires, and somehow, that form is messed up for that user.

Very handy. I was going to be looking for something like this soon myself. How does this work - does it effect traffic from a certain IP, so could I run it with the IP of my development server for instance?

Author:  josh [ Sun Aug 29, 2010 2:29 pm ]
Post subject:  Re: Network emulator

I think this is too pro for me to understand, vlad can you list an example how I would test this out? :D

Author:  Weirdan [ Sun Aug 29, 2010 3:32 pm ]
Post subject:  Re: Network emulator


Author:  VladSun [ Sun Sep 05, 2010 3:28 am ]
Post subject:  Re: Network emulator


Author:  Weirdan [ Tue Apr 19, 2011 10:40 am ]
Post subject:  Re: Network emulator

Interestingly, it works when I use it against local machine (DEV="lo" IP="127.0.0.1") but doesn't seem to work with remote connection (DEV="eth0" IP="xxx.xxx.xxx.xxx"). By "doesn't work" I mean bandwidth limits not being applied. I tried to limit bandwidth to 30kbit, but still get ~1.4MBps (megabytes per second) downloads from that remote host.

Tested using wget <url>

Author:  AbraCadaver [ Tue Apr 19, 2011 11:17 am ]
Post subject:  Re: Network emulator

Using this method you can only affect egress (outgoing packets). To affect ingress you have to use something like Intermediate Functional Block pseudo-device.

Author:  Weirdan [ Tue Apr 19, 2011 2:09 pm ]
Post subject:  Re: Network emulator


Author:  VladSun [ Tue Apr 19, 2011 2:15 pm ]
Post subject:  Re: Network emulator


Author:  VladSun [ Tue Apr 19, 2011 2:18 pm ]
Post subject:  Re: Network emulator


Author:  AbraCadaver [ Tue Apr 19, 2011 2:26 pm ]
Post subject:  Re: Network emulator

That would be hackish and kind of defeat the purpose here to be able to add real values such as bandwidth, latency, jitter, etc. and see the effects. Use IFB, something like:

Syntax: [ Download ] [ Hide ]
modprobe ifb
ip link set dev ifb0 up
tc qdisc add dev eth0 ingress
tc filter add dev eth0 parent ffff: protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0

# then start using the ifb0 dev
tc qdisc add dev ifb0 root netem delay 100ms


It's been a while, but as far as delay goes you may have to use the one-way latency since it will be applied ingress and egress. So the above would give 200ms round-trip latency.

Author:  Weirdan [ Tue Apr 19, 2011 3:14 pm ]
Post subject:  Re: Network emulator

You see, I don't need anything fancy and there's no requirements for any kind of precise control. Just make all connections to specific host(s) "slow", make them fast again. It's to be used in testing to see how web application would behave for a client on a slow connection.

For now what you guys discussing is a bit over my head, as I only have a basic understanding of network stack and absolutely no experience with linux networking (I had some experience on FreeBSD, but that was like 10 years ago).

Author:  VladSun [ Tue Apr 19, 2011 4:42 pm ]
Post subject:  Re: Network emulator

Nice one, AbraCadaver :)
Maybe Weirdan needs something less complex like in http://blog.stevedoria.net/20050906/ing ... nux-and-tc

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/