1

我正在测试IWebDrivervs的并行执行WebClient。(如果存在性能差异以及差异有多大)

在我设法这样做之前,我遇到了简单的 WebClient-Parallel 调用问题。

好像还没有执行,我确实AgilityPacDocExtraction 在特定行上放了一个刹车点WebClient.DownloadString(URL)

但是程序退出而不是调试Step Into可能会显示 yeald 字符串。

计划是为需要采取的所有操作提供单一方法,通过每个操作的“模式”选择器,然后使用一个简单foreach的方法来迭代所有可用Enum values- 模式

主要执行:

   static void Main(string[] args)
   {
        EnumForEach<Action>(Execute);
        Task.WaitAll();
   }
   public static void EnumForEach<Mode>(Action<Mode> Exec)
   {

            foreach (Mode mode in Enum.GetValues(typeof(Mode)))
            {
                Mode Curr = mode;

                Task.Factory.StartNew(() => Exec(Curr) );
            }

   }

模式/动作选择器

    enum Action
    {
        Act1, Act2
    }

实际执行

    static  BrowsresFactory.IeEngine IeNgn = new BrowsresFactory.IeEngin();
    static string 
        FlNm = Environment.CurrentDirectory,
        URL = "",
        TmpHtm ="";


   static void Execute(Action Exc)
   {


        switch (Exc)
        {
            case Action.Act1:
                break;

            case Action.Act2:
                URL  = "UrlofUrChoise here...";
                FlNm += "\\TempHtm.htm";
                TmpHtm = IeNgn.AgilityPacDocExtraction(URL).GetElementbyId("Dv_Main").InnerHtml;
                File.WriteAllText(FlNm, TmpHtm);
                break;

        }
     }

持有WebClientIWebDriver(由硒)不包括在这里的类,所以它不会在这篇文章中占用更多空间,而且现在也不相关。

class BrowsresFactory
{
    public class IeEngine
{

    private WebClient WC = new WebClient();
    private string tmpExtractedPageValue = "";
    private HtmlAgilityPack.HtmlDocument retAglPacHtmDoc = new HtmlAgilityPack.HtmlDocument();

    public HtmlAgilityPack.HtmlDocument AgilityPacDocExtraction(string URL)
    {
                WC.Encoding = Encoding.GetEncoding("UTF-8");
                tmpExtractedPageValue = WC.DownloadString(URL); //<--- tried to break here
                retAglPacHtmDoc.LoadHtml(tmpExtractedPageValue);
                return retAglPacHtmDoc;
    }
}
}

问题是我看不到文件中应该通过从 WebClient 提取的值来更改的任何内容,而且在调试模式下我无法进入上面代码中注释的行。我在这里做错了什么?

4

2 回答 2

1

上述代码中没有定义该函数Download(url, htmlDictionary),一个可能的版本是:

private static void Download(string url, ConcurrentDictionary<string, string> htmlDictionary)
{
    using (var webClient = new SmartWebClient())
    {
        htmlDictionary.TryAdd(url, webClient.DownloadString(url));
    }
}

... 上面的代码似乎是另一个 Stack Overflow 帖子的副本。有关参考,请参阅 Retrieve a string contains html Document source using Task parallel

于 2014-07-04T01:28:12.553 回答
0

我已经设法解决了这个问题WebClient,我认为使用它需要更少的资源WebDriver,如果这是真的,这也意味着需要更少的时间。

这是代码:

public void StartEngins()
{
    const string URL_Dollar = "URL_Dollar";
    const string URL_UpdateUsersTimeOut = "URL_UpdateUsersTimeOut";


    var urlList = new Dictionary<string, string>();
    urlList.Add(URL_Dollar, "http://bing.com");
    urlList.Add(URL_UpdateUsersTimeOut, "http://localhost:..../.......aspx");


    var htmlDictionary = new ConcurrentDictionary<string, string>();
    Parallel.ForEach(
                    urlList.Values,
                    new ParallelOptions { MaxDegreeOfParallelism = 20 },
                    url => Download(url, htmlDictionary)
                    );
    foreach (var pair in htmlDictionary)
    {
        ///Process(pair);
        MessageBox.Show(pair.Value);
    }
}

public class SmartWebClient : WebClient
{
    private readonly int maxConcurentConnectionCount;

    public SmartWebClient(int maxConcurentConnectionCount = 20)
    {

        this.maxConcurentConnectionCount = maxConcurentConnectionCount;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var httpWebRequest = (HttpWebRequest)base.GetWebRequest(address);
        if (httpWebRequest == null)
        {
            return null;
        }

        if (maxConcurentConnectionCount != 0)
        {
            httpWebRequest.ServicePoint.ConnectionLimit = maxConcurentConnectionCount;
        }

        return httpWebRequest;
    }

}
于 2012-11-24T02:10:17.683 回答