使用NLC来辅助网络相关程序的测试
2012-05-18 09:40
489 查看
Network Link Conditioner
安装:A:
It's in an additional download. Use this menu item:
Xcode > Open Developer Tool > More Developer Tools...
and get "Hardware IO Tools for Xcode".
B:
/System/Library/PreferencePanes, /Library/PreferencePanes, or ~/Library/PreferencePanes. If you double-click
the prefpane
介绍:这个工具可以用于各种开发,比如iphone app(模拟器测试),网页开发(测试脚本的response,下载需要的时间等)
Throttling comes in as a need for various reasons. I’ve wanted to throttle my connection a number of times to test the sequence of JavaScript events that get fired, check out how the initial load of a page would feel, or include some graceful fallback for those
times when a connection would completely drop. Formerly, I would use the Charles
Web Debugging Proxyand it was awesome.
Charles, at a very high level, was a proxy that you could direct your requests through and it would throttle how fast things took place. The recording tools were awesome, but way beyond what I was needing to do. The other issue with it was a required dependency
on a Firefox Add-on. With that, you could only simulate alternative network conditions in a single browser.
==
Throttling comes in as a need for various reasons. I’ve wanted to throttle my connection a number of times to test the sequence of JavaScript events that get fired, check out how the initial load of a page would feel, or include some graceful fallback for those
times when a connection would completely drop. Formerly, I would use the Charles
Web Debugging Proxyand it was awesome.
Charles, at a very high level, was a proxy that you could direct your requests through and it would throttle how fast things took place. The recording tools were awesome, but way beyond what I was needing to do. The other issue with it was a required dependency
on a Firefox Add-on. With that, you could only simulate alternative network conditions in a single browser.
The Network Link Conditioner (NLC) tool is an extremely useful tool introduced with Lion that allows you to test how well an application behaves when subject to various network conditions. Whilst it is an OS X tool
that runs on a Mac it can still be used test iOS apps that are running in the iOS Simulator. In this post I will show some examples of how to use the tool to test an iOS app and verify how it well it handles various network error conditions.
Getting Up and Running
With Xcode installed on Lion you should find the NLC tool installed in /Developer/Applications/Utilities/Network Link Conditioner/. It is actually implemented as a System Preferences Pane which you can install witha double-click. The Xcode 4.2 release notes mention a limitation that you need to reboot before the NLC daemon will run or you can manually reload with the following command:
sudo launchctl load /system/library/launchdaemons/com.apple.networklinkconditioner.plist
Once installed you should find the Network Link Conditioner icon in the “Other” section of the System Preferences pane:
The User Interface is pretty simple, consisting of a master on/off switch, a list of pre-defined profiles and a manage profiles button to create your own profiles. Note that the NLC tool applies to the network interface
of your Mac so it will impact ALL traffic on your computer when you turn it on. So you probably want to avoid doing anything else that is relying on a network connection whilst you are testing.
Creating Test Profiles
The NLC can be used limit bandwidth, add delays both to normal traffic and to DNS queries and create packet loss. It comes pre-configured with a number of typical network profiles to allow you to simulate common
3G cellular, cable modem, DSL and WiFi connections. For testing purposes I created three rather extreme network conditions as follows:
Slow Net - Very Limited Bandwidth
This profile severely limits the bandwidth by setting both the Downlink and Uplink Bandwidth to 32kbps. A 100ms delay is also added to packets in each direction and for good measure there is also a 200ms delay toDNS queries.
This configuration is useful for slowing down all network communication so that you can really see what your app is doing. This helps flush out any points in your code where you have network code blocking the main
thread. It can also help you see any progress bars or temporary spinning activity indicators that might otherwise only be displayed for a split second when testing on a fast network.
Nasty Net - High Packet Loss
This network profile increases the pain levels even further by dropping 90% of all packets in each direction:
Black Hole - 100% Downlink Packet Loss
Finally I have a network profile which simply drops ALL downlink packets. This is useful for testing network timeout conditions. Try starting with Slow Net and switching to Black Hole once your network traffic getsunderway if you really want to be mean….
Putting an App to the Test
So to try things out I will run the Twitter Search app that I used to exploreNSURLConnection in the iOS Simulator. If you are interested in the code under test refer back to that earlier post for details. The app takes a single string and then uses NSURLConnection to query the Twitter search API and display the results in a table view.
Normally when run in the iOS Simulator the results are displayed in under a second which makes it hard to see what is happening. If I turn on the Slow Net profile though the query takes 5-10 seconds making it a lot easier to verify how the app behaves.
Since we are using an NSURLConnection the actual network connection happens asynchronously, the user interface is not blocked and our table view, displaying a Loading… message responds to touches. In addition we
can see that the network activity spinner has been activated (more on this in a moment) to inform the user that we accessing the network.
To further test what happens when our network connection fails we will switch to the Black Hole profile which will prevent all responses from reaching our app. Now if we wait long enough the network connection should
time out and we can test how well we cope with that situation. Our application implements the connection:didFailWithError: delegate method of NSURLConnection to display the error message and we should also see our status message in the first row of the table
update to indicate that the service is not available. Sure enough after about a minute the connection times out and the user interface responds correctly:
This is all very good but whilst playing around with this app I actually spotted some unintended behaviour (ok that is another way of saying a bug…). With either the Slow Net or the Nasty Net profile enabled it is
very easy to reproduce. As I mentioned previously whilst we are waiting for the search query to complete so we can display the results the user interface is not blocked. This means that the user can hit the Search button in the Navigation bar to return to
the initial screen. When they do that the NSURLConnection is still running and the network activity indicator is still active. Now it will eventually time out and clean itself up but there is nothing to stop the user from initiating another search before the
old one has timed out. That is certainly not what I intended when I wrote the app but I never really considered this situation. The fix is easy if we implement the viewWillDisappear method in our view controller and use that to cancel the connection:
- (void)viewWillDisappear:(BOOL)animated
{
[self cancelConnection];
}
The cancelConnection method takes care of cleaning things up for us:
- (void)cancelConnection
{
if (self.connection != nil)
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self.connection cancel];
self.connection = nil;
self.buffer = nil;
}
}
Now when we exit the search results table view controller any running connection is cancelled, the network activity spinner is stopped and the temporary buffer is cleaned up.
Wrapping Up
When I started out to write this post I was already sure that testing applications under challenging network conditions was a good idea. However I guess the fact that I actually also found an error in some previously
posted code further demonstrates the point. You can find the updated code in my github CodeExamples repository.
One final note, don’t forget to turn NLC off when your done or you may find your Github commits take a bit longer than expected….
1
Comment | tagged
networking
View
Printer Friendly Version
Article to Friend
Reader Comments (1)
Hi,Great post! Thanks a lot for sharing!
I noticed that since Xcode 4.3 the Network Link Conditioner is available as an addition atDownloads for Apple
Developers in the "Hardware IO Tools for Xcode".
Antoine
February 18, 2012 |
Antoine
Marcadet
Post
a New Comment
Enter your information below to add a new comment.My response is on my own website »
Author:
Author Email (optional):
Author URL (optional):
Post:
↓ | ↑
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Notify me of follow-up comments via email.
Throttling comes in as a need for various reasons. I’ve wanted to throttle my connection a number of times to test the sequence of JavaScript events that get fired, check out how the initial load of a page would feel, or include some graceful fallback for those
times when a connection would completely drop. Formerly, I would use the Charles
Web Debugging Proxyand it was awesome.
Charles, at a very high level, was a proxy that you could direct your requests through and it would throttle how fast things took place. The recording tools were awesome, but way beyond what I was needing to do. The other issue with it was a required dependency
on a Firefox Add-on. With that, you could only simulate alternative network conditions in a single browser.
相关文章推荐
- 一些linux应用小技巧,网络相关,系统管理,shell,程序使用
- snmp++3.3.7在2013下编译成库,并测试例子程序(网络管理相关)
- 编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。
- 微软MSDN给出的使用.Net开发Windows Form网络应用相关程序的优化建议
- 编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。
- 最简单的使用程序进行网络链接测试
- 最简单的使用程序进行网络链接测试
- zf2框架的PHP程序,测试环境使用apache,准生产环境使用nginx,出现的问题
- 使用Java测试网络连通性的几种方法
- 使用 Watir 加速面向 Web 应用的自动化测试程序的开发
- 使用UDP协议编写一个网络程序,设置接收端程序的监听端口是8001,发送端发送的数据是“Hello, world”。
- 使用Java测试网络连通性的几种方法
- [翻译]DrawMe - 使用WPF/WCF创建的网络白板聊天程序
- Linux网络测试工具Netperf安装及使用
- InternetOpen/InternetOpenUrl/InternetReadFile 等相关Win32 网络API 使用详细说明
- NET程序内存分析工具CLRProfiler的使用(性能测试)
- 查看使用网络端口的程序
- Selenium-webdriver系列教程(十)————使用jquery辅助进行测试
- 使用BSD socket编写Windows版的网络程序
- 使用Derby工具和启动辅助程序