首页 新闻 会员 周边

WCF主机服务 承载REST 服务效率高 大概有10倍

0
悬赏园豆:100 [已解决问题] 解决于 2013-11-29 11:48

我是在本地测试  测试代码是做了一个根据用户登陆查询的功能。

使用我自己写的一个hosting承载了一个服务 地址:http://127.0.0.1:83/Client/Login

 
代码如下
 private void ThreadStart()
        {
            webServiceHost = new WebServiceHost(typeof(ClientService), new Uri("http://127.0.0.1:83/Client"));
            webServiceHost.AddServiceEndpoint(typeof(IClientService), new WebHttpBinding(), "");
            webServiceHost.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior { HelpEnabled = true });

            webServiceHost.Opened += delegate
            {
                ServiceEndpoint endpoinit = webServiceHost.Description.Endpoints[0];
                MessageBox.Show(string.Format("服务开启!\n服务地址:{0}", endpoinit.Address));
            };
            webServiceHost.Open();
        }
 

又使用WCF主机服务承载了一个相同的服务 地址:http://127.0.0.1:82/Client/Login

 

写了一个控制台应用程序用来测试

        public static void DoWork()
        {
            using (WebClient webClient = new WebClient())
            {
                while (Run)
                {
                    try
                    {
                        DateTime olderTime = DateTime.Now;
                        webClient.Headers.Add("Content-Type", "application/json");
                        webClient.Encoding = Encoding.UTF8;
                        string PostData = "{\"Password\":\"" + rd.Next(99999999) + "\",\"UserName\":\"" + rd.Next(99999999) + "\",\"isRember\":true}";
                        string result = webClient.UploadString("http://127.0.0.1:82/Client/Login", PostData);
                        totalNum++;
                        DateTime newTime = DateTime.Now;
                        TimeSpan ts = newTime - olderTime;
                        totalMilliseconds += ts.TotalMilliseconds;
                        Console.WriteLine(string.Format("线程{0}耗时{1} 平均耗时{2} 次数:{3}  结果:", Thread.CurrentThread.GetHashCode(), ts.TotalMilliseconds, totalMilliseconds / totalNum, totalNum));
                        Console.WriteLine(result);

                    }
                    catch
                    {
                    }
                }
            }
            

        }

 

我自己的测试图

 

WCF主机服务的测试图

 

可以看到效率差了 5倍左右  有的时候能差10倍。

用的是多线程处理,其中Message消息中是数据库查询耗时,线程耗时是总耗时。

 

想知道这是为什么?

Songlw的主页 Songlw | 初学一级 | 园豆:110
提问于:2013-11-12 23:56
< >
分享
最佳答案
0

原因是在调试模式下。。。。

Songlw | 初学一级 |园豆:110 | 2013-11-28 23:49
其他回答(2)
0

每个主机服务器处理的数据本来就是不同的,就像单页面php应该比java快一样

收获园豆:50
angelshelter | 园豆:9887 (大侠五级) | 2013-11-13 17:57

原因找到了,是因为我是在调试模式下运行的服务,直接打开生成的软件速度就上来了。

支持(0) 反对(0) Songlw | 园豆:110 (初学一级) | 2013-11-13 20:10

@Songlw:  原来如此。呵呵=

支持(0) 反对(0) angelshelter | 园豆:9887 (大侠五级) | 2013-11-13 21:15

@angelshelter: 很奇特。明显看到查询数据库速度也慢了。服务是通过dll调用的,不知道这个原理是什么

支持(0) 反对(0) Songlw | 园豆:110 (初学一级) | 2013-11-13 21:18
0

用Thrift

 

用二进制传办输

收获园豆:50
黑侠客 | 园豆:158 (初学一级) | 2013-11-13 21:29

因为要跟网页通信所以要用这种服务

支持(0) 反对(0) Songlw | 园豆:110 (初学一级) | 2013-11-13 21:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册