using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using PersistentMap; // Ensure your PersistentMap namespace is included here // using MyProject.Collections; [MemoryDiagnoser] public class ImmutableBenchmark { [Params(10, 100, 1000)] public int N { get; set; } [Params(1000)] public int CollectionSize { get; set; } private ImmutableDictionary _immutableDict; private ImmutableSortedDictionary _immutableSortedDict; // 1. Add field for your map private PersistentMap _persistentMap; private string[] _searchKeys; [GlobalSetup] public void Setup() { var random = new Random(42); var data = new Dictionary(); while (data.Count < CollectionSize) { string key = GenerateRandomString(random, N); if (!data.ContainsKey(key)) { data[key] = "value"; } } _immutableDict = data.ToImmutableDictionary(); _immutableSortedDict = data.ToImmutableSortedDictionary(); // 2. Initialize your map. // ASSUMPTION: Standard immutable pattern (Add returns new instance). // Adjust if you have a bulk loader like .ToPersistentMap() or a constructor. _persistentMap = PersistentMap.Empty(new UnicodeStrategy()); foreach (var kvp in data) { _persistentMap = _persistentMap.Set(kvp.Key, kvp.Value); } _searchKeys = data.Keys.ToArray(); } [Benchmark(Baseline = true)] public string ImmutableDict_Lookup() { var key = _searchKeys[CollectionSize / 2]; _immutableDict.TryGetValue(key, out var value); return value; } [Benchmark] public string ImmutableSortedDict_Lookup() { var key = _searchKeys[CollectionSize / 2]; _immutableSortedDict.TryGetValue(key, out var value); return value; } // 3. Add the benchmark case [Benchmark] public string PersistentMap_Lookup() { var key = _searchKeys[CollectionSize / 2]; // Adjust API call if your map uses a different method (e.g. Find, Get, indexer) _persistentMap.TryGetValue(key, out var value); return value; } private string GenerateRandomString(Random rng, int length) { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var buffer = new char[length]; for (int i = 0; i < length; i++) { buffer[i] = chars[rng.Next(chars.Length)]; } return new string(buffer); } }