-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
257 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Nethermind.JsonRpc\Nethermind.JsonRpc.csproj" /> | ||
<ProjectReference Include="..\Nethermind.Sockets\Nethermind.Sockets.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
using System.Net.Sockets; | ||
using System.Net; | ||
using Nethermind.Sockets; | ||
using Nethermind.JsonRpc.WebSockets; | ||
using Nethermind.JsonRpc; | ||
using Nethermind.JsonRpc.Modules; | ||
using Nethermind.Serialization.Json; | ||
using Nethermind.Evm.Tracing.GethStyle; | ||
using System.Diagnostics; | ||
|
||
static async Task<int> CountNumberOfMessages(Socket socket, CancellationToken token) | ||
{ | ||
using IpcSocketMessageStream stream = new(socket); | ||
|
||
|
||
|
||
int messages = 0; | ||
try | ||
{ | ||
byte[] buffer = new byte[10]; | ||
//int i = 0; | ||
while (true) | ||
{ | ||
ReceiveResult? result = await stream.ReceiveAsync(buffer).ConfigureAwait(false); | ||
|
||
if (Stopwatch.GetTimestamp() % 1001 == 0) | ||
{ | ||
await Task.Delay(1, token); | ||
} | ||
|
||
|
||
if (result is not null && IsEndOfIpcMessage(result)) | ||
{ | ||
//var data = buffer.Take(result.Read).ToArray(); | ||
//var str = System.Text.Encoding.UTF8.GetString(data); | ||
|
||
//Console.WriteLine(i++); | ||
|
||
//Console.WriteLine($"{Convert.ToHexString(data)}"); | ||
//Console.WriteLine($"{str}: {data.Length}"); | ||
|
||
//if (data.Length != 0) | ||
messages++; | ||
} | ||
|
||
if (result is null || result.Closed) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
catch (OperationCanceledException) { } | ||
|
||
return messages; | ||
} | ||
|
||
var messageCount = 50; | ||
|
||
CancellationTokenSource cts = new(); | ||
IPEndPoint ipEndPoint = IPEndPoint.Parse("127.0.0.1:1337"); | ||
|
||
Task<int> receiveMessages = OneShotServer( | ||
ipEndPoint, | ||
async socket => await CountNumberOfMessages(socket, cts.Token) | ||
); | ||
|
||
Task<int> sendMessages = Task.Run(async () => | ||
{ | ||
using Socket socket = new(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | ||
await socket.ConnectAsync(ipEndPoint).ConfigureAwait(false); | ||
|
||
using IpcSocketMessageStream stream = new(socket); | ||
using JsonRpcSocketsClient<IpcSocketMessageStream> client = new( | ||
clientName: "TestClient", | ||
stream: stream, | ||
endpointType: RpcEndpoint.Ws, | ||
jsonRpcProcessor: null!, | ||
jsonRpcLocalStats: new NullJsonRpcLocalStats(), | ||
jsonSerializer: new EthereumJsonSerializer() | ||
); | ||
int disposeCount = 0; | ||
|
||
for (int i = 0; i < messageCount; i++) | ||
{ | ||
using JsonRpcResult result = JsonRpcResult.Single(RandomSuccessResponse(1000, () => disposeCount++), default); | ||
await client.SendJsonRpcResult(result).ConfigureAwait(false); | ||
await Task.Delay(1).ConfigureAwait(false); | ||
} | ||
|
||
//disposeCount.Should().Be(messageCount); | ||
await cts.CancelAsync().ConfigureAwait(false); | ||
|
||
return messageCount; | ||
}); | ||
|
||
await Task.WhenAll(sendMessages, receiveMessages).ConfigureAwait(false); | ||
int sent = sendMessages.Result; | ||
int received = receiveMessages.Result; | ||
|
||
Console.WriteLine($"Sent: {sent}, Received: {received}"); | ||
|
||
static async Task<T> OneShotServer<T>(IPEndPoint ipEndPoint, Func<Socket, Task<T>> func) | ||
{ | ||
using Socket socket = new(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | ||
socket.Bind(ipEndPoint); | ||
socket.Listen(); | ||
|
||
Socket handler = await socket.AcceptAsync(); | ||
|
||
return await func(handler); | ||
} | ||
|
||
static JsonRpcSuccessResponse RandomSuccessResponse(int size, Action? disposeAction = null) | ||
{ | ||
return new JsonRpcSuccessResponse(disposeAction) | ||
{ | ||
MethodName = "mock", | ||
Id = "42", | ||
Result = RandomObject(size) | ||
}; | ||
} | ||
|
||
|
||
|
||
static object RandomObject(int size) | ||
{ | ||
string[] strings = RandomStringArray(size / 2); | ||
object obj = new GethLikeTxTrace() | ||
{ | ||
Entries = | ||
{ | ||
new GethTxTraceEntry | ||
{ | ||
Stack = strings, Memory = strings, | ||
} | ||
} | ||
}; | ||
return obj; | ||
} | ||
|
||
static string[] RandomStringArray(int length, bool runGc = true) | ||
{ | ||
string[] array = new string[length]; | ||
for (int i = 0; i < length; i++) | ||
{ | ||
array[i] = RandomString(length); | ||
if (runGc && i % 100 == 0) | ||
{ | ||
GC.Collect(); | ||
} | ||
} | ||
return array; | ||
} | ||
|
||
static string RandomString(int length) | ||
{ | ||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
char[] stringChars = new char[length]; | ||
Random random = new(); | ||
|
||
for (int i = 0; i < stringChars.Length; i++) | ||
{ | ||
stringChars[i] = chars[random.Next(chars.Length)]; | ||
} | ||
return new string(stringChars); | ||
} | ||
static bool IsEndOfIpcMessage(ReceiveResult result) => result.EndOfMessage && (!result.Closed || result.Read != 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"profiles": { | ||
"ConsoleApp1": { | ||
"commandName": "Project" | ||
}, | ||
"WSL": { | ||
"commandName": "WSL2", | ||
"distributionName": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters