Skip to content

Commit

Permalink
⚡️ optimize for single instance of HttpClient without factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Aug 6, 2023
1 parent 2c00c4c commit 3cd0c7d
Show file tree
Hide file tree
Showing 21 changed files with 297 additions and 123 deletions.
11 changes: 5 additions & 6 deletions SurrealDb.Benchmarks/BaseBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ public class BaseBenchmark
protected string HttpUrl { get; } = "http://localhost:8000";
protected string WsUrl { get; } = "ws://localhost:8000/rpc";

protected async Task InitializeSurrealDbClient(ISurrealDbClient client, DatabaseInfo databaseInfo)
protected void InitializeSurrealDbClient(ISurrealDbClient client, DatabaseInfo databaseInfo)
{
await client.SignIn(new RootAuth { Username = "root", Password = "root" });
await client.Use(databaseInfo.Namespace, databaseInfo.Database);
client.Configure(databaseInfo.Namespace, databaseInfo.Database, "root", "root");
}

protected async Task CreatePostTable(string url, DatabaseInfo databaseInfo)
{
var client = new SurrealDbClient(url);
await InitializeSurrealDbClient(client, databaseInfo);
InitializeSurrealDbClient(client, databaseInfo);

string query = GetPostQueryContent();
await client.Query(query);
Expand All @@ -25,7 +24,7 @@ protected async Task CreatePostTable(string url, DatabaseInfo databaseInfo)
protected async Task<List<GeneratedPost>> SeedData(string url, DatabaseInfo databaseInfo, int count = 1000)
{
var client = new SurrealDbClient(url);
await InitializeSurrealDbClient(client, databaseInfo);
InitializeSurrealDbClient(client, databaseInfo);

var tasks = new List<Task>();

Expand All @@ -45,7 +44,7 @@ protected async Task<List<GeneratedPost>> SeedData(string url, DatabaseInfo data
protected async Task<Post> GetFirstPost(string httpUrl, DatabaseInfo databaseInfo)
{
var client = new SurrealDbClient(httpUrl);
await InitializeSurrealDbClient(client, databaseInfo);
InitializeSurrealDbClient(client, databaseInfo);

var posts = await client.Select<Post>("post");
return posts.First();
Expand Down
9 changes: 6 additions & 3 deletions SurrealDb.Benchmarks/CreateBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ public async Task GlobalSetup()
{
case 0:
_surrealdbHttpClient = new SurrealDbClient(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
await _surrealdbHttpClient.Connect();
break;
case 1:
_surrealdbHttpClientWithHttpClientFactory = clientGenerator.Create(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
await _surrealdbHttpClientWithHttpClientFactory.Connect();
break;
case 2:
_surrealdbWsTextClient = new SurrealDbClient(WsUrl);
await InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
await _surrealdbWsTextClient.Connect();
break;
}
}
Expand Down
9 changes: 6 additions & 3 deletions SurrealDb.Benchmarks/DeleteBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ public async Task GlobalSetup()
{
case 0:
_surrealdbHttpClient = new SurrealDbClient(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
await _surrealdbHttpClient.Connect();
break;
case 1:
_surrealdbHttpClientWithHttpClientFactory = clientGenerator.Create(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
await _surrealdbHttpClientWithHttpClientFactory.Connect();
break;
case 2:
_surrealdbWsTextClient = new SurrealDbClient(WsUrl);
await InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
await _surrealdbWsTextClient.Connect();
break;
}

Expand Down
9 changes: 6 additions & 3 deletions SurrealDb.Benchmarks/QueryBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ public async Task GlobalSetup()
{
case 0:
_surrealdbHttpClient = new SurrealDbClient(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
await _surrealdbHttpClient.Connect();
break;
case 1:
_surrealdbHttpClientWithHttpClientFactory = clientGenerator.Create(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
await _surrealdbHttpClientWithHttpClientFactory.Connect();
break;
case 2:
_surrealdbWsTextClient = new SurrealDbClient(WsUrl);
await InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
await _surrealdbWsTextClient.Connect();
break;
}

Expand Down
9 changes: 6 additions & 3 deletions SurrealDb.Benchmarks/SelectBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ public async Task GlobalSetup()
{
case 0:
_surrealdbHttpClient = new SurrealDbClient(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
await _surrealdbHttpClient.Connect();
break;
case 1:
_surrealdbHttpClientWithHttpClientFactory = clientGenerator.Create(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
await _surrealdbHttpClientWithHttpClientFactory.Connect();
break;
case 2:
_surrealdbWsTextClient = new SurrealDbClient(WsUrl);
await InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
await _surrealdbWsTextClient.Connect();
break;
}

Expand Down
9 changes: 6 additions & 3 deletions SurrealDb.Benchmarks/UpsertBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ public async Task GlobalSetup()
{
case 0:
_surrealdbHttpClient = new SurrealDbClient(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClient, dbInfo);
await _surrealdbHttpClient.Connect();
break;
case 1:
_surrealdbHttpClientWithHttpClientFactory = clientGenerator.Create(HttpUrl);
await InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
InitializeSurrealDbClient(_surrealdbHttpClientWithHttpClientFactory, dbInfo);
await _surrealdbHttpClientWithHttpClientFactory.Connect();
break;
case 2:
_surrealdbWsTextClient = new SurrealDbClient(WsUrl);
await InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
InitializeSurrealDbClient(_surrealdbWsTextClient, dbInfo);
await _surrealdbWsTextClient.Connect();
break;
}

Expand Down
6 changes: 1 addition & 5 deletions SurrealDb/Internals/Auth/Auth.Basic.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
namespace SurrealDb.Internals.Auth;

internal class BasicAuth : IAuth
{
public string Username { get; set; } = string.Empty;
public string? Password { get; set; }
}
internal record BasicAuth(string Username, string? Password) : IAuth;
5 changes: 1 addition & 4 deletions SurrealDb/Internals/Auth/Auth.Bearer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
namespace SurrealDb.Internals.Auth;

internal class BearerAuth : IAuth
{
public string Token { get; set; } = string.Empty;
}
internal record BearerAuth(string Token) : IAuth;
2 changes: 1 addition & 1 deletion SurrealDb/Internals/Auth/Auth.None.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace SurrealDb.Internals.Auth;

internal class NoAuth : IAuth { }
internal record NoAuth : IAuth;
10 changes: 10 additions & 0 deletions SurrealDb/Internals/Constants/HttpConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SurrealDb.Internals.Constants;

internal static class HttpConstants
{
public const string ACCEPT_HEADER_NAME = "Accept";
public const string NS_HEADER_NAME = "NS";
public const string DB_HEADER_NAME = "DB";

public static readonly string[] ACCEPT_HEADER_VALUES = new[] { "application/json" };
}
5 changes: 5 additions & 0 deletions SurrealDb/Internals/Http/HttpClientConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using SurrealDb.Internals.Auth;

namespace SurrealDb.Internals.Http;

internal record HttpClientConfiguration(string? Namespace, string? Database, IAuth Auth);
20 changes: 20 additions & 0 deletions SurrealDb/Internals/Http/HttpClientWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace SurrealDb.Internals.Http;

internal class HttpClientWrapper : IDisposable
{
private readonly bool _shouldDispose;

public HttpClient Instance { get; }

public HttpClientWrapper(HttpClient httpClient, bool shouldDispose)
{
Instance = httpClient;
_shouldDispose = shouldDispose;
}

public void Dispose()
{
if (_shouldDispose)
Instance.Dispose();
}
}
4 changes: 2 additions & 2 deletions SurrealDb/Internals/Http/SurrealDbHttpEngineConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public void Use(string ns, string? db)

public void SetBasicAuth(string username, string? password)
{
Auth = new BasicAuth { Username = username, Password = password };
Auth = new BasicAuth(username, password);
}

public void SetBearerAuth(string token)
{
Auth = new BearerAuth { Token = token };
Auth = new BearerAuth(token);
}

public void ResetAuth()
Expand Down
7 changes: 7 additions & 0 deletions SurrealDb/Internals/Http/UseConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SurrealDb.Internals.Http;

internal class UseConfiguration
{
public string? Ns { get; set; }
public string? Db { get; set; }
}
Loading

0 comments on commit 3cd0c7d

Please sign in to comment.