2020-12-23 00:34:10 -05:00
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
using BenchmarkDotNet.Attributes;
|
2020-11-16 03:36:35 -05:00
|
|
|
|
using BenchmarkDotNet.Running;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using RulesEngine.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace RulesEngineBenchmark
|
|
|
|
|
{
|
|
|
|
|
[MemoryDiagnoser]
|
|
|
|
|
public class REBenchmark
|
|
|
|
|
{
|
|
|
|
|
private readonly RulesEngine.RulesEngine rulesEngine;
|
|
|
|
|
private readonly object ruleInput;
|
|
|
|
|
private readonly List<WorkflowRules> workflows;
|
2020-12-23 00:34:10 -05:00
|
|
|
|
|
|
|
|
|
private class ListItem
|
2020-11-16 03:36:35 -05:00
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public string Value { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public REBenchmark()
|
|
|
|
|
{
|
|
|
|
|
var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "NestedInputDemo.json", SearchOption.AllDirectories);
|
|
|
|
|
if (files == null || files.Length == 0)
|
2020-12-23 00:34:10 -05:00
|
|
|
|
{
|
2020-11-16 03:36:35 -05:00
|
|
|
|
throw new Exception("Rules not found.");
|
2020-12-23 00:34:10 -05:00
|
|
|
|
}
|
2020-11-16 03:36:35 -05:00
|
|
|
|
|
|
|
|
|
var fileData = File.ReadAllText(files[0]);
|
|
|
|
|
workflows = JsonConvert.DeserializeObject<List<WorkflowRules>>(fileData);
|
2020-12-23 00:34:10 -05:00
|
|
|
|
|
|
|
|
|
rulesEngine = new RulesEngine.RulesEngine(workflows.ToArray(), null, new ReSettings {
|
2020-11-16 03:36:35 -05:00
|
|
|
|
EnableFormattedErrorMessage = false,
|
|
|
|
|
EnableLocalParams = false
|
|
|
|
|
});
|
|
|
|
|
|
2020-12-23 00:34:10 -05:00
|
|
|
|
ruleInput = new {
|
2020-11-16 03:36:35 -05:00
|
|
|
|
SimpleProp = "simpleProp",
|
2020-12-23 00:34:10 -05:00
|
|
|
|
NestedProp = new {
|
2020-11-16 03:36:35 -05:00
|
|
|
|
SimpleProp = "nestedSimpleProp",
|
|
|
|
|
ListProp = new List<ListItem>
|
|
|
|
|
{
|
|
|
|
|
new ListItem
|
|
|
|
|
{
|
|
|
|
|
Id = 1,
|
|
|
|
|
Value = "first"
|
|
|
|
|
},
|
|
|
|
|
new ListItem
|
|
|
|
|
{
|
|
|
|
|
Id = 2,
|
|
|
|
|
Value = "second"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Params(1000, 10000)]
|
|
|
|
|
public int N;
|
|
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
|
public void RuleExecutionDefault()
|
|
|
|
|
{
|
|
|
|
|
foreach (var workflow in workflows)
|
|
|
|
|
{
|
2020-12-23 00:34:10 -05:00
|
|
|
|
_ = rulesEngine.ExecuteAllRulesAsync(workflow.WorkflowName, ruleInput).Result;
|
2020-11-16 03:36:35 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-23 00:34:10 -05:00
|
|
|
|
public class Program
|
2020-11-16 03:36:35 -05:00
|
|
|
|
{
|
2020-12-23 00:34:10 -05:00
|
|
|
|
public static void Main(string[] args)
|
2020-11-16 03:36:35 -05:00
|
|
|
|
{
|
2020-12-23 00:34:10 -05:00
|
|
|
|
_ = BenchmarkRunner.Run<REBenchmark>();
|
2020-11-16 03:36:35 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|