Skip to content

Commit d79152d

Browse files
authored
fix: remove download overhead for Queries (#181)
1 parent 847c5be commit d79152d

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
### Bug Fixes
1313
1. [#168](https://github.com/influxdata/influxdb-client-csharp/pull/168): DateTime is always serialized into UTC
1414
1. [#169](https://github.com/influxdata/influxdb-client-csharp/pull/169): Fixed domain structure for Flux AST
15+
1. [#181](https://github.com/influxdata/influxdb-client-csharp/pull/181): Removed download overhead for Queries
1516

1617
### Dependencies
1718
1. [#175](https://github.com/influxdata/influxdb-client-csharp/pull/175): Updated dependencies of `InfluxDB.Client`:

Client.Core/Internal/AbstractQueryClient.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ private async Task Query(RestRequest query, Action<ICancellable, Stream> consume
113113
consumer(cancellable, responseStream);
114114
};
115115

116-
await Task.Run(() => { RestClient.DownloadData(query, true); }).ConfigureAwait(false);
116+
var restResponse = await RestClient.ExecuteAsync(query, Method.POST).ConfigureAwait(false);
117+
if (restResponse.ErrorException != null)
118+
{
119+
throw restResponse.ErrorException;
120+
}
121+
117122
if (!cancellable.IsCancelled())
118123
{
119124
onComplete();
@@ -147,7 +152,12 @@ private void QuerySync(RestRequest query, Action<ICancellable, Stream> consumer,
147152
consumer(cancellable, responseStream);
148153
};
149154

150-
RestClient.DownloadData(query, true);
155+
var restResponse = RestClient.Execute(query, Method.POST);
156+
if (restResponse.ErrorException != null)
157+
{
158+
throw restResponse.ErrorException;
159+
}
160+
151161
if (!cancellable.IsCancelled())
152162
{
153163
onComplete();

Client.Legacy/FluxClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ private async Task<IRestResponse> ExecuteAsync(RestRequest request)
423423
{
424424
BeforeIntercept(request);
425425

426-
var response = await Task.Run(() => RestClient.Execute(request)).ConfigureAwait(false);
426+
var response = await RestClient.ExecuteAsync(request).ConfigureAwait(false);
427427

428428
RaiseForInfluxError(response, response.Content);
429429

Client.Test/QueryApiTest.cs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using InfluxDB.Client.Core;
7+
using InfluxDB.Client.Core.Flux.Domain;
8+
using InfluxDB.Client.Core.Test;
9+
using NUnit.Framework;
10+
using WireMock.RequestBuilders;
11+
12+
namespace InfluxDB.Client.Test
13+
{
14+
[TestFixture]
15+
public class QueryApiTest : AbstractMockServerTest
16+
{
17+
private InfluxDBClient _influxDbClient;
18+
private QueryApi _queryApi;
19+
20+
private const string Data =
21+
"#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,string,string,string,string,double\n"
22+
+ "#group,false,false,false,false,false,false,false,false,false,false\n"
23+
+ "#default,_result,,,,,,,,,\n"
24+
+ ",result,table,_start,_stop,_time,id,_field,_measurement,host,_value\n"
25+
+ ",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,ab,free,mem,A,12.25\n"
26+
+ ",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,cd,free,mem,A,13.00\n";
27+
28+
[SetUp]
29+
public new void SetUp()
30+
{
31+
var options = InfluxDBClientOptions.Builder
32+
.CreateNew()
33+
.Url(MockServerUrl)
34+
.AuthenticateToken("token")
35+
.Org("my-org")
36+
.Build();
37+
38+
_influxDbClient = InfluxDBClientFactory.Create(options);
39+
_influxDbClient.SetLogLevel(LogLevel.Body);
40+
_queryApi = _influxDbClient.GetQueryApi();
41+
}
42+
43+
[Test]
44+
public async Task ParallelRequest()
45+
{
46+
MockServer
47+
.Given(Request.Create().WithPath("/api/v2/query").UsingPost())
48+
.RespondWith(CreateResponse(Data));
49+
50+
var stopWatch = new Stopwatch();
51+
stopWatch.Start();
52+
53+
var tasks = new List<Task<List<FluxTable>>>();
54+
foreach (var _ in Enumerable.Range(0, 100))
55+
{
56+
tasks.Add(_queryApi.QueryAsync("from(bucket:\"my-bucket\") |> range(start: 0)", "my-org"));
57+
}
58+
await Task.WhenAll(tasks);
59+
60+
var ts = stopWatch.Elapsed;
61+
Assert.LessOrEqual(ts.TotalSeconds, 10, $"Elapsed time: {ts}");
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)