using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Net; using System.Reflection; using System.Threading; using System.Configuration; namespace FreeSwitchBreakRPC { class Program { private const int NumThreads = 30; private static ManualResetEvent[] resetEvents; private static DateTime start = DateTime.Now; private static int iTotReq = 0; private static long lTotLapse = 0; private static string url = ""; static void Main(string[] args) { resetEvents = new ManualResetEvent[NumThreads]; Thread.CurrentThread.Name = "Main"; SetUseUnsafeHeaderParsing(true); string host = "dvorak"; // url = @"http://192.168.149.128:8080/webapi/xml_locate?root"; // url = @"http://127.0.0.1:8080"; // url = @"http://dvorak:8080/webapi/xml_locate?root"; // url = @"http://localhost:8080/api/xml_locate?root"; // url = @"http://localhost:8080/api/status"; // url = @"http://dvorak:8080/txtapi/lua?lua/dialer.lua%20bye='Tot%20ziens!'"; if (args.Length == 1) { host = args[0]; } url = @"http://" + host + @":8080/txtapi/lua?lua/dialer.lua" + HttpUtility.UrlPathEncode(" bye='Tot ziëeàns!'"); for (int i = 0; i < NumThreads; i++) { resetEvents[i] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback(dowork), i); } start = DateTime.Now; Console.WriteLine("Url: {0}", url); Console.WriteLine("Waiting..."); WaitHandle.WaitAll(resetEvents); } private static void dowork(object o) { int index = (int)o; int iRequests = 0; DateTime dt = DateTime.Now; long lLapse = 0; Thread.CurrentThread.Name = index.ToString(); System.Net.WebClient client = new System.Net.WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); // create login credentials client.Credentials = new NetworkCredential("freeswitch","works"); while (true) { try { iRequests++; dt = DateTime.Now; string ret = string.Empty; if ((ret = client.DownloadString(url)) != null) { Interlocked.Increment(ref iTotReq); lLapse = (long) (10000 * (DateTime.Now - dt).TotalMilliseconds); Interlocked.Add(ref lTotLapse, lLapse); Console.WriteLine("Thread: {0,2}, req# {3,6}-{5,8}, rsp time: {4,6}/{6,4}/{7,4} time: {2}, xml size = {1}", Thread.CurrentThread.Name , ret.Length , DateTime.Now - start , iRequests , (int) lLapse/10000 // Current response time , iTotReq // read probably safe - most of the time // Total Requests , (int) (lTotLapse/iTotReq/10000) // Average Request Response time , (int) ((DateTime.Now - start).TotalMilliseconds / iTotReq ) // Average Request Response time ); } else { break; } } catch(Exception e) { Console.WriteLine("Thread:{0,2}, {1}", Thread.CurrentThread.Name, e.Message); // ignore and continue } Thread.Sleep(300); } resetEvents[index].Set(); } // no config file. public static bool SetUseUnsafeHeaderParsing(bool b) { Assembly a = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection)); if (a == null) return false; Type t = a.GetType("System.Net.Configuration.SettingsSectionInternal"); if (t == null) return false; object o = t.InvokeMember("Section", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); if (o == null) return false; FieldInfo f = t.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); if (f == null) return false; f.SetValue(o, b); return true; } } }