压力测试后的收获

最近开发的站点进行压力测试,服务器CPU占用率居高不下,发现了一些影响站点效率的代码问题:

1  太多的string + string操作,最后通通改为了StringBulider。其实这是自己的编程习惯不好,明明知道StringBuilder的效率高,写代码时图省事,就直接用了+操作,量变引起质变。

2  正则表达式会对速度的影响,多余的正则表达式要删除,原来是有一段UBBCode需要转换,于是调用了共通的转换类,这个类中定义了N多正则转换,但我用到的就是那么几个,不需要的在程序运行时也执行了。

3  数据进行了缓存,但页面没有进行部分缓存,当时是考虑页面上有些每次都要更新的东西,于是就没对页面进行缓存处理,片面的认为数据缓存已经够了。现实是页面的部分缓存还是要做的。实现页面部分缓存有两种方式:一种子是将页面中需要缓存的部分置于用户控件中,并对用户控件设置缓存功能,是通常所说的“控件缓存”。另外一种是“缓存后 替换”的方法,该项方法与用户控件缓存正好相反,将页面中的某一部分设置为不缓存,虽然缓存了整个页面,但是当再次请求该项页面时,将重新处理那些没有设 置为缓存的内容。这些都有成熟的方法了。但公司的同事教了我另一个方法:
主要是利用了System.IO.StringWriter和RenderControl,将页面的显示用div进行了缓存,其中的div都是runat="server".

    string content1 = "";
            string content2 = "";
            string content3 = "";
            string content4 = "";
            string content5 = "";
            string content6 = "";

            //Cache.Remove(_pId.ToString() + "_item1");
            //Cache.Remove(_pId.ToString() + "_item2");
            //Cache.Remove(_pId.ToString() + "_item3");
            //Cache.Remove(_pId.ToString() + "_item4");
            //Cache.Remove(_pId.ToString() + "_item5");
            //Cache.Remove(_pId.ToString() + "_item6");
            
            
            content1 = Cache[_pId.ToString() + "_item1"] as string;
            content2 = Cache[_pId.ToString() + "_item2"] as string;
            content3 = Cache[_pId.ToString() + "_item3"] as string;
            content4 = Cache[_pId.ToString() + "_item4"] as string;
            content5 = Cache[_pId.ToString() + "_item5"] as string;
            content6 = Cache[_pId.ToString() + "_item6"] as string;


            if (!string.IsNullOrEmpty(content1) && _sub == 0)
            {
                divItemList1.InnerHtml = content1;
                divItemList2.InnerHtml = content2;
                divItemList3.InnerHtml = content3;
                divItemList4.InnerHtml = content4;
                divItemList5.InnerHtml = content5;
                divItemList6.InnerHtml = content6;
            }
            else
            {
                rptItemList1.DataSource = disInfo.DisList[0];
                rptItemList1.DataBind();

                rptItemList2.DataSource = disInfo.DisList[1];
                rptItemList2.DataBind();

                rptItemList3.DataSource = disInfo.DisList[2];
                rptItemList3.DataBind();

                rptItemList4.DataSource = disInfo.DisList[3];
                rptItemList4.DataBind();

                rptItemList5.DataSource = disInfo.DisList[4];
                rptItemList5.DataBind();

                rptItemList6.DataSource = disInfo.DisList[5];
                rptItemList6.DataBind();

                if (_sub == 0)
                {
                    System.IO.StringWriter wr = new System.IO.StringWriter();
                    rptItemList1.RenderControl(new HtmlTextWriter(wr));
                    content1 = wr.ToString();
                    Cache.Add(_pId.ToString() + "_item1", content1, null, DateTime.MaxValue, TimeSpan.FromSeconds(3600), CacheItemPriority.High, null);
                    divItemList1.InnerHtml = content1;

                    wr = new System.IO.StringWriter();
                    rptItemList2.RenderControl(new HtmlTextWriter(wr));
                    content2 = wr.ToString();
                    Cache.Add(_pId.ToString() + "_item2", content2, null, DateTime.MaxValue, TimeSpan.FromSeconds(3600), CacheItemPriority.High, null);
                    divItemList2.InnerHtml = content2;

                    wr = new System.IO.StringWriter();
                    rptItemList3.RenderControl(new HtmlTextWriter(wr));
                    content3 = wr.ToString();
                    Cache.Add(_pId.ToString() + "_item3", content3, null, DateTime.MaxValue, TimeSpan.FromSeconds(3600), CacheItemPriority.High, null);
                    divItemList3.InnerHtml = content3;

                    wr = new System.IO.StringWriter();
                    rptItemList4.RenderControl(new HtmlTextWriter(wr));
                    content4 = wr.ToString();
                    Cache.Add(_pId.ToString() + "_item4", content4, null, DateTime.MaxValue, TimeSpan.FromSeconds(3600), CacheItemPriority.High, null);
                    divItemList4.InnerHtml = content4;

                    wr = new System.IO.StringWriter();
                    rptItemList5.RenderControl(new HtmlTextWriter(wr));
                    content5 = wr.ToString();
                    Cache.Add(_pId.ToString() + "_item5", content5, null, DateTime.MaxValue, TimeSpan.FromSeconds(3600), CacheItemPriority.High, null);
                    divItemList5.InnerHtml = content5;

                    wr = new System.IO.StringWriter();
                    rptItemList6.RenderControl(new HtmlTextWriter(wr));
                    content6 = wr.ToString();
                    Cache.Add(_pId.ToString() + "_item6", content6, null, DateTime.MaxValue, TimeSpan.FromSeconds(3600), CacheItemPriority.High, null);
                    divItemList6.InnerHtml = content6;
                }

            }

posted @ 2012-06-10 23:51  永动机  阅读(249)  评论(0)    收藏  举报