forked from managedcode/graphrag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunityBuilderBenchmarks.cs
More file actions
104 lines (89 loc) · 3.2 KB
/
CommunityBuilderBenchmarks.cs
File metadata and controls
104 lines (89 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
using GraphRag.Community;
using GraphRag.Config;
using GraphRag.Entities;
using GraphRag.Relationships;
namespace ManagedCode.GraphRag.Benchmarks.Community;
[MemoryDiagnoser]
public class CommunityBuilderBenchmarks
{
private EntityRecord[] _entities = null!;
private RelationshipRecord[] _relationships = null!;
private ClusterGraphConfig _labelPropagationConfig = null!;
private ClusterGraphConfig _connectedComponentsConfig = null!;
[Params(100, 1_000, 5_000)]
public int NodeCount { get; set; }
[GlobalSetup]
public void Setup()
{
_labelPropagationConfig = new ClusterGraphConfig
{
Algorithm = CommunityDetectionAlgorithm.FastLabelPropagation,
MaxIterations = 20,
MaxClusterSize = 25,
Seed = 42
};
_connectedComponentsConfig = new ClusterGraphConfig
{
Algorithm = CommunityDetectionAlgorithm.ConnectedComponents,
MaxClusterSize = 25,
Seed = 42
};
(_entities, _relationships) = GenerateGraph(NodeCount, avgEdgesPerNode: 5);
}
[Benchmark(Baseline = true)]
public IReadOnlyList<CommunityRecord> FastLabelPropagation()
{
return CommunityBuilder.Build(_entities, _relationships, _labelPropagationConfig);
}
[Benchmark]
public IReadOnlyList<CommunityRecord> ConnectedComponents()
{
return CommunityBuilder.Build(_entities, _relationships, _connectedComponentsConfig);
}
private static (EntityRecord[] Entities, RelationshipRecord[] Relationships) GenerateGraph(
int nodeCount,
int avgEdgesPerNode)
{
var random = new Random(42);
var entities = new EntityRecord[nodeCount];
for (var i = 0; i < nodeCount; i++)
{
entities[i] = new EntityRecord(
Id: $"entity-{i}",
HumanReadableId: i,
Title: $"Entity_{i}",
Type: "ENTITY",
Description: $"Description for entity {i}",
TextUnitIds: ImmutableArray.Create($"tu-{i}"),
Frequency: 1,
Degree: 0,
X: 0,
Y: 0);
}
var totalEdges = nodeCount * avgEdgesPerNode;
var relationships = new List<RelationshipRecord>(totalEdges);
for (var i = 0; i < totalEdges; i++)
{
var sourceIdx = random.Next(nodeCount);
var targetIdx = random.Next(nodeCount);
if (sourceIdx == targetIdx)
{
targetIdx = (targetIdx + 1) % nodeCount;
}
relationships.Add(new RelationshipRecord(
Id: $"rel-{i}",
HumanReadableId: i,
Source: entities[sourceIdx].Title,
Target: entities[targetIdx].Title,
Type: "RELATED_TO",
Description: null,
Weight: random.NextDouble(),
CombinedDegree: 2,
TextUnitIds: ImmutableArray.Create($"tu-{sourceIdx}", $"tu-{targetIdx}"),
Bidirectional: false));
}
return (entities, relationships.ToArray());
}
}