Skip to content

Commit

Permalink
Merge pull request #311 from gritcsenko/CSharpLatest
Browse files Browse the repository at this point in the history
Use file-scoped namespaces
  • Loading branch information
snikolayev committed Nov 20, 2022
2 parents 0765b36 + 5bbce17 commit f84f59d
Show file tree
Hide file tree
Showing 440 changed files with 34,918 additions and 35,342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@
using NRules.Diagnostics;
using NRules.Rete;

namespace NRules.Benchmark.Expressions
namespace NRules.Benchmark.Expressions;

[MemoryDiagnoser]
public abstract class BenchmarkBase
{
[MemoryDiagnoser]
public abstract class BenchmarkBase
internal IExecutionContext Context;

protected BenchmarkBase()
{
internal IExecutionContext Context;
Context = new ExecutionContext(null, null, null, new EventAggregator(), null, null);
}

protected BenchmarkBase()
{
Context = new ExecutionContext(null, null, null, new EventAggregator(), null, null);
}
internal static Tuple ToTuple(params object[] values)
{
int i = 0;
var tuple = new Tuple(i);

internal static Tuple ToTuple(params object[] values)
foreach (var value in values)
{
int i = 0;
var tuple = new Tuple(i);

foreach (var value in values)
{
tuple = new Tuple(++i, tuple, new Fact(value));
}

return tuple;
tuple = new Tuple(++i, tuple, new Fact(value));
}

return tuple;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,40 @@
using NRules.Utilities;
using Tuple = NRules.Rete.Tuple;

namespace NRules.Benchmark.Expressions
namespace NRules.Benchmark.Expressions;

[BenchmarkCategory("Micro", "Expressions")]
public class BenchmarkLhsExpression : BenchmarkBase
{
[BenchmarkCategory("Micro", "Expressions")]
public class BenchmarkLhsExpression : BenchmarkBase
private readonly NodeInfo _nodeInfo;
private readonly ILhsExpression<bool> _lhsExpression;
private readonly ILhsTupleExpression<bool> _lhsTupleExpression;
private readonly ILhsFactExpression<bool> _lhsFactExpression;
private readonly Tuple _tuple;
private readonly Fact _fact;

public BenchmarkLhsExpression()
{
private readonly NodeInfo _nodeInfo;
private readonly ILhsExpression<bool> _lhsExpression;
private readonly ILhsTupleExpression<bool> _lhsTupleExpression;
private readonly ILhsFactExpression<bool> _lhsFactExpression;
private readonly Tuple _tuple;
private readonly Fact _fact;
_nodeInfo = new NodeInfo();
var ruleExpressionCompiler = new RuleExpressionCompiler();
Expression<Func<string, int, decimal, bool>> betaExpression = (s, i, d) => s.Length == i;
var betaElement = Element.Condition(betaExpression);
_lhsExpression = ruleExpressionCompiler.CompileLhsTupleFactExpression<bool>(betaElement, betaElement.Imports.ToList());
_lhsTupleExpression = ruleExpressionCompiler.CompileLhsTupleExpression<bool>(betaElement, betaElement.Imports.ToList());
_tuple = ToTuple("abcd", 4, 1.0m);

public BenchmarkLhsExpression()
{
_nodeInfo = new NodeInfo();
var ruleExpressionCompiler = new RuleExpressionCompiler();
Expression<Func<string, int, decimal, bool>> betaExpression = (s, i, d) => s.Length == i;
var betaElement = Element.Condition(betaExpression);
_lhsExpression = ruleExpressionCompiler.CompileLhsTupleFactExpression<bool>(betaElement, betaElement.Imports.ToList());
_lhsTupleExpression = ruleExpressionCompiler.CompileLhsTupleExpression<bool>(betaElement, betaElement.Imports.ToList());
_tuple = ToTuple("abcd", 4, 1.0m);
Expression<Func<string, bool>> alphaExpression = s => s.Length == 1;
var alphaElement = Element.Condition(alphaExpression);
_lhsFactExpression = ruleExpressionCompiler.CompileLhsFactExpression<bool>(alphaElement);
_fact = new Fact("abcd");
}

Expression<Func<string, bool>> alphaExpression = s => s.Length == 1;
var alphaElement = Element.Condition(alphaExpression);
_lhsFactExpression = ruleExpressionCompiler.CompileLhsFactExpression<bool>(alphaElement);
_fact = new Fact("abcd");
}
[Benchmark]
public bool EvaluateTupleFactExpression() => _lhsExpression.Invoke(Context, _nodeInfo, _tuple.LeftTuple, _tuple.RightFact);

[Benchmark]
public bool EvaluateTupleFactExpression() => _lhsExpression.Invoke(Context, _nodeInfo, _tuple.LeftTuple, _tuple.RightFact);

[Benchmark]
public bool EvaluateTupleExpression() => _lhsTupleExpression.Invoke(Context, _nodeInfo, _tuple);

[Benchmark]
public bool EvaluateFactExpression() => _lhsFactExpression.Invoke(Context, _nodeInfo, _fact);
}
[Benchmark]
public bool EvaluateTupleExpression() => _lhsTupleExpression.Invoke(Context, _nodeInfo, _tuple);

[Benchmark]
public bool EvaluateFactExpression() => _lhsFactExpression.Invoke(Context, _nodeInfo, _fact);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,36 @@
using NRules.RuleModel.Builders;
using NRules.Utilities;

namespace NRules.Benchmark.Expressions
namespace NRules.Benchmark.Expressions;

[BenchmarkCategory("Micro", "Expressions")]
public class BenchmarkRuleAction : BenchmarkBase
{
[BenchmarkCategory("Micro", "Expressions")]
public class BenchmarkRuleAction : BenchmarkBase
{
private readonly IActionContext _actionContext;
private readonly IRuleAction _ruleAction;
private readonly IActionContext _actionContext;
private readonly IRuleAction _ruleAction;

public BenchmarkRuleAction()
{
var ruleExpressionCompiler = new RuleExpressionCompiler();
Expression<Action<IContext, string, int, decimal>> expression = (c, s, i, d) => PerformAction(c, s, i, d);
var element = Element.Action(expression);
var map = IndexMap.CreateMap(element.Imports, element.Imports);
_ruleAction = ruleExpressionCompiler.CompileAction(element, element.Imports.ToList(), new List<DependencyElement>(), map);
public BenchmarkRuleAction()
{
var ruleExpressionCompiler = new RuleExpressionCompiler();
Expression<Action<IContext, string, int, decimal>> expression = (c, s, i, d) => PerformAction(c, s, i, d);
var element = Element.Action(expression);
var map = IndexMap.CreateMap(element.Imports, element.Imports);
_ruleAction = ruleExpressionCompiler.CompileAction(element, element.Imports.ToList(), new List<DependencyElement>(), map);

var compiledRule = new CompiledRule(null, element.Imports, new []{_ruleAction}, null, map);
var tuple = ToTuple("abcd", 4, 1.0m);
var activation = new Activation(compiledRule, tuple);
_actionContext = new ActionContext(Context.Session, activation, CancellationToken.None);
}
var compiledRule = new CompiledRule(null, element.Imports, new []{_ruleAction}, null, map);
var tuple = ToTuple("abcd", 4, 1.0m);
var activation = new Activation(compiledRule, tuple);
_actionContext = new ActionContext(Context.Session, activation, CancellationToken.None);
}

[Benchmark]
public void EvaluateExpression()
{
_ruleAction.Invoke(Context, _actionContext);
}
[Benchmark]
public void EvaluateExpression()
{
_ruleAction.Invoke(Context, _actionContext);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void PerformAction(IContext context, string value1, int value2, decimal value3)
{
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void PerformAction(IContext context, string value1, int value2, decimal value3)
{
}
}
23 changes: 11 additions & 12 deletions bench/NRules.Benchmark/NRules.Benchmark/Meta/BenchmarkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
using NRules.Fluent;
using NRules.Fluent.Dsl;

namespace NRules.Benchmark.Meta
namespace NRules.Benchmark.Meta;

[MemoryDiagnoser]
[ShortRunJob, WarmupCount(1)]
public abstract class BenchmarkBase
{
[MemoryDiagnoser]
[ShortRunJob, WarmupCount(1)]
public abstract class BenchmarkBase
{
protected ISessionFactory Factory { get; set; }
protected ISessionFactory Factory { get; set; }

protected void SetupRule<T>() where T : Rule
{
var repository = new RuleRepository();
repository.Load(x => x.NestedTypes().PrivateTypes().From(xx => xx.Type(typeof(T))));
protected void SetupRule<T>() where T : Rule
{
var repository = new RuleRepository();
repository.Load(x => x.NestedTypes().PrivateTypes().From(xx => xx.Type(typeof(T))));

Factory = repository.Compile();
}
Factory = repository.Compile();
}
}
135 changes: 67 additions & 68 deletions bench/NRules.Benchmark/NRules.Benchmark/Meta/BenchmarkMultipleRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,88 @@
using NRules.RuleModel;
using NRules.RuleModel.Builders;

namespace NRules.Benchmark.Meta
namespace NRules.Benchmark.Meta;

[BenchmarkCategory("Meta")]
public class BenchmarkMultipleRules : BenchmarkBase
{
[BenchmarkCategory("Meta")]
public class BenchmarkMultipleRules : BenchmarkBase
{
private TestFact1[] _facts1;
private TestFact1[] _facts1;

[GlobalSetup]
public void Setup()
[GlobalSetup]
public void Setup()
{
var rules = new List<IRuleDefinition>();
for (int i = 1; i <= RuleCount; i++)
{
var rules = new List<IRuleDefinition>();
for (int i = 1; i <= RuleCount; i++)
{
var rule = BuildRule(i);
rules.Add(rule);
}

var compiler = new RuleCompiler();
Factory = compiler.Compile(rules);

_facts1 = new TestFact1[FactCount];
for (int i = 0; i < FactCount; i++)
{
_facts1[i] = new TestFact1{IntProperty = i};
}
var rule = BuildRule(i);
rules.Add(rule);
}

[Params(10, 100, 1000)]
public int RuleCount { get; set; }

[Params(100)]
public int FactCount { get; set; }
var compiler = new RuleCompiler();
Factory = compiler.Compile(rules);

[Benchmark]
public int Insert()
_facts1 = new TestFact1[FactCount];
for (int i = 0; i < FactCount; i++)
{
var session = Factory.CreateSession();
session.InsertAll(_facts1);
return session.Fire();
_facts1[i] = new TestFact1{IntProperty = i};
}
}

[Benchmark]
public int InsertUpdate()
{
var session = Factory.CreateSession();
session.InsertAll(_facts1);
session.UpdateAll(_facts1);
return session.Fire();
}
[Params(10, 100, 1000)]
public int RuleCount { get; set; }

[Benchmark]
public int InsertRetract()
{
var session = Factory.CreateSession();
session.InsertAll(_facts1);
session.RetractAll(_facts1);
return session.Fire();
}
[Params(100)]
public int FactCount { get; set; }

private class TestFact1
{
public int IntProperty { get; set; }
}
[Benchmark]
public int Insert()
{
var session = Factory.CreateSession();
session.InsertAll(_facts1);
return session.Fire();
}

private IRuleDefinition BuildRule(int index)
{
var builder = new RuleBuilder();
builder.Name($"TestRule{index}");
[Benchmark]
public int InsertUpdate()
{
var session = Factory.CreateSession();
session.InsertAll(_facts1);
session.UpdateAll(_facts1);
return session.Fire();
}

var group = builder.LeftHandSide();
var pattern = group.Pattern(typeof(TestFact1), "fact1");
Expression<Func<TestFact1, bool>> condition = fact1 => fact1.IntProperty % index == 0;
pattern.Condition(condition);
[Benchmark]
public int InsertRetract()
{
var session = Factory.CreateSession();
session.InsertAll(_facts1);
session.RetractAll(_facts1);
return session.Fire();
}

var actions = builder.RightHandSide();
Expression<Action<IContext>> action = ctx => Nothing();
actions.Action(action);
private class TestFact1
{
public int IntProperty { get; set; }
}

return builder.Build();
}
private IRuleDefinition BuildRule(int index)
{
var builder = new RuleBuilder();
builder.Name($"TestRule{index}");

private static void Nothing()
{
}
var group = builder.LeftHandSide();
var pattern = group.Pattern(typeof(TestFact1), "fact1");
Expression<Func<TestFact1, bool>> condition = fact1 => fact1.IntProperty % index == 0;
pattern.Condition(condition);

var actions = builder.RightHandSide();
Expression<Action<IContext>> action = ctx => Nothing();
actions.Action(action);

return builder.Build();
}

private static void Nothing()
{
}
}
Loading

0 comments on commit f84f59d

Please sign in to comment.