Skip to content

Commit eadda8a

Browse files
committed
refactor sample
1 parent 77b6a4c commit eadda8a

File tree

6 files changed

+124
-14
lines changed

6 files changed

+124
-14
lines changed

samples/ConsoleApp/Benchy.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using BenchmarkDotNet.Attributes;
2+
using NoSQLite;
3+
4+
namespace ConsoleApp;
5+
6+
[MemoryDiagnoser(true)]
7+
public class Benchy
8+
{
9+
public NoSQLiteConnection Connection { get; set; } = null!;
10+
public NoSQLiteTable WriteTable { get; set; } = null!;
11+
public NoSQLiteTable ReadTable { get; set; } = null!;
12+
13+
public Person Person { get; set; } = null!;
14+
public string[] PeopleIds { get; set; } = null!;
15+
public Dictionary<string, Person> People { get; set; } = null!;
16+
17+
[GlobalSetup]
18+
public void Setup()
19+
{
20+
Connection = new(Path.Combine(Environment.CurrentDirectory, "benchmark.sqlite3"));
21+
WriteTable = Connection.GetTable("Write");
22+
ReadTable = Connection.GetTable("Read");
23+
24+
Person = new Person
25+
{
26+
Name = "singe_write",
27+
Description = "A"
28+
};
29+
30+
PeopleIds = (0..100).Select(static num => num.ToString()).ToArray();
31+
32+
People = PeopleIds.Select(num => new Person
33+
{
34+
Name = num.ToString(),
35+
Description = "Yay",
36+
Surname = "no"
37+
})
38+
.ToDictionary(static x => x.Name);
39+
40+
ReadTable.InsertMany(People);
41+
}
42+
43+
[GlobalCleanup]
44+
public void Cleanup() => Connection.Dispose();
45+
46+
[Benchmark]
47+
public Person Read_Singe() => ReadTable.Find<Person>("0")!;
48+
49+
[Benchmark]
50+
public List<Person> Read_Many() => ReadTable.FindMany<Person>(PeopleIds).ToList();
51+
52+
[Benchmark]
53+
public void Write_Singe() => WriteTable.Insert(Person.Name, Person);
54+
55+
[Benchmark]
56+
public void Write_Many() => WriteTable.InsertMany(People);
57+
}

samples/ConsoleApp/CRUD.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ namespace ConsoleApp;
44

55
public static class CRUD
66
{
7-
public static void Objects(NoSQLiteConnection db)
7+
public static void Objects(NoSQLiteConnection connection)
88
{
9+
var db = connection.GetTable();
910
// objects
1011
var panos = new Person
1112
{
@@ -23,7 +24,7 @@ public static void Objects(NoSQLiteConnection db)
2324
db.Insert("0", panos);
2425
db.Insert("1", john);
2526

26-
db.Insert(new Dictionary<string, Person>
27+
db.InsertMany(new Dictionary<string, Person>
2728
{
2829
["0"] = panos,
2930
["1"] = john
@@ -34,17 +35,19 @@ public static void Objects(NoSQLiteConnection db)
3435
var panos2 = db.Find<Person>("0");
3536
var john2 = db.Find<Person>("1");
3637

37-
var people = db.Find<Person>(new[] { "0", "1" }).ToArray();
38+
var people = db.FindMany<Person>(new[] { "0", "1" }).ToArray();
3839

3940
db.Remove("0");
40-
db.Remove(new[] { "0", "1" });
41+
db.RemoveMany(new[] { "0", "1" });
4142

4243
db.Insert("0", panos);
4344
db.Insert("1", john);
4445
}
4546

46-
public static void Lists(NoSQLiteConnection db)
47+
public static void Lists(NoSQLiteConnection connection)
4748
{
49+
var db = connection.GetTable();
50+
4851
var people = new[]
4952
{
5053
new Person{ Name = "person1", },

samples/ConsoleApp/ConsoleApp.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
66
<NoWarn>CS1591;NU1603;</NoWarn>
7+
<IsPackable>false</IsPackable>
8+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
79
</PropertyGroup>
810

11+
<ItemGroup>
12+
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
13+
</ItemGroup>
14+
915
<ItemGroup>
1016
<ProjectReference Include="..\..\src\NoSQLite\NoSQLite.csproj" />
1117
</ItemGroup>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace System;
2+
3+
public static class RangeExtensions
4+
{
5+
public static RangeEnumerator GetEnumerator(this Range range) => new(range);
6+
7+
public static IEnumerable<TResult> Select<TResult>(this Range range, Func<int, TResult> selector)
8+
{
9+
foreach (var num in range)
10+
{
11+
yield return selector(num);
12+
}
13+
}
14+
}
15+
16+
public struct RangeEnumerator
17+
{
18+
private int _current;
19+
private readonly int _end;
20+
21+
public RangeEnumerator(Range range)
22+
{
23+
if (range.End.IsFromEnd) throw new NotSupportedException("From end ranges not supported");
24+
25+
_current = range.Start.Value - 1;
26+
_end = range.End.Value;
27+
}
28+
29+
public int Current => _current;
30+
31+
public bool MoveNext()
32+
{
33+
_current += 1;
34+
return _current <= _end;
35+
}
36+
}

samples/ConsoleApp/INDEX.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ namespace ConsoleApp;
44

55
public static class INDEX
66
{
7-
public static void Execute(NoSQLiteConnection db)
7+
public static void Execute(NoSQLiteConnection connection)
88
{
9+
var db = connection.GetTable();
10+
911
var indexName = "name_index";
1012

1113
var exists = db.IndexExists(indexName);

samples/ConsoleApp/Program.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
global using ConsoleApp.Data;
2+
using BenchmarkDotNet.Configs;
3+
using BenchmarkDotNet.Running;
24
using ConsoleApp;
35
using NoSQLite;
46
using System.Diagnostics;
57
using System.Text.Json;
68
using System.Text.Json.Serialization;
79

10+
11+
//BenchmarkRunner.Run<Benchy>();
12+
13+
//return;
14+
815
var stopwatch = Stopwatch.StartNew();
916

10-
var db = new NoSQLiteConnection(
17+
var connection = new NoSQLiteConnection(
1118
Path.Combine(Environment.CurrentDirectory, "console.sqlite3"),
1219
new JsonSerializerOptions
1320
{
@@ -19,22 +26,21 @@
1926
Console.WriteLine($"Open elapsed time: {stopwatch.ElapsedMilliseconds}");
2027

2128
Console.WriteLine($"""
22-
Path: {db.Path}
23-
Table: {db.Table}
24-
Version: {db.Version}
29+
Path: {connection.Path}
30+
Version: {connection.Version}
2531
""");
2632

2733
stopwatch.Restart();
2834

29-
CRUD.Objects(db);
30-
CRUD.Lists(db);
31-
INDEX.Execute(db);
35+
CRUD.Objects(connection);
36+
CRUD.Lists(connection);
37+
INDEX.Execute(connection);
3238

3339
stopwatch.Stop();
3440
Console.WriteLine($"Operation time: {stopwatch.ElapsedMilliseconds}");
3541

3642
stopwatch.Restart();
37-
db.Dispose();
43+
connection.Dispose();
3844

3945
stopwatch.Stop();
4046
Console.WriteLine($"Close elapsed time: {stopwatch.ElapsedMilliseconds}");

0 commit comments

Comments
 (0)