IPC:ソケットと名前付きパイプ

絶対数はあまり意味がありませんが、比較として、情報はある程度の価値があります

条件


更新プログラムが適用されたWindows 7 x64
AMD Athlon X2 4600+(2.41GHz)
DDR2 2GB
.Net Framework 3.5、FCLのクラス。
追加の特権のない2つの64ビットプロセス。
カスペルスキーアンチウイルスはオフになっています。 これを使用すると、ソケットの結果が著しく悪化します。
非同期モードのソケットと名前付きパイプ。
バッファーのサイズは、IPCの各タイプに対して実験的に最適に選択されました。

結果


ソケット
一方向の転送速度-1秒あたり160メガバイト(ウイルス対策で105)
CPU使用率-90%、そのうちコアの約2/3
RAM-プロセスごとに4〜5メガバイト、時間とともに成長しませんでした

名前付きパイプ
一方向の転送速度-755メガバイト/秒(ウイルス対策は影響を受けません)
CPU使用率-80%、そのうちコアの約2/3
RAM-プロセスごとに4〜5メガバイト、時間とともに成長しませんでした

ソースコード


ソケット
using System;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;

namespace Server
{
public static class Program
{
private static byte [] buffer = new byte [512 * 1024];

private static void OnSocketSend(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;

client.EndSend(ar);
client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, OnSocketSend, client);
}

public static void Main( string [] args)
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType. Stream , ProtocolType.Tcp);

server.Bind( new IPEndPoint(IPAddress.Loopback, 5000));
server.Listen(10);

while ( true )
{
Socket client = server.Accept();

client.SendBufferSize = 2 * buffer.Length;
client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, OnSocketSend, client);

Console .WriteLine( "CONNECTED" );
}
}
}
}


* This source code was highlighted with Source Code Highlighter .


using System;
using System.Diagnostics;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;

namespace Client
{
public static class Program
{
private static byte [] buffer = new byte [512 * 1024];
private static Stopwatch watch = new Stopwatch();
private static long traffic = 0;
private static int step = 0;

private static void OnSocketReceive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;

traffic += client.EndReceive(ar);
step++;

if ((step % 1000) == 0)
{
watch.Stop();

Console .WriteLine(
"{0} MB/s" ,
(1000 * (traffic >> 20)) / watch.ElapsedMilliseconds);

watch.Start();
}

client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnSocketReceive, client);
}

public static void Main( string [] args)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType. Stream , ProtocolType.Tcp);

client.ReceiveBufferSize = 2 * buffer.Length;
client.Connect( new IPEndPoint(IPAddress.Loopback, 5000));

watch.Start();

client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnSocketReceive, client);

Console .WriteLine( "CONNECTED" );
Console .ReadLine();
}
}
}


* This source code was highlighted with Source Code Highlighter .

名前付きパイプ

using System;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;

namespace Server
{
public static class Program
{
private static byte [] buffer = new byte [256 * 1024];

private static void OnPipeSend(IAsyncResult ar)
{
NamedPipeServerStream server = (NamedPipeServerStream)ar.AsyncState;

server.EndWrite(ar);
server.BeginWrite(buffer, 0, buffer.Length, OnPipeSend, server);
}

public static void Main( string [] args)
{
NamedPipeServerStream server = new NamedPipeServerStream( "TestPipe" , PipeDirection.InOut, 1, PipeTransmissionMode. Byte , PipeOptions.Asynchronous, 1024 * 1024, 1024 * 1024);

server.WaitForConnection();
server.BeginWrite(buffer, 0, buffer.Length, OnPipeSend, server);

Console .WriteLine( "CONNECTED" );
Console .ReadLine();
}
}
}


* This source code was highlighted with Source Code Highlighter .


using System;
using System.Diagnostics;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;

namespace Client
{
public static class Program
{
private static byte [] buffer = new byte [256 * 1024];
private static Stopwatch watch = new Stopwatch();
private static long traffic = 0;
private static int step = 0;

private static void OnPipeReceive(IAsyncResult ar)
{
NamedPipeClientStream client = (NamedPipeClientStream)ar.AsyncState;

traffic += client.EndRead(ar);
step++;

if ((step % 1000) == 0)
{
watch.Stop();

Console .WriteLine(
"{0} MB/s" ,
(1000 * (traffic >> 20)) / watch.ElapsedMilliseconds);

watch.Start();
}

client.BeginRead(buffer, 0, buffer.Length, OnPipeReceive, client);
}

public static void Main( string [] args)
{
NamedPipeClientStream client = new NamedPipeClientStream( "." , "TestPipe" , PipeDirection.InOut, PipeOptions.Asynchronous);

client.Connect();

watch.Start();

client.BeginRead(buffer, 0, buffer.Length, OnPipeReceive, client);

Console .WriteLine( "CONNECTED" );
Console .ReadLine();
}
}
}


* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/J81067/


All Articles