Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active April 9, 2024 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itn3000/c18e2878e2dba2cb6fdd60865040dda6 to your computer and use it in GitHub Desktop.
Save itn3000/c18e2878e2dba2cb6fdd60865040dda6 to your computer and use it in GitHub Desktop.
garnet sandbox codes
using System.Diagnostics;
using System.Text;
using Azure.Core.Pipeline;
using Garnet;
using Garnet.client;
using Garnet.server;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
var services = new ServiceCollection()
.AddLogging(loggingBuilder => loggingBuilder.AddConsole());
using var serviceProvider = services.BuildServiceProvider();
using var cts = new CancellationTokenSource();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
await Task.WhenAll(
StartServer(6000, cts.Token, loggerFactory),
ClientTask(6000, cts.Token, loggerFactory),
SaveTask(6000, cts.Token, loggerFactory),
Task.Run(() =>
{
Console.ReadLine();
cts.Cancel();
})
);
static async Task SaveTask(int port, CancellationToken ct, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger("Save");
try
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(20));
using var client = new GarnetClient("localhost", port, logger: logger);
await client.ConnectAsync(ct);
var sw = new Stopwatch();
while(await timer.WaitForNextTickAsync(ct))
{
sw.Start();
await client.Save(ct);
logger.LogInformation($"save elapsed: {sw}");
sw.Reset();
// var infoServer = await client.Info(Garnet.common.InfoMetricsType.SERVER).ConfigureAwait(false);
// logger.LogInformation(infoServer);
// logger.LogInformation("Save Done");
}
await client.QuitAsync(ct);
}
catch(OperationCanceledException)
{
}
catch(Exception e)
{
logger.LogError(e, "SaveTask error");
}
}
static async Task ClientTask(int port, CancellationToken ct, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger("Client");
try
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
using var client = new GarnetClient("localhost", port, logger: logger);
await client.ConnectAsync(ct);
long cnt = 0;
var sw = new Stopwatch();
do
{
sw.Start();
var data = new byte[128];
for (int i = 0; i < 1000000; i++)
{
data.AsSpan().Fill((byte)(cnt & 0xff));
await client.StringSetAsync(Encoding.UTF8.GetBytes($"k{i}"), data.AsMemory(), ct);
}
cnt++;
logger.LogInformation($"iteration {cnt}: {sw.Elapsed}");
sw.Reset();
} while (await timer.WaitForNextTickAsync(ct));
await client.QuitAsync(ct);
}
catch (OperationCanceledException)
{
}
catch(Exception e)
{
logger.LogError(e, "ClientTask error");
}
}
static async Task StartServer(int port, CancellationToken ct, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger("Server");
using var svr = new GarnetServer(new GarnetServerOptions()
{
EnableStorageTier = true,
Port = port,
MemorySize = "64m",
PageSize = "8m",
SegmentSize = "512m",
ObjectStoreIndexSize = "64m",
IndexSize = "64m",
CompactionFrequencySecs = 5,
CheckpointThrottleFlushDelayMs = 10000,
IndexResizeFrequencySecs = 20,
IndexMaxSize = "128m",
ObjectStoreIndexMaxSize = "128m",
ObjectStoreLogMemorySize = "64m",
LogDir = "log",
// FullCheckpointLogInterval = 10,
CheckpointDir = "checkpoint",
CompactionMaxSegments = 2,
}, loggerFactory: loggerFactory);
svr.Start();
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
try
{
do
{
} while (await timer.WaitForNextTickAsync(ct));
}
catch (OperationCanceledException)
{
}
catch(Exception e)
{
logger.LogError(e, "ServerTask error");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment