Skip to content
Open

Patch-1 #3161

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions benchmark/Microsoft.IdentityModel.Benchmarks/ExpiredCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using BenchmarkDotNet.Attributes;

namespace Microsoft.IdentityModel.Benchmarks
{
// dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ExpiredCheck*

public class ExpiredCheck
{
private ConcurrentDictionary<int, DateTime> _map1;
private ConcurrentDictionary<int, DateTime> _map2;

[GlobalSetup]
public void Setup()
{
_map1 = new ConcurrentDictionary<int, DateTime>();
_map2 = new ConcurrentDictionary<int, DateTime>();

DateTime now = DateTime.UtcNow;
for (int i = 0; i < 100000; i++)
{
_map1.TryAdd(i, now);
_map2.TryAdd(i, now);
}
}

[Benchmark]
public void RemoveUsingDateTimeUtcNow()
{
foreach (var entry in _map1)
{
if (entry.Value < DateTime.UtcNow)
{
_map1.TryRemove(entry.Key, out _);
}
}
}

[Benchmark]
public void RemoveUsingDateTimeVariable()
{
DateTime now = DateTime.UtcNow;
foreach (var entry in _map2)
{
if (entry.Value < now)
{
_map2.TryRemove(entry.Key, out _);
}

}
}
}
}
Loading