コード例 #1
0
ファイル: Form1.cs プロジェクト: chjfth/trivials
        async void StartHttp()
        {
            // NOTE: For an `async void` function, we have to wrap all its code
            // inside try-catch-block, so that exception does NOT leak into unknown world.
            try
            {
                string url = edtURL.Text;
                int    timeout_millisec = int.Parse(edtTimeoutMs.Text);

                AsyncHttp.HeaderDict headers = new AsyncHttp.HeaderDict()
                {
                    {
                        HttpRequestHeader.UserAgent,
                        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; rv:11.0) like Gecko"
                    },
                };

                string postbody = "MyPostText";
                postbody = null;

                AsyncHttp ahttp = new AsyncHttp(url, headers, postbody);

                _cts = new CancellationTokenSource();

                logtid($"HTTP starts. \r\n" +
                       $"  URL:     {url}\r\n" +
                       $"  Timeout: {timeout_millisec} millisec"
                       ); // note: "\n" only does not cause line break

                _tskHttp = ahttp.StartAsText(_cts.Token, timeout_millisec);
                string body = await _tskHttp;

                logtid(
                    $"HTTP success. Body bytes: {ahttp._respbody_bytes.Length}, body chars: {ahttp._respbody_text.Length}");
            }
            catch (Exception e)
            {
                UIPromptException(e);
            }
            finally
            {
                if (_tskHttp != null)
                {
                    _tskHttp.Dispose();
                }

                _tskHttp = null;
            } // try-block end
        }     // async function end
コード例 #2
0
ファイル: Form1.cs プロジェクト: chjfth/trivials
        }     // async function end

        async Task StartStress()
        {
            try
            {
                string url              = edtURL.Text;
                int    cycles           = int.Parse(edtStressCycles.Text);
                int    timeout_millisec = int.Parse(edtTimeoutMs.Text);

                bool is_eager_report = ckbEagerReport.Checked;

                Stopwatch sw = new Stopwatch();
                sw.Restart();
                long msec_prev = 0;
                long msec_now  = 0;

                var dict_bytes2count = new Dictionary <int, int>(); // map http response-body bytes to count

                AsyncHttp.HeaderDict headers = new AsyncHttp.HeaderDict()
                {
                    {
                        HttpRequestHeader.UserAgent,
                        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; rv:11.0) like Gecko"
                    },
                };

                string postbody = "MyPostText";
                postbody = null;

                for (int i = 0; i < cycles; i++)
                {
                    AsyncHttp ahttp = new AsyncHttp(url, headers, postbody);

                    _cts = new CancellationTokenSource();

                    if (is_eager_report)
                    {
                        logtid($"HTTP starts. {url}");
                    }

                    _tskHttp = ahttp.StartAsText(_cts.Token, timeout_millisec);
                    string body = await _tskHttp;

                    int nrbyte = ahttp._respbody_bytes.Length;
                    int nrchar = ahttp._respbody_text.Length;
                    if (dict_bytes2count.ContainsKey(nrbyte))
                    {
                        dict_bytes2count[nrbyte]++;
                    }
                    else
                    {
                        dict_bytes2count[nrbyte] = 1;
                    }

                    if (is_eager_report)
                    {
                        logtid(
                            $"[{i}]HTTP success. Body bytes: {nrbyte}, body chars: {nrchar}");
                    }
                    else // not eager report, report every 1 second
                    {
                        msec_now = sw.ElapsedMilliseconds;
                        if (msec_now - msec_prev >= 1000)
                        {
                            logtid($"{i+1} success");
                            msec_prev = msec_now;
                        }
                    }
                }

                // print summary

                string info = "HTTP response summary:\r\n";
                foreach (int nrbyte in dict_bytes2count.Keys.OrderBy(key => key))
                {
                    string oneline = $"    [{nrbyte} bytes] {dict_bytes2count[nrbyte]} occurrence.";
                    info += oneline + "\r\n";
                }
                logtid(info);

                logtid($"Total milliseconds cost: {sw.ElapsedMilliseconds}");
            }
            catch (Exception e)
            {
                UIPromptException(e);
            }
            finally
            {
//                _tskHttp.Dispose();
                _tskHttp = null;
            } // try-block end
        }