Skip to content

Commit 195cfd6

Browse files
Support Windows 11 in OsBrandStringHelper
1 parent ed53a3f commit 195cfd6

File tree

2 files changed

+71
-48
lines changed

2 files changed

+71
-48
lines changed

src/BenchmarkDotNet/Environments/OsBrandStringHelper.cs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
@@ -10,7 +11,8 @@ namespace BenchmarkDotNet.Environments
1011
public class OsBrandStringHelper
1112
{
1213
// See https://en.wikipedia.org/wiki/Ver_(command)
13-
// See https://docs.microsoft.com/en-us/windows/release-information/
14+
// See https://docs.microsoft.com/en-us/windows/release-health/release-information
15+
// See https://docs.microsoft.com/en-us/windows/release-health/windows11-release-information
1416
private static readonly Dictionary<string, string> WindowsBrandVersions = new Dictionary<string, string>
1517
{
1618
{ "1.04", "1.0" },
@@ -99,22 +101,24 @@ public class OsBrandStringHelper
99101
{ "10.0.19041", "10 20H1 [2004, May 2020 Update]" },
100102
{ "10.0.19042", "10 20H2 [20H2, October 2020 Update]" },
101103
{ "10.0.19043", "10 21H1 [21H1, May 2021 Update]" },
102-
{ "10.0.19044", "10 21H2 [21H2]" }
104+
{ "10.0.19044", "10 21H2 [21H2, November 2021 Update]" },
105+
{ "10.0.22000", "11 21H2 [21H2]" },
103106
};
104107

105-
private class Windows10Version
108+
private class Windows1XVersion
106109
{
107-
private string Version { get; }
108-
[NotNull] private string CodeName { get; }
109-
[NotNull] private string MarketingName { get; }
110+
[CanBeNull] private string CodeVersion { get; }
111+
[CanBeNull] private string CodeName { get; }
112+
[CanBeNull] private string MarketingName { get; }
110113
private int BuildNumber { get; }
111114

112-
[NotNull] private string ShortifiedCodeName => CodeName.Replace(" ", "");
113-
[NotNull] private string ShortifiedMarketingName => MarketingName.Replace(" ", "");
115+
[NotNull] private string MarketingNumber => BuildNumber >= 22000 ? "11" : "10";
116+
[CanBeNull] private string ShortifiedCodeName => CodeName?.Replace(" ", "");
117+
[CanBeNull] private string ShortifiedMarketingName => MarketingName?.Replace(" ", "");
114118

115-
private Windows10Version(string version, [NotNull] string codeName, [NotNull] string marketingName, int buildNumber)
119+
private Windows1XVersion([CanBeNull] string codeVersion, [CanBeNull] string codeName, [CanBeNull] string marketingName, int buildNumber)
116120
{
117-
Version = version;
121+
CodeVersion = codeVersion;
118122
CodeName = codeName;
119123
MarketingName = marketingName;
120124
BuildNumber = buildNumber;
@@ -129,31 +133,46 @@ private string ToFullVersion([CanBeNull] int? ubr = null)
129133
// When people past in on GitHub, it can be a reason of an ugly horizontal scrollbar.
130134
// To avoid this, we are trying to minimize this line and use the minimum possible number of characters.
131135
public string ToPrettifiedString([CanBeNull] int? ubr)
132-
=> Version == ShortifiedCodeName
133-
? $"{ToFullVersion(ubr)} ({Collapse(Version, ShortifiedMarketingName)})"
134-
: $"{ToFullVersion(ubr)} ({Collapse(Version, ShortifiedMarketingName, ShortifiedCodeName)})";
136+
=> CodeVersion == ShortifiedCodeName
137+
? $"{MarketingNumber} ({Collapse(ToFullVersion(ubr), CodeVersion, ShortifiedMarketingName)})"
138+
: $"{MarketingNumber} ({Collapse(ToFullVersion(ubr), CodeVersion, ShortifiedMarketingName, ShortifiedCodeName)})";
135139

136140
// See https://en.wikipedia.org/wiki/Windows_10_version_history
137-
private static readonly List<Windows10Version> WellKnownVersions = new List<Windows10Version>
141+
// See https://en.wikipedia.org/wiki/Windows_11_version_history
142+
private static readonly List<Windows1XVersion> WellKnownVersions = new()
138143
{
139-
new Windows10Version("1507", "Threshold 1", "RTM", 10240),
140-
new Windows10Version("1511", "Threshold 2", "November Update", 10586),
141-
new Windows10Version("1607", "Redstone 1", "Anniversary Update", 14393),
142-
new Windows10Version("1703", "Redstone 2", "Creators Update", 15063),
143-
new Windows10Version("1709", "Redstone 3", "Fall Creators Update", 16299),
144-
new Windows10Version("1803", "Redstone 4", "April 2018 Update", 17134),
145-
new Windows10Version("1809", "Redstone 5", "October 2018 Update", 17763),
146-
new Windows10Version("1903", "19H1", "May 2019 Update", 18362),
147-
new Windows10Version("1909", "19H2", "November 2019 Update", 18363),
148-
new Windows10Version("2004", "20H1", "May 2020 Update", 19041),
149-
new Windows10Version("20H2", "20H2", "October 2020 Update", 19042),
150-
new Windows10Version("21H1", "21H1", "May 2021 Update", 19043),
151-
new Windows10Version("21H2", "21H2", "", 19044), // The markingName is not announced yet
144+
// Windows 10
145+
new Windows1XVersion("1507", "Threshold 1", "RTM", 10240),
146+
new Windows1XVersion("1511", "Threshold 2", "November Update", 10586),
147+
new Windows1XVersion("1607", "Redstone 1", "Anniversary Update", 14393),
148+
new Windows1XVersion("1703", "Redstone 2", "Creators Update", 15063),
149+
new Windows1XVersion("1709", "Redstone 3", "Fall Creators Update", 16299),
150+
new Windows1XVersion("1803", "Redstone 4", "April 2018 Update", 17134),
151+
new Windows1XVersion("1809", "Redstone 5", "October 2018 Update", 17763),
152+
new Windows1XVersion("1903", "19H1", "May 2019 Update", 18362),
153+
new Windows1XVersion("1909", "19H2", "November 2019 Update", 18363),
154+
new Windows1XVersion("2004", "20H1", "May 2020 Update", 19041),
155+
new Windows1XVersion("20H2", "20H2", "October 2020 Update", 19042),
156+
new Windows1XVersion("21H1", "21H1", "May 2021 Update", 19043),
157+
new Windows1XVersion("21H2", "21H2", "November 2021 Update", 19044),
158+
new Windows1XVersion("21H2", "21H2", "November 2021 Update", 19044),
159+
// Windows 11
160+
new Windows1XVersion("21H2", "21H2", null, 22000),
152161
};
153162

154163
[CanBeNull]
155-
public static Windows10Version Resolve([NotNull] string osVersion)
156-
=> WellKnownVersions.FirstOrDefault(v => osVersion == $"10.0.{v.BuildNumber}");
164+
public static Windows1XVersion Resolve([NotNull] string osVersionString)
165+
{
166+
var windows1XVersion = WellKnownVersions.FirstOrDefault(v => osVersionString == $"10.0.{v.BuildNumber}");
167+
if (windows1XVersion != null)
168+
return windows1XVersion;
169+
if (Version.TryParse(osVersionString, out var osVersion))
170+
{
171+
if (osVersion.Major == 10 && osVersion.Minor == 0)
172+
return new Windows1XVersion(null, null, null, osVersion.Build);
173+
}
174+
return null;
175+
}
157176
}
158177

159178
/// <summary>
@@ -174,9 +193,9 @@ public static string Prettify([NotNull] string osName, [NotNull] string osVersio
174193
[NotNull]
175194
private static string PrettifyWindows([NotNull] string osVersion, [CanBeNull] int? windowsUbr)
176195
{
177-
var windows10Version = Windows10Version.Resolve(osVersion);
178-
if (windows10Version != null)
179-
return "Windows " + windows10Version.ToPrettifiedString(windowsUbr);
196+
var windows1XVersion = Windows1XVersion.Resolve(osVersion);
197+
if (windows1XVersion != null)
198+
return "Windows " + windows1XVersion.ToPrettifiedString(windowsUbr);
180199

181200
string brandVersion = WindowsBrandVersions.GetValueOrDefault(osVersion);
182201
string completeOsVersion = windowsUbr != null && osVersion.Count(c => c == '.') == 2

tests/BenchmarkDotNet.Tests/Environments/OsBrandStringTests.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,34 @@ private void Check(string actual, string expected)
2121
// When people past in on GitHub, it can be a reason of an ugly horizontal scrollbar.
2222
// To avoid this, we are trying to minimize this line and use the minimum possible number of characters.
2323
// In this test, we check that the length of the OS brand string for typical Windows versions
24-
// is less than 60 characters.
25-
Assert.True(actual.Length <= 60);
24+
// is less than 61 characters.
25+
const int maxLength = 61;
26+
Assert.True(actual.Length <= maxLength, $"The brand string name length should be <= {maxLength}");
2627
}
2728

2829
[Theory]
2930
[InlineData("6.3.9600", "Windows 8.1 (6.3.9600)")]
30-
[InlineData("10.0.14393", "Windows 10.0.14393 (1607/AnniversaryUpdate/Redstone1)")]
31+
[InlineData("10.0.14393", "Windows 10 (10.0.14393/1607/AnniversaryUpdate/Redstone1)")]
3132
public void WindowsIsPrettified(string originalVersion, string prettifiedName)
3233
=> Check(OsBrandStringHelper.Prettify("Windows", originalVersion), prettifiedName);
3334

3435
[Theory]
35-
[InlineData("10.0.10240", 17797, "Windows 10.0.10240.17797 (1507/RTM/Threshold1)")]
36-
[InlineData("10.0.10586", 1478, "Windows 10.0.10586.1478 (1511/NovemberUpdate/Threshold2)")]
37-
[InlineData("10.0.14393", 2156, "Windows 10.0.14393.2156 (1607/AnniversaryUpdate/Redstone1)")]
38-
[InlineData("10.0.15063", 997, "Windows 10.0.15063.997 (1703/CreatorsUpdate/Redstone2)")]
39-
[InlineData("10.0.16299", 334, "Windows 10.0.16299.334 (1709/FallCreatorsUpdate/Redstone3)")]
40-
[InlineData("10.0.17134", 48, "Windows 10.0.17134.48 (1803/April2018Update/Redstone4)")]
41-
[InlineData("10.0.17763", 1, "Windows 10.0.17763.1 (1809/October2018Update/Redstone5)")]
42-
[InlineData("10.0.18362", 693, "Windows 10.0.18362.693 (1903/May2019Update/19H1)")]
43-
[InlineData("10.0.18363", 657, "Windows 10.0.18363.657 (1909/November2019Update/19H2)")]
44-
[InlineData("10.0.19041", 1, "Windows 10.0.19041.1 (2004/May2020Update/20H1)")]
45-
[InlineData("10.0.19042", 746, "Windows 10.0.19042.746 (20H2/October2020Update)")]
46-
[InlineData("10.0.19043", 964, "Windows 10.0.19043.964 (21H1/May2021Update)")]
47-
[InlineData("10.0.19044", 1147, "Windows 10.0.19044.1147 (21H2)")]
36+
[InlineData("10.0.10240", 17797, "Windows 10 (10.0.10240.17797/1507/RTM/Threshold1)")]
37+
[InlineData("10.0.10586", 1478, "Windows 10 (10.0.10586.1478/1511/NovemberUpdate/Threshold2)")]
38+
[InlineData("10.0.14393", 2156, "Windows 10 (10.0.14393.2156/1607/AnniversaryUpdate/Redstone1)")]
39+
[InlineData("10.0.15063", 997, "Windows 10 (10.0.15063.997/1703/CreatorsUpdate/Redstone2)")]
40+
[InlineData("10.0.16299", 334, "Windows 10 (10.0.16299.334/1709/FallCreatorsUpdate/Redstone3)")]
41+
[InlineData("10.0.17134", 48, "Windows 10 (10.0.17134.48/1803/April2018Update/Redstone4)")]
42+
[InlineData("10.0.17763", 1, "Windows 10 (10.0.17763.1/1809/October2018Update/Redstone5)")]
43+
[InlineData("10.0.18362", 693, "Windows 10 (10.0.18362.693/1903/May2019Update/19H1)")]
44+
[InlineData("10.0.18363", 657, "Windows 10 (10.0.18363.657/1909/November2019Update/19H2)")]
45+
[InlineData("10.0.19041", 1, "Windows 10 (10.0.19041.1/2004/May2020Update/20H1)")]
46+
[InlineData("10.0.19042", 746, "Windows 10 (10.0.19042.746/20H2/October2020Update)")]
47+
[InlineData("10.0.19043", 964, "Windows 10 (10.0.19043.964/21H1/May2021Update)")]
48+
[InlineData("10.0.19044", 1147, "Windows 10 (10.0.19044.1147/21H2/November2021Update)")]
49+
[InlineData("10.0.19099", 1729, "Windows 10 (10.0.19099.1729)")]
50+
[InlineData("10.0.22000", 348, "Windows 11 (10.0.22000.348/21H2)")]
51+
[InlineData("10.0.22518", 1012, "Windows 11 (10.0.22518.1012)")]
4852
public void WindowsWithUbrIsPrettified(string originalVersion, int ubr, string prettifiedName)
4953
=> Check(OsBrandStringHelper.Prettify("Windows", originalVersion, ubr), prettifiedName);
5054

0 commit comments

Comments
 (0)