-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Potential performance problem #104
Comments
Hello Thomas,
Let's concentrate on the .NET 8.0 runtime for now. The
I assume your machine is even more performant than this one.
|
Of course 50ms is not forever. It just struck me that the same query The 50ms is measured with |
This is weird. Do you have a reproduction program to share? |
using System.Diagnostics;
using System.Text.Json;
using SurrealDb.Net;
var builder = WebApplication.CreateBuilder(args);
var url = Var("SURREAL_URL") ?? "127.0.0.1:8000";
var protocol = Var("SURREAL_PROTOCOL") ?? "ws";
var suffix = protocol.StartsWith("ws") ? "/rpc" : "";
var endpoint = $"{protocol}://{url}{suffix}";
builder.Services.AddSurreal(
SurrealDbOptions
.Create()
.WithEndpoint(endpoint)
.WithNamespace(Var("SURREAL_NAMESPACE") ?? "default_ns")
.WithDatabase(Var("SURREAL_DATABASE") ?? "default_db")
.WithUsername(Var("SURREAL_USERNAME") ?? "root")
.WithPassword(Var("SURREAL_PASSWORD") ?? "root")
.Build(),
lifetime: ServiceLifetime.Scoped,
configureJsonSerializerOptions: options => {
options.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
}
);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.MapGet("/AllMigrations", async (ISurrealDbClient client, CancellationToken ct) => {
var stopwatch = new Stopwatch();
stopwatch.Start();
var response = await client.Query($"SELECT * FROM migrations", ct);
stopwatch.Stop();
Console.WriteLine($"Ellapsed on AllMigrations query: {stopwatch.ElapsedMilliseconds}ms");
});
app.MapGet("/GetCompletedMigrations", async (ISurrealDbClient client, CancellationToken ct) => {
var stopwatch = new Stopwatch();
stopwatch.Start();
var response = await client.Query($"SELECT VALUE name FROM migrations WHERE completed = true", ct);
stopwatch.Stop();
Console.WriteLine($"Ellapsed on GetCompletedMigrations query: {stopwatch.ElapsedMilliseconds}ms");
stopwatch = new Stopwatch();
stopwatch.Start();
var names = response.GetValues<string>(0);
stopwatch.Stop();
Console.WriteLine($"Ellapsed on GetValues<string> for GetCompletedMigrations: {stopwatch.ElapsedMilliseconds}ms");
return names;
});
app.Run();
static string? Var(string varName) {
return Environment.GetEnvironmentVariable(varName);
} |
Ah, thank you for taking the time to make a reproduction example. I can see 2 major performance limitations. This first one made me jump from 30ms to 3ms. The second one down to 1ms. I'll explain.
I am currently working on a way to consume a pool of clients, so that you do not have to recreate a client and better yet not to reconnect on each instance injection. So, I'd recommend to use Singleton (if you can) for the best performance until this feature is available.
Hope that would be helpful to you. Feel free to ask any question. |
That makes sense. I thought client pooling was implemented, so I automatically chose scoped lifetime, but singleton is the way to go for me too. Unfortunately, I removed all the converters I have implemented for Strong Typed IDs from this demo. So I'll wait. I'm closing this issue now that penalties are already addressed. Thanks for the explanation and I look forward to the update! P.S. Wouldn't it be useful to note that for now there is no client pooling and JsonSerializer is not cached? |
For some reason when I do an HTTP query to get all data from a table that contains 3 records the average response is 50ms.
But when I do the query directly using the HTTP client (Insomnia) the query takes under 1ms. Even in python library query using WS takes under 1ms.
For both HTTP and WS client the query duration is same.
os: macOS 14.5
chipset: Apple M3 Pro 18gb
docker: 4.30.0
surreal: 1.5.2
dotnet: 8.0.302
surreal.net: 0.5.0
The text was updated successfully, but these errors were encountered: