Skip to content

Commit

Permalink
Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
masesdevelopers committed Sep 27, 2023
1 parent fdb6645 commit 16a5ba9
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@

using Java.Lang;
using Java.Util;
using MASES.JCOBridge.C2JBridge;
using MASES.KNet.Common;
using MASES.KNet.Producer;
using MASES.KNet.Streams;
using Org.Apache.Kafka.Clients.Consumer;
using Org.Apache.Kafka.Clients.Producer;
using Org.Apache.Kafka.Streams;
using System.Globalization;
using System.Text;

namespace MASES.EntityFrameworkCore.KNet.Infrastructure.Internal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

using MASES.EntityFrameworkCore.KNet.Infrastructure.Internal;
using MASES.KNet;
using MASES.KNet.Common;
using MASES.KNet.Producer;
using MASES.KNet.Streams;
Expand Down
3 changes: 2 additions & 1 deletion test/KEFCore.Test/KEFCore.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
</ItemGroup>
</Project>
224 changes: 161 additions & 63 deletions test/KEFCore.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,116 +25,204 @@
using MASES.EntityFrameworkCore.KNet.Infrastructure;
using MASES.KNet.Streams;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace MASES.EntityFrameworkCore.KNet.Test
{
class Program
partial class Program
{
const string theServer = "localhost:9092";
static string serverToUse = theServer;
static string databaseName = "TestDB";
static string databaseNameWithModel = "TestDBWithModel";
static string applicationId = "TestApplication";

static void Main(string[] args)
{
KEFCore.CreateGlobalInstance();
var appArgs = KEFCore.FilteredArgs;

if (appArgs.Length != 0)
if (!UseInMemoryProvider)
{
serverToUse = args[0];
KEFCore.CreateGlobalInstance();
var appArgs = KEFCore.FilteredArgs;

if (appArgs.Length > 0)
{
serverToUse = args[0];
}

if (appArgs.Length > 1)
{
deleteApplication = args[1].ToLowerInvariant() == "true";
}

if (appArgs.Length > 2)
{
applicationId = args[2];
}
}

var streamConfig = StreamsConfigBuilder.Create();
streamConfig = streamConfig.WithAcceptableRecoveryLag(100);
DatabaseName = UseModelBuilder ? databaseNameWithModel : databaseName;

using (var context = new BloggingContext()
var globalWatcher = Stopwatch.StartNew();
StreamsConfigBuilder streamConfig = null;
if (!UseInMemoryProvider)
{
streamConfig = StreamsConfigBuilder.Create();
streamConfig = streamConfig.WithAcceptableRecoveryLag(100);
}

var context = new BloggingContext()
{
BootstrapServers = serverToUse,
ApplicationId = "TestApplication",
DbName = "TestDB",
ApplicationId = applicationId,
DbName = DatabaseName,
StreamsConfigBuilder = streamConfig,
})
};

if (deleteApplication)
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}

for (int i = 0; i < 1000; i++)
var testWatcher = Stopwatch.StartNew();
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < 1000; i++)
{
context.Add(new Blog
{
context.Add(new Blog
{
Url = "http://blogs.msdn.com/adonet" + i.ToString(),
Posts = new List<Post>()
Url = "http://blogs.msdn.com/adonet" + i.ToString(),
Posts = new List<Post>()
{
new Post()
{
Title = "title",
Content = i.ToString()
}
},
Rating = i,
});
}
context.SaveChanges();
Rating = i,
});
}
watch.Stop();
Trace.WriteLine($"Elapsed data load {watch.ElapsedMilliseconds} ms");

using (var context = new BloggingContext()
{
BootstrapServers = serverToUse,
ApplicationId = "TestApplication",
DbName = "TestDB",
StreamsConfigBuilder = streamConfig,
})
{

//var pageObject = (from op in context.Blogs
// join pg in context.Posts on op.BlogId equals pg.BlogId
// where pg.BlogId == op.BlogId
// select new { pg, op }).SingleOrDefault();

Stopwatch watch = Stopwatch.StartNew();
var post = context.Posts.Single(b => b.BlogId == 2);
watch.Stop();
Trace.WriteLine($"Elapsed {watch.ElapsedMilliseconds} ms");
watch.Restart();
context.SaveChanges();
watch.Stop();
Trace.WriteLine($"Elapsed SaveChanges {watch.ElapsedMilliseconds} ms");

if (UseModelBuilder)
{
watch.Restart();
post = context.Posts.Single(b => b.BlogId == 1);
var pageObject = (from op in context.Blogs
join pg in context.Posts on op.BlogId equals pg.BlogId
where pg.BlogId == op.BlogId
select new { pg, op }).SingleOrDefault();
watch.Stop();
Trace.WriteLine($"Elapsed {watch.ElapsedMilliseconds} ms");
Trace.WriteLine($"Elapsed UseModelBuilder {watch.ElapsedMilliseconds} ms");
}

watch.Restart();
var all = context.Posts.All((o) => true);
watch.Stop();
Trace.WriteLine($"Elapsed {watch.ElapsedMilliseconds} ms");
watch.Restart();
var post = context.Posts.Single(b => b.BlogId == 2);
watch.Stop();
Trace.WriteLine($"Elapsed context.Posts.Single(b => b.BlogId == 2) {watch.ElapsedMilliseconds} ms. Result is {post}");

watch.Restart();
post = context.Posts.Single(b => b.BlogId == 1);
watch.Stop();
Trace.WriteLine($"Elapsed context.Posts.Single(b => b.BlogId == 1) {watch.ElapsedMilliseconds} ms. Result is {post}");

watch.Restart();
var all = context.Posts.All((o) => true);
watch.Stop();
Trace.WriteLine($"Elapsed context.Posts.All((o) => true) {watch.ElapsedMilliseconds} ms. Result is {all}");

watch.Restart();
var blog = context.Blogs!.Single(b => b.BlogId == 1);
watch.Stop();
Trace.WriteLine($"Elapsed context.Blogs!.Single(b => b.BlogId == 1) {watch.ElapsedMilliseconds} ms. Result is {blog}");

watch.Restart();
context.Remove(post);
context.Remove(blog);
watch.Stop();
Trace.WriteLine($"Elapsed data remove {watch.ElapsedMilliseconds} ms");

watch.Restart();
context.SaveChanges();
watch.Stop();
Trace.WriteLine($"Elapsed SaveChanges {watch.ElapsedMilliseconds} ms");

watch.Restart();
for (int i = 1000; i < 1100; i++)
{
context.Add(new Blog
{
Url = "http://blogs.msdn.com/adonet" + i.ToString(),
Posts = new List<Post>()
{
new Post()
{
Title = "title",
Content = i.ToString()
}
},
Rating = i,
});
}
watch.Stop();
Trace.WriteLine($"Elapsed data load {watch.ElapsedMilliseconds} ms");

watch.Restart();
var blog = context.Blogs!.Single(b => b.BlogId == 1);
watch.Stop();
Trace.WriteLine($"Elapsed {watch.ElapsedMilliseconds} ms");
watch.Restart();
context.SaveChanges();
watch.Stop();
Trace.WriteLine($"Elapsed SaveChanges {watch.ElapsedMilliseconds} ms");

var value = context.Blogs.AsQueryable().ToQueryString();
}
watch.Restart();
post = context.Posts.Single(b => b.BlogId == 1009);
watch.Stop();
Trace.WriteLine($"Elapsed context.Posts.Single(b => b.BlogId == 1009) {watch.ElapsedMilliseconds} ms. Result is {post}");

var value = context.Blogs.AsQueryable().ToQueryString();

context?.Dispose();
testWatcher.Stop();
globalWatcher.Stop();
Console.WriteLine($"Full test completed in {globalWatcher.Elapsed}, only tests completed in {testWatcher.Elapsed}");
}
}

public class BloggingContext : KafkaDbContext
{
public override bool UseCompactedReplicator { get; set; } = Program.UseCompactedReplicator;

public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//{
// optionsBuilder.UseKafkaDatabase(ApplicationId, DbName, BootstrapServers, (o) =>
// {
// o.StreamsConfig(o.EmptyStreamsConfigBuilder.WithAcceptableRecoveryLag(100)).WithDefaultNumPartitions(10);
// });
//}

//protected override void OnModelCreating(ModelBuilder modelBuilder)
//{
// modelBuilder.Entity<Blog>().HasKey(c => new { c.BlogId, c.Rating });
//}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (Program.UseInMemoryProvider)
{
optionsBuilder.UseInMemoryDatabase(Program.DatabaseName);
}
else
{
base.OnConfiguring(optionsBuilder);
}
//optionsBuilder.UseKafkaDatabase(ApplicationId, DbName, BootstrapServers, (o) =>
//{
// o.StreamsConfig(o.EmptyStreamsConfigBuilder.WithAcceptableRecoveryLag(100)).WithDefaultNumPartitions(10);
//});
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (!Program.UseModelBuilder) return;

modelBuilder.Entity<Blog>().HasKey(c => new { c.BlogId, c.Rating });
}
}

public class Blog
Expand All @@ -143,6 +231,11 @@ public class Blog
public string Url { get; set; }
public long Rating { get; set; }
public List<Post> Posts { get; set; }

public override string ToString()
{
return $"BlogId: {BlogId} Url: {Url} Rating: {Rating}";
}
}

public class Post
Expand All @@ -153,5 +246,10 @@ public class Post

public int BlogId { get; set; }
public Blog Blog { get; set; }

public override string ToString()
{
return $"PostId: {PostId} Title: {Title} Content: {Content} BlogId: {BlogId}";
}
}
}
35 changes: 35 additions & 0 deletions test/KEFCore.Test/ProgramConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* MIT License
*
* Copyright (c) 2022 MASES s.r.l.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace MASES.EntityFrameworkCore.KNet.Test
{
partial class Program
{
public static bool UseInMemoryProvider = false;
public static bool UseModelBuilder = false;
public static bool UseCompactedReplicator = false;
public static string DatabaseName = null;
static bool deleteApplication = false;
}
}

0 comments on commit 16a5ba9

Please sign in to comment.