Skip to content

Commit 5bb0e0a

Browse files
committed
Reset counters on the static fields before testing
1 parent b69193f commit 5bb0e0a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public void FileDescriptor_CreateMessageWithDeepDependencies_BuildFromByteString
4343

4444
// uncomment below to find how much slower it is without caching
4545
//FileDescriptor.DisableExtensionCaching();
46+
#if DEBUG
47+
FileDescriptor.ResetCounters();
48+
#endif
4649

4750
var stopwatchCached = Stopwatch.StartNew();
4851

@@ -56,17 +59,17 @@ public void FileDescriptor_CreateMessageWithDeepDependencies_BuildFromByteString
5659

5760
#if DEBUG
5861
// For performance reaons, benachmarking is only enabled in Debug builds
59-
Assert.AreEqual(402, FileDescriptor.GetAllExtensionsCount);
60-
Assert.AreEqual(573, FileDescriptor.GetAllGeneratedExtensionsCount);
61-
Assert.AreEqual(399, FileDescriptor.GetAllDependedExtensionsCount);
62-
Assert.AreEqual(39, FileDescriptor.GetAllDependedExtensionsFromMessageCount);
63-
Assert.AreEqual(118, FileDescriptor.TotalReturnedExtensionsCount);
62+
Assert.AreEqual(393, FileDescriptor.GetAllExtensionsCount);
63+
Assert.AreEqual(394, FileDescriptor.GetAllGeneratedExtensionsCount);
64+
Assert.AreEqual(392, FileDescriptor.GetAllDependedExtensionsCount);
65+
Assert.AreEqual(0, FileDescriptor.GetAllDependedExtensionsFromMessageCount);
66+
Assert.AreEqual(0, FileDescriptor.TotalReturnedExtensionsCount);
6467
#else
6568
Console.WriteLine("Benchmarking is only enabled for Debug build: Cannot verify expected method call and returned Extension count");
6669
#endif
6770

6871
// actually test that the performance increase is significant
69-
Assert.Less(stopwatchCached.Elapsed, TimeSpan.FromSeconds(2), "Extension performance was not as low as expected - please improve performance of extension resolution");
72+
Assert.Less(stopwatchCached.Elapsed, TimeSpan.FromSeconds(1), "Extension performance was not as low as expected - please improve performance of extension resolution");
7073
}
7174

7275
[Test]

csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,27 @@ static FileDescriptor()
7272
private static bool extensionCachingEnabled = true;
7373

7474
/// <summary>
75-
/// Enables caching of discovered extensions. Greatly improves performance at the tradeoff of memory usage.
75+
/// Disables caching of discovered extensions. Greatly reduces performance in highly nested protos but lowers the memory usage.
7676
/// </summary>
7777
public static void DisableExtensionCaching()
7878
{
7979
extensionCachingEnabled = false;
8080
}
8181

8282
#if WITH_BENCHMARKING
83+
84+
/// <summary>
85+
/// Resets the benchmarking counters.
86+
/// </summary>
87+
public static void ResetCounters()
88+
{
89+
GetAllExtensionsCount = 0;
90+
TotalReturnedExtensionsCount = 0;
91+
GetAllGeneratedExtensionsCount = 0;
92+
GetAllDependedExtensionsCount = 0;
93+
GetAllDependedExtensionsFromMessageCount = 0;
94+
}
95+
8396
internal static long GetAllExtensionsCount { get; private set; }
8497
internal static long TotalReturnedExtensionsCount { get; private set;}
8598
internal static long GetAllGeneratedExtensionsCount { get; private set; }

0 commit comments

Comments
 (0)