influxdata / influxdata/influxdb-client-csharp
Solution to write large amount of points
- Dominant language
- C#
- Stars
- 377
- Forks
- 93
- Avg merge
- 9m
- Merged PRs (30d)
- 2
Description
__Proposal:__
Hi, I have a multi-threading program that contain 80,000 field, the data changed ~10ms.
Is there any efficient method to write that amount of data to a local Influx DB?
__Current behavior:__
I have implemented the sample of batching write method that create multi-thread, each thread managed 5000 items and write items to db every 10ms.
```
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
using System.Diagnostics;
internal class Program
{
private const string Host = "http://localhost:8086";
private static string Username = "admin";
private const string Password = "password";
private const string Bucket = "PERFORMANCE";
private const string Organization = "ORG";
private static InfluxDBClient _client;
private static async Task Main(string[] args)
{
_client = CreateClient();
List influxItems = Enumerable.Range(1, 80_000).Select(x => new InfluxItem(x)).ToList();
List influxBatches = new List();
var batches = influxItems.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 5000)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
for (int i = 0; i < batches.Count; i++)
influxBatches.Add(new($"Batch{i:000}", batches[i], _client));
Parallel.ForEach(influxBatches, x => Task.Run(x.WriteLoop));
while (true)
{
influxBatches.ForEach(x => Console.WriteLine($"Batch {x.BatchName} elapsed: {x.ExecutionMillisecond:0.00}"));
await Task.Delay(10);
}
}
private static InfluxDBClient CreateClient()
{
var builder = InfluxDBClientOptions.Builder.CreateNew();
builder.Url(Host);
builder.Authenticate(Username, Password.ToCharArray());
//builder.AuthenticateToken(Token);
builder.Bucket(Bucket);
builder.Org(Organization);
builder.LogLevel(LogLevel.None);
builder.TimeOut(TimeSpan.FromSeconds(600));
return new(builder.Build());
}
}
public class InfluxItem
{
public string? BatchName { get; set; }
private readonly string _field;
private float _value;
private DateTime _timestamp;
public PointData InfluxPoint => PointData.Measurement("Test")
.Field(_field, _value)
.Tag("BatchName", BatchName)
.Timestamp(_timestamp, WritePrecision.Ns);
public InfluxItem(int fieldCount)
{
_field = $"Influx{fieldCount:000000}";
_value = 0;
}
public void UpdateValue()
{
_value += 0.1f;
_timestamp = DateTime.Now;
}
}
public class InfluxBatch
{
private readonly InfluxDBClient _client;
public string BatchName { get; set; }
public List InfluxItems { get; set; }
public double ExecutionMillisecond { get; set; }
public InfluxBatch(string batchName, List influxItems, InfluxDBClient client)
{
InfluxItems = influxItems;
_client = client;
BatchName = batchName;
InfluxItems.ForEach(x => x.BatchName = batchName);
}
public async Task WriteLoop()
{
var stopwatch = new Stopwatch();
while (true)
{
stopwatch.Restart();
InfluxItems.ForEach(x => x.UpdateValue());
var points = InfluxItems.Select(x => x.InfluxPoint).ToList();
await _client.GetWriteApiAsync().WritePointsAsync(points);
stopwatch.Stop();
ExecutionMillisecond = stopwatch.Elapsed.Milliseconds / 1000.0;
await Task.Delay(10);
}
}
}
```
The execution time of each thread to update and write data to db < 1ms. That mean the interval between each datapoint < 11ms. But when I check the update frequency from query, the gap time > 200ms.
```
|> elapsed(unit: 1ms)
```
__Desired behavior:__
Is there any efficient method to write large amount of data to influx DB without ?
__Alternatives considered:__
Server specs:
CPU: Intel Xeon Silver 4210 (20 CPUs)
Ram: 128GB
Database storage: Local disk, HDD: xTB
__Use case:__
This program gather the factory sensor data.
Thank you!
Contributor guide
No contributing guide indexed for this repository
Research direction
No repository files or tests are named. Start by reproducing the provided C# batching program and inspecting the client behavior around GetWriteApiAsync().WritePointsAsync and the query used to measure elapsed time. Done means identifying and documenting a supported efficient approach for this write volume, with evidence from a repeatable measurement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- databases, performance
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100