gpt4 book ai didi

c# - 为什么在20%繁忙的计算机上出现IPC延迟

转载 作者:行者123 更新时间:2023-12-03 11:58:09 24 4
gpt4 key购买 nike

我在具有10个内核并且正在运行我的ClientApp的47个实例且都与MasterApp通信的一台机器上的IPC上遇到了延迟。

我有时会出现严重的延迟。这是我的日志的一部分。左边的DateTime是日志DateTime(高性能记录器)。 []中的DateTimes是从MasterApp发送消息的时间。每个消息都以@结尾。

因此,第一个消息仅落后1毫秒,而最后一条消息落后71毫秒。

任何可能导致这种情况的想法,以及我为摆脱延迟而可能采取的措施

20141030T120401.015 [--------*MD|USD/JPY 109.032 109.034 1000000.00 1000000.00 20141030T120401014@]
20141030T120401.084 [--------*MD|EUR/CHF 1.20580 1.20588 3000000.00 2000000.00 20141030T120401019@]
20141030T120401.163 [--------*MD|USD/JPY 109.031 109.034 1000000.00 1000000.00 20141030T120401088@*MD|EUR/CHF 1.20580 1.20588 3000000.00 1000000.00 20141030T120401092@]

代码摘录:
    public void Connect(int port)
{

IPAddress[] aryLocalAddr = null;
String strHostName = "";
try
{
// NOTE: DNS lookups are nice and all but quite time consuming.
strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
aryLocalAddr = ipEntry.AddressList;
}
catch (Exception ex)
{
OutputWriteLine("Error trying to get local address: " + ex.Message);
}


socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Blocking = false;

IPEndPoint epServer = new IPEndPoint(aryLocalAddr[0], port);

socket.BeginConnect(epServer, new AsyncCallback(ConnectCallBack), socket);

}

public void ConnectCallBack(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
NewConnection(socket);
}


public void NewConnection(Socket socket)
{
Connection mc = new Connection(socket);
connections.Add(mc);
//OutputWriteLine("Client " + mc.SessionID() + " joined");

DateTime now = DateTime.Now;
String intraMessage = "*IDENT|" + modelInitiatorApp.G.SLOTNAME;

modelInitiatorApp.SetConnected();

SendMessage(mc, intraMessage);

socket.BeginReceive(mc.stateObject.buffer, 0, mc.stateObject.buffer.Length, SocketFlags.None, new AsyncCallback(ReceivedCallBack), mc.stateObject);
}

public void ReceivedCallBack(IAsyncResult ar)
{
//
StateObject state = (StateObject)ar.AsyncState;
Socket socket = state.socket;

try
{
int bytesRead = socket.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
OutputWriteLine("[--------"+state.sb.ToString()+"]");

string[] contents = state.sb.ToString().Split('@');
int delimCount = state.sb.ToString().Count(x => x == '@');

for (int d = 0; d < delimCount; d++)
{
if (contents[d] != "")
OnMessage(state, contents[d]);
}

if (!state.sb.ToString().EndsWith("@"))
{
state.sb.Clear();
state.sb.Append(contents[contents.Count() - 1]);

}
else
{
state.sb.Clear();

}

socket.BeginReceive(state.buffer, 0, state.buffer.Length, SocketFlags.None, new AsyncCallback(ReceivedCallBack), state);
}
else
{
// If no data was received then the connection is probably dead
OutputWriteLine("Client " + state.SessionID() + " disconnected");
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Unusual error during Receive!");
}
}

最佳答案

好的,我认为这里的TCP很好,但我仍然相信在这种情况下,命名管道和本地套接字之间的性能将相似(不过,我很希望看到基准测试)。我注意到的一件事是,您的ReceivedCallback会调用EndReceive,然后在再次调用BeginReceive之前进行大量工作。这意味着您仍然可以在套接字上接收数据(很可能是因为localhost上没有延迟),但是您实际上并没有处理它。您应该考虑使用socket.BeginReceive()是EndReceive()之后的第一个调用(并检查错误,连接关闭等),以便在处理数据时不会堆积任何东西。显然,您需要先复制缓冲区或使用缓冲池(我个人会使用缓冲池),以便您不会破坏数据,但不会挂起BeginReceive()调用并坐在那里处理数据会增加一些人为延迟,因为您会很快获得数据,但您只是忽略了它。我已经看到过这种事情发生在Web对象之前,其中通过回调处理非常大的对象的反序列化,因此如果是这种情况,我不会感到惊讶。我想说这是下一步,您应该尝试看看这是否会改变延迟。我完全有可能不正确,但我认为按照我的建议重构您的代码值得您花时间查看它是否可以解决此问题。

关于c# - 为什么在20%繁忙的计算机上出现IPC延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26653624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com