Created
September 17, 2014 18:24
-
-
Save margusmartsepp/0617df9345ea7a37b5b0 to your computer and use it in GitHub Desktop.
NUnit tests comparing sync, async, lazy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //PM> Install-Package Rx-Linq | |
| readonly List<string> _list = new List<string> { "http://www.google.com", "https://www.gmail.com", "http://www.aripaev.ee" }; | |
| private readonly string format = "[{0}] {1} : {2} [{3}]"; | |
| [Category("WebClient")] | |
| [TestCase("sync" )] | |
| public void SynchronousTest(string name) | |
| { | |
| DateTime start = DateTime.Now; | |
| var dict = _list.ToDictionary(o => o, o => new WebClient().DownloadString(new Uri(o))); | |
| dict.Keys.ToList().ForEach(o => Console.WriteLine(format, DateTime.Now - start, o, dict[o].Length, name)); | |
| } | |
| [Category("WebClient")] | |
| [TestCase("async")] | |
| public void AsynchronousTest(string name) | |
| { | |
| DateTime start = DateTime.Now; | |
| var dict = _list.ToDictionary(o => o, async o => await new WebClient().DownloadStringTaskAsync(new Uri(o))); | |
| dict.Keys.ToList().ForEach(o => Console.WriteLine(format, DateTime.Now - start, o, dict[o].Result.Length, name)); | |
| } | |
| [Category("WebClient")] | |
| [TestCase("lazy")] | |
| public void LazyTest(string name) | |
| { | |
| var start = DateTime.Now; | |
| var dict = _list.ToDictionary(o => o, o => new Lazy<string>(() => new WebClient().DownloadString(new Uri(o)))); | |
| dict.Keys.ToList().ForEach(o => Console.WriteLine(format, DateTime.Now - start, o, dict[o].Value.Length, name)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [00:00:00.9520952] http://www.google.com : 51348 [sync] | |
| [00:00:00.9520952] https://www.gmail.com : 58704 [sync] | |
| [00:00:00.9520952] http://www.aripaev.ee : 208324 [sync] | |
| [00:00:00.0010001] http://www.google.com : 51412 [lazy] | |
| [00:00:00.6550655] https://www.gmail.com : 58664 [lazy] | |
| [00:00:00.7690769] http://www.aripaev.ee : 208324 [lazy] | |
| [00:00:00.1430143] http://www.google.com : 51366 [async] | |
| [00:00:00.3430343] https://www.gmail.com : 58616 [async] | |
| [00:00:00.5150515] http://www.aripaev.ee : 208324 [async] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment