Skip to content

Commit

Permalink
updating insertion, transactions, and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstonis committed Sep 1, 2021
1 parent ca74680 commit daefaa2
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 236 deletions.
42 changes: 21 additions & 21 deletions SqliteJson/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tycho;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace SqliteJson
{
public partial class MainPage : ContentPage
{
public MainPage ()
{
InitializeComponent ();
}

using Xamarin.Forms;

namespace SqliteJson
{
public partial class MainPage : ContentPage
{
public MainPage ()
{
InitializeComponent ();
}

protected override async void OnAppearing ()
{
base.OnAppearing ();

using var db =
await new TychoDb (FileSystem.AppDataDirectory)
await new TychoDb (FileSystem.AppDataDirectory, new SystemTextJsonSerializer())
.ConnectAsync();

var testObj =
Expand All @@ -38,7 +38,7 @@ protected override async void OnAppearing ()
var readResult = await db.ReadObjectAsync<TestClassA> (testObj.StringProperty);

System.Diagnostics.Debug.WriteLine ($"{readResult}");
}
}
}

class TestClassA
Expand All @@ -48,5 +48,5 @@ class TestClassA
public int IntProperty { get; set; }

public long TimestampMillis { get; set; }
}
}
}
}
2 changes: 2 additions & 0 deletions SqliteJson/SqliteJson.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tycho.NewtonsoftJson\Tycho.NewtonsoftJson.csproj" />
<ProjectReference Include="..\Tycho.SystemTextJson\Tycho.SystemTextJson.csproj" />
<ProjectReference Include="..\Tycho\Tycho.csproj" />
</ItemGroup>
</Project>
162 changes: 116 additions & 46 deletions Tycho.Benchmarks/Benchmarks/Insertion.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,118 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.Diagnostics.Tracing.Parsers.ApplicationServer;

namespace Tycho.Benchmarks.Benchmarks
{
[MemoryDiagnoser]
[SimpleJob (launchCount: 1, warmupCount: 1, targetCount: 10)]
public class Insertion
{
[ParamsSource (nameof (JsonSerializers))]
public IJsonSerializer JsonSerializer { get; set; }

public static IEnumerable<IJsonSerializer> JsonSerializers ()
=> new IJsonSerializer[] { new SystemTextJsonSerializer (), new NewtonsoftJsonSerializer() };

[Benchmark]
public async Task<int> InsertManyAsync ()
{
namespace Tycho.Benchmarks.Benchmarks
{
[MemoryDiagnoser]
[SimpleJob (launchCount: 1, warmupCount: 1, targetCount: 10)]
public class Insertion
{
[ParamsSource (nameof (JsonSerializers))]
public IJsonSerializer JsonSerializer { get; set; }

public static IEnumerable<IJsonSerializer> JsonSerializers ()
=> new IJsonSerializer[] { new SystemTextJsonSerializer (), new NewtonsoftJsonSerializer() };

private static TestClassE _largeTestObject =
new TestClassE()
{
TestClassId = Guid.NewGuid(),
Values =
new []
{
new TestClassD()
{
DoubleProperty = 12d,
FloatProperty = 15f,
ValueC =
new TestClassC()
{
DoubleProperty = 14d,
IntProperty = 15,
}
},
new TestClassD()
{
DoubleProperty = 12d,
FloatProperty = 15f,
ValueC =
new TestClassC()
{
DoubleProperty = 14d,
IntProperty = 15,
}
},
new TestClassD()
{
DoubleProperty = 12d,
FloatProperty = 15f,
ValueC =
new TestClassC()
{
DoubleProperty = 14d,
IntProperty = 15,
}
},
new TestClassD()
{
DoubleProperty = 12d,
FloatProperty = 15f,
ValueC =
new TestClassC()
{
DoubleProperty = 14d,
IntProperty = 15,
}
}
}


};

internal static TestClassE LargeTestObject => _largeTestObject;

[Benchmark]
public async Task InsertSingularAsync()
{
using var db =
new TychoDb(Path.GetTempPath(), JsonSerializer, rebuildCache: true)
.Connect();

var testObj =
new TestClassA
{
StringProperty = $"Test String",
IntProperty = 100,
TimestampMillis = 123451234,
};


await db.WriteObjectAsync(testObj, x => x.StringProperty).ConfigureAwait(false);
}

[Benchmark]
public async Task InsertSingularLargeObjectAsync ()
{
using var db =
new TychoDb(Path.GetTempPath(), JsonSerializer, rebuildCache: true)
.Connect();

await db.WriteObjectAsync (LargeTestObject, x => x.TestClassId).ConfigureAwait (false);
}

[Benchmark]
public async Task InsertManyAsync ()
{
using var db =
new TychoDb (Path.GetTempPath (), JsonSerializer, rebuildCache: true)
.Connect ();

var successWrites = 0;

for (int i = 100; i < 1100; i++)
{
var testObj =
Expand All @@ -38,26 +124,17 @@ public async Task<int> InsertManyAsync ()
};


var resultWrite = await db.WriteObjectAsync (testObj, x => x.StringProperty).ConfigureAwait (false);
await db.WriteObjectAsync (testObj, x => x.StringProperty).ConfigureAwait (false);
}
}

if (resultWrite)
{
Interlocked.Increment (ref successWrites);
}
}

return successWrites;
}

[Benchmark]
public async Task<int> InsertManyConcurrentAsync ()
{
[Benchmark]
public async Task InsertManyConcurrentAsync ()
{
using var db =
new TychoDb (Path.GetTempPath (), JsonSerializer, rebuildCache: true)
.Connect ();

var successWrites = 0;


var tasks =
Enumerable
.Range (100, 1000)
Expand All @@ -73,18 +150,11 @@ public async Task<int> InsertManyConcurrentAsync ()
};


var resultWrite = await db.WriteObjectAsync (testObj, x => x.StringProperty).ConfigureAwait (false);

if (resultWrite)
{
Interlocked.Increment (ref successWrites);
}
await db.WriteObjectAsync (testObj, x => x.StringProperty).ConfigureAwait (false);
})
.ToList ();

await Task.WhenAll (tasks).ConfigureAwait (false);

return successWrites;
}
}
}
await Task.WhenAll (tasks).ConfigureAwait (false);
}
}
}
8 changes: 4 additions & 4 deletions Tycho.Benchmarks/TestModels.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;

using System.Collections.Generic;

namespace Tycho.Benchmarks
{
{
class TestClassA
{
public string StringProperty { get; set; }
Expand All @@ -24,7 +24,7 @@ class TestClassC
{
public int IntProperty { get; set; }

public string DoubleProperty { get; set; }
public double DoubleProperty { get; set; }
}

class TestClassD
Expand Down
9 changes: 5 additions & 4 deletions Tycho.UnitTests/Tycho.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.3"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions Tycho.UnitTests/TychoDbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,9 @@ public class Patient : ModelBase
}

public abstract class ModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
{
#pragma warning disable CS0067
public event PropertyChangedEventHandler PropertyChanged;
#pragma warning restore CS0067
}
}
5 changes: 5 additions & 0 deletions Tycho/Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ FROM JsonValue
AND
FullTypeName = $fullTypeName";

public const string SelectPartitions =
@"
SELECT DISTINCT Partition
From JsonValue";

public const string SelectDataFromJsonValueWithFullTypeName =
@"
SELECT Data
Expand Down
38 changes: 19 additions & 19 deletions Tycho/RegisteredTypeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ internal struct RegisteredTypeInformation

public string IdProperty { get; set; }

public string IdPropertyPath { get; set; }

public bool IsNumeric { get; set; }

public string TypeFullName { get; set; }

public string TypeName { get; set; }
Expand All @@ -31,7 +35,9 @@ public static RegisteredTypeInformation Create<T, TId> (Expression<Func<T, TId>>
new RegisteredTypeInformation
{
FuncIdSelector = compiledExpression,
IdProperty = GetMemberInfo(idProperty).Member.Name,
IdProperty = GetExpressionMemberName (idProperty),
IdPropertyPath = QueryPropertyPath.BuildPath(idProperty),
IsNumeric = QueryPropertyPath.IsNumeric(idProperty),
TypeFullName = type.FullName,
TypeName = type.Name,
TypeNamespace = type.Namespace,
Expand All @@ -49,28 +55,22 @@ public Func<T, object> GetId<T> ()
return (Func<T, object>)FuncIdSelector;
}

private static MemberExpression GetMemberInfo (Expression method)
private static string GetExpressionMemberName (Expression method)
{
LambdaExpression lambda = method as LambdaExpression;
if (lambda == null)
throw new ArgumentNullException ("method");

MemberExpression memberExpr = null;

if (lambda.Body.NodeType == ExpressionType.Convert)
if(method is LambdaExpression lex)
{
memberExpr =
((UnaryExpression)lambda.Body).Operand as MemberExpression;
}
else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
{
memberExpr = lambda.Body as MemberExpression;
}
if (lex.Body.NodeType == ExpressionType.Convert)
{
return (((UnaryExpression)lex.Body).Operand as MemberExpression).Member.Name;
}

if (memberExpr == null)
throw new ArgumentException ("method");
if (lex.Body.NodeType == ExpressionType.MemberAccess)
{
return (lex.Body as MemberExpression).Member.Name;
}
}

return memberExpr;
throw new TychoDbException ("The provided expression is not valid member expression");
}
}
}
Loading

0 comments on commit daefaa2

Please sign in to comment.