Skip to content

Commit 581c44d

Browse files
committed
few more tests
1 parent 5f5e484 commit 581c44d

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

src/net-questdb-client/LineTcpSender.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,22 @@ namespace QuestDB
88
public class LineTcpSender : IDisposable
99
{
1010
private static readonly long EpochTicks = new DateTime(1970, 1, 1).Ticks;
11-
private readonly TcpClient _client;
12-
private readonly bool _closeClient;
11+
private readonly Socket _clientSocket;
1312
private readonly byte[] _sendBuffer;
1413
private int _position;
1514
private bool _hasMetric;
1615
private bool _quoted;
1716
private bool _noFields = true;
18-
19-
public LineTcpSender(TcpClient client, int bufferSize = 4096)
17+
18+
public LineTcpSender(String address, int port, int bufferSize = 4096)
2019
{
21-
_client = client;
20+
_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
21+
_clientSocket.NoDelay = true;
22+
_clientSocket.Blocking = true;
23+
_clientSocket.Connect(address, port);
2224
_sendBuffer = new byte[bufferSize];
2325
}
2426

25-
public LineTcpSender(String address, int port, int bufferSize = 4096) : this(new TcpClient(address, port), bufferSize)
26-
{
27-
_closeClient = true;
28-
}
29-
3027
public LineTcpSender Metric(ReadOnlySpan<char> name)
3128
{
3229
if (_hasMetric)
@@ -141,9 +138,9 @@ private void PutUtf8(char c)
141138
Flush();
142139
}
143140

144-
Span<byte> bytes = _sendBuffer;
141+
Span<byte> bytes = _sendBuffer.AsSpan(_position);
145142
Span<char> chars = stackalloc char[1] {c};
146-
_position += Encoding.UTF8.GetBytes(chars, bytes.Slice(_position, 4));
143+
_position += Encoding.UTF8.GetBytes(chars, bytes);
147144
}
148145

149146
private void PutSpecial(char c)
@@ -202,7 +199,8 @@ private LineTcpSender Put(char c)
202199

203200
public void Flush()
204201
{
205-
_position -= _client.Client.Send(_sendBuffer, 0, _position, SocketFlags.None);
202+
int sent = _clientSocket.Send(_sendBuffer, 0, _position, SocketFlags.None);
203+
_position -= sent;
206204
}
207205

208206
public void Dispose()
@@ -220,10 +218,7 @@ public void Dispose()
220218
}
221219
finally
222220
{
223-
if (_closeClient)
224-
{
225-
_client.Dispose();
226-
}
221+
_clientSocket.Dispose();
227222
}
228223
}
229224

@@ -237,7 +232,7 @@ public void AtNow()
237232
public void At(DateTime timestamp)
238233
{
239234
long epoch = timestamp.Ticks - EpochTicks;
240-
Put(' ').Put(epoch).Put("00").AtNow();
235+
Put(' ').Put(epoch).Put('0').Put('0').AtNow();
241236
}
242237
}
243238
}

src/tcp-client-test/LineTcpSenderTests.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Net;
34
using System.Net.Sockets;
45
using System.Text;
@@ -40,8 +41,8 @@ public void SendLineExceedsBuffer()
4041
srv.AcceptAsync();
4142

4243
using var ls = new LineTcpSender(IPAddress.Loopback.ToString(), _port, 25);
43-
var lineCount = 50;
44-
var expected = "metric\\ name,t\\ a\\ g=v\\ alu\\,\\ e number=10i,db\\ l=123.12,string=\" -=\\\"\",привед=\"медвед\" 1000000000\n";
44+
var lineCount = 500;
45+
var expected = "metric\\ name,t\\ a\\ g=v\\ alu\\,\\ e number=10i,db\\ l=123.12,string=\" -=\\\"\",при\\ вед=\"медвед\" 1000000000\n";
4546
var totalExpectedSb = new StringBuilder();
4647
for (int i = 0; i < lineCount; i++)
4748
{
@@ -50,7 +51,7 @@ public void SendLineExceedsBuffer()
5051
.Field("number", 10)
5152
.Field("db l", 123.12)
5253
.Field("string", " -=\"")
53-
.Field("привед", "медвед")
54+
.Field("при вед", "медвед")
5455
.At(new DateTime(1970, 01, 01, 0, 0, 1));
5556
totalExpectedSb.Append(expected);
5657
}
@@ -78,6 +79,31 @@ public void SendNegativeLongAndDouble()
7879
var expected = "neg\\\\name number1=-9223372036854775807i,number2=9223372036854775807i,number3=-1.7976931348623157E+308,number4=1.7976931348623157E+308\n";
7980
WaitAssert(srv, expected);
8081
}
82+
83+
[Test]
84+
public void SendMillionToFile()
85+
{
86+
using var srv = CreateTcpListener(_port);
87+
srv.AcceptAsync();
88+
89+
var nowMillisecond = DateTime.Now.Millisecond;
90+
var metric = "metric_name" + nowMillisecond;
91+
92+
using var ls = new LineTcpSender(IPAddress.Loopback.ToString(), _port, 2048);
93+
for(int i = 0; i < 1E6; i++)
94+
{
95+
ls.Metric(metric)
96+
.Tag("nopoint", "tag" + i%100 )
97+
.Field("counter", i * 1111.1)
98+
.Field("int", i)
99+
.Field("привед", "мед вед")
100+
.At(new DateTime(2021, 1, 1, (i/360/1000) % 60, (i/60/1000) % 60, (i / 1000) % 60, i % 1000));
101+
}
102+
ls.Flush();
103+
104+
File.WriteAllText($"out-{nowMillisecond}.txt", srv.GetTextReceived());
105+
}
106+
81107

82108
private static void WaitAssert(DummyIlpServer srv, string expected)
83109
{
@@ -177,8 +203,8 @@ private class DummyIlpServer : IDisposable
177203
private readonly TcpListener _server;
178204
private readonly byte[] _buffer = new byte[2048];
179205
private readonly CancellationTokenSource _cancellationTokenSource = new();
180-
private readonly StringBuilder _request = new();
181-
private volatile int _totalReceived = 0;
206+
private volatile int _totalReceived;
207+
private readonly MemoryStream _received = new();
182208

183209
public DummyIlpServer(int port)
184210
{
@@ -211,9 +237,8 @@ private async Task SaveData(Socket connection)
211237
int received = await connection.ReceiveAsync(_buffer, SocketFlags.None, _cancellationTokenSource.Token);
212238
if (received > 0)
213239
{
214-
var value = Encoding.UTF8.GetString(_buffer, 0, received);
215-
_request.Append(value);
216-
_totalReceived += value.Length;
240+
_received.Write(_buffer, 0, received);
241+
_totalReceived += received;
217242
}
218243
}
219244
}
@@ -228,7 +253,7 @@ public void Dispose()
228253

229254
public string GetTextReceived()
230255
{
231-
return _request.ToString();
256+
return Encoding.UTF8.GetString(_received.GetBuffer(), 0, (int)_received.Length);
232257
}
233258
}
234259
}

0 commit comments

Comments
 (0)