网上都说Ipc通道的速度比Tcp、http通道快,也有相关的测试文章。但我在多线程测试中发现Ipc通道比Tcp慢了差不多20多倍,测试方法是在IIS6中创建Remoting通道,用WAS压力测试工具进行测试。IIS6中Web园设置为100个进程,核心请求队列设置为4000,然后打开WAS并设置1000个线程,每个线程2个连接。点击启动。
以下是WAS运行30s后的测试结果,WAS显示时间不准确是由于截图时间没有把握好
IPC通道
TCP通道:
对比可见,IPC通道在多线程状态下比TCP通道慢的太多了。
Tcp服务器端代码:
Code
private void Form1_Load(object sender, EventArgs e)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["port"] = 8086;
TcpChannel channel = new TcpChannel(props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
//SayHello sayHello = new SayHello();
sayHello = new SayHello();
RemotingServices.Marshal(sayHello, "SayHello");
sayHello.ConnectedEvent += new DataDelegate.Connected(sayHello_ConnectedEvent);
sayHello.DisConnectedEvent += new DataDelegate.DisConnected(sayHello_DisConnectedEvent);
}
private void Form1_Load(object sender, EventArgs e)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["port"] = 8086;
TcpChannel channel = new TcpChannel(props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
//SayHello sayHello = new SayHello();
sayHello = new SayHello();
RemotingServices.Marshal(sayHello, "SayHello");
sayHello.ConnectedEvent += new DataDelegate.Connected(sayHello_ConnectedEvent);
sayHello.DisConnectedEvent += new DataDelegate.DisConnected(sayHello_DisConnectedEvent);
}
IPC服务器端代码
Code
private void Form1_Load(object sender, EventArgs e)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["portName"] = "Chater";
props["name"] = "ipc";
props["authorizedGroup"] = "Everyone";
IpcChannel channel = new IpcChannel(props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
sayHello = new SayHello();
RemotingServices.Marshal(sayHello, "SayHello");
sayHello.ConnectedEvent += new DataDelegate.Connected(sayHello_ConnectedEvent);
sayHello.DisConnectedEvent += new DataDelegate.DisConnected(sayHello_DisConnectedEvent);
}
private void Form1_Load(object sender, EventArgs e)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["portName"] = "Chater";
props["name"] = "ipc";
props["authorizedGroup"] = "Everyone";
IpcChannel channel = new IpcChannel(props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
sayHello = new SayHello();
RemotingServices.Marshal(sayHello, "SayHello");
sayHello.ConnectedEvent += new DataDelegate.Connected(sayHello_ConnectedEvent);
sayHello.DisConnectedEvent += new DataDelegate.DisConnected(sayHello_DisConnectedEvent);
}
远程代理类部分代码,
Code
//这个数组是为了降低多线程竞争资源
private Hashtable[] reDict = new Hashtable[MaxHashTables];
public void AddEventReappear(string clientId, SayEventReappear re)
{
Random x = new Random();
int i = x.Next(MaxHashTables);
lock (reDict[i].SyncRoot)
{
this.reDict[i][clientId] = re;
}
}
//这个数组是为了降低多线程竞争资源
private Hashtable[] reDict = new Hashtable[MaxHashTables];
public void AddEventReappear(string clientId, SayEventReappear re)
{
Random x = new Random();
int i = x.Next(MaxHashTables);
lock (reDict[i].SyncRoot)
{
this.reDict[i][clientId] = re;
}
}
TCP客户端代码,运行与IIS中
Code
protected void Page_Load(object sender, EventArgs e)
{
string testL=new string (' ',36*8);
SayHello sh;
TcpChannel channel = null;
string Identity=Request.QueryString["name"]??new Random().Next(10000000).ToString();
if (ChannelServices.RegisteredChannels.Length == 0)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["port"] = 0;
// props["exclusiveAddressUse"] = "false";
//props["authorizedGroup"] = "Everyone";
channel = new TcpChannel(props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
}
sh = (SayHello)Activator.GetObject(typeof(SayHello), "tcp://localhost:8086/SayHello");
SayEventReappear re = new SayEventReappear();
re.OnSay += new SayHandler(re_OnSay);
sh.AddEventReappear(Identity, re);
Response.Write(testL);
System.Threading.Thread.Sleep(120000);
re.OnSay -= new SayHandler(re_OnSay);
sh.SubEventReappear(Identity);
}
protected void Page_Load(object sender, EventArgs e)
{
string testL=new string (' ',36*8);
SayHello sh;
TcpChannel channel = null;
string Identity=Request.QueryString["name"]??new Random().Next(10000000).ToString();
if (ChannelServices.RegisteredChannels.Length == 0)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["port"] = 0;
// props["exclusiveAddressUse"] = "false";
//props["authorizedGroup"] = "Everyone";
channel = new TcpChannel(props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
}
sh = (SayHello)Activator.GetObject(typeof(SayHello), "tcp://localhost:8086/SayHello");
SayEventReappear re = new SayEventReappear();
re.OnSay += new SayHandler(re_OnSay);
sh.AddEventReappear(Identity, re);
Response.Write(testL);
System.Threading.Thread.Sleep(120000);
re.OnSay -= new SayHandler(re_OnSay);
sh.SubEventReappear(Identity);
}
IPC客户端代码,运行与IIS中
IpcChannel的
Code
protected void Page_Load(object sender, EventArgs e)
{
string testL=new string (' ',36*8);
SayHello sh;
IpcChannel channel = null;
string Identity=Request.QueryString["name"]??new Random().Next(10000000).ToString();
if (ChannelServices.RegisteredChannels.Length == 0)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["portName"] = "ChatClient";
props["authorizedGroup"] = "Everyone";
channel = new IpcChannel (props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
}
sh = (SayHello)Activator.GetObject(typeof(SayHello), "ipc://Chater/SayHello");
SayEventReappear re = new SayEventReappear();
re.OnSay += new SayHandler(re_OnSay);
sh.AddEventReappear(Identity, re);
Response.Write(testL);
System.Threading.Thread.Sleep(120000);
re.OnSay -= new SayHandler(re_OnSay);
sh.SubEventReappear(Identity);
}
protected void Page_Load(object sender, EventArgs e)
{
string testL=new string (' ',36*8);
SayHello sh;
IpcChannel channel = null;
string Identity=Request.QueryString["name"]??new Random().Next(10000000).ToString();
if (ChannelServices.RegisteredChannels.Length == 0)
{
BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
sfsp.TypeFilterLevel = TypeFilterLevel.Full;
Hashtable props = new Hashtable();
props["portName"] = "ChatClient";
props["authorizedGroup"] = "Everyone";
channel = new IpcChannel (props, null, sfsp);
ChannelServices.RegisterChannel(channel, false);
}
sh = (SayHello)Activator.GetObject(typeof(SayHello), "ipc://Chater/SayHello");
SayEventReappear re = new SayEventReappear();
re.OnSay += new SayHandler(re_OnSay);
sh.AddEventReappear(Identity, re);
Response.Write(testL);
System.Threading.Thread.Sleep(120000);
re.OnSay -= new SayHandler(re_OnSay);
sh.SubEventReappear(Identity);
}