Skip to content

Commit d686013

Browse files
committed
Changing default compilation optimization level to release
1 parent 194f961 commit d686013

File tree

5 files changed

+98
-8
lines changed

5 files changed

+98
-8
lines changed

src/WebJobs.Script/Description/DotNet/DotNetCompilationServiceFactory.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,39 @@ ImmutableArray<ScriptType> ICompilationServiceFactory.SupportedScriptTypes
2727
}
2828
}
2929

30-
private static OptimizationLevel OptimizationLevel
30+
private static bool IsLocal => string.IsNullOrEmpty(Environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId));
31+
32+
private static bool IsRemoteDebuggingEnabled => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(EnvironmentSettingNames.RemoteDebuggingPort));
33+
34+
internal static OptimizationLevel OptimizationLevel
3135
{
3236
get
3337
{
3438
if (_optimizationLevel == null)
3539
{
36-
string releaseModeSetting = Environment.GetEnvironmentVariable(DotNetConstants.CompilationReleaseMode);
40+
// Get the release mode setting. If set, this will take priority over environment settings.
41+
string releaseModeSetting = Environment.GetEnvironmentVariable(EnvironmentSettingNames.CompilationReleaseMode);
42+
43+
bool releaseMode;
44+
if (!bool.TryParse(releaseModeSetting, out releaseMode) && !IsLocal && !IsRemoteDebuggingEnabled)
45+
{
46+
// If the release mode setting is not set, we're running in Azure
47+
// and not remote debugging, use release mode.
48+
releaseMode = true;
49+
}
3750

38-
_optimizationLevel = string.Equals(releaseModeSetting, bool.TrueString, StringComparison.OrdinalIgnoreCase)
39-
? OptimizationLevel.Release
40-
: OptimizationLevel.Debug;
51+
_optimizationLevel = releaseMode ? OptimizationLevel.Release : OptimizationLevel.Debug;
4152
}
4253

4354
return _optimizationLevel.Value;
4455
}
4556
}
4657

58+
internal static void SetOptimizationLevel(OptimizationLevel? level)
59+
{
60+
_optimizationLevel = level;
61+
}
62+
4763
public ICompilationService CreateService(ScriptType scriptType, IFunctionMetadataResolver metadataResolver)
4864
{
4965
switch (scriptType)
@@ -53,7 +69,7 @@ public ICompilationService CreateService(ScriptType scriptType, IFunctionMetadat
5369
case ScriptType.FSharp:
5470
return new FSharpCompiler(metadataResolver, OptimizationLevel);
5571
default:
56-
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
72+
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
5773
"The script type {0} is not supported by the {1}", scriptType, typeof(DotNetCompilationServiceFactory).Name));
5874
}
5975
}

src/WebJobs.Script/Description/DotNet/DotNetConstants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ public static class DotNetConstants
1616
public const string RedundantPackageAssemblyReference = "AF005";
1717
public const string InvalidFileMetadataReferenceCode = "AF006";
1818
public const string InvalidEntryPointNameCompilationCode = "AF007";
19-
20-
public const string CompilationReleaseMode = "AzureWebJobsDotNetReleaseCompilation";
2119
}
2220
}

src/WebJobs.Script/EnvironmentSettingNames.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ public static class EnvironmentSettingNames
1111
public const string AzureWebsiteOwnerName = "WEBSITE_OWNER_NAME";
1212
public const string AzureWebsiteInstanceId = "WEBSITE_INSTANCE_ID";
1313
public const string AzureWebsiteSku = "WEBSITE_SKU";
14+
public const string RemoteDebuggingPort = "REMOTEDEBUGGINGPORT";
1415
public const string AzureWebsitePlaceholderMode = "WEBSITE_PLACEHOLDER_MODE";
1516
public const string AzureWebsiteHomePath = "HOME";
1617
public const string AzureWebJobsScriptRoot = "AzureWebJobsScriptRoot";
18+
public const string CompilationReleaseMode = "AzureWebJobsDotNetReleaseCompilation";
1719
}
1820
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Reflection;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Microsoft.Azure.WebJobs.Script.Description;
11+
using Microsoft.CodeAnalysis;
12+
using Xunit;
13+
14+
namespace Microsoft.Azure.WebJobs.Script.Tests.Description.DotNet
15+
{
16+
public class DotNetCompilationServiceFactoryTests
17+
{
18+
[Fact]
19+
public void OptimizationLevel_CompilationReleaseModeSetToTrue_ReturnsRelease()
20+
{
21+
ResetOptimizationFlag();
22+
23+
using (var variables = new TestScopedEnvironmentVariables(EnvironmentSettingNames.CompilationReleaseMode, "True"))
24+
{
25+
Assert.Equal(OptimizationLevel.Release, DotNetCompilationServiceFactory.OptimizationLevel);
26+
}
27+
}
28+
29+
[Fact]
30+
public void OptimizationLevel_CompilationReleaseModeSetToFalse_ReturnsDebug()
31+
{
32+
ResetOptimizationFlag();
33+
34+
using (var variables = new TestScopedEnvironmentVariables(EnvironmentSettingNames.CompilationReleaseMode, "False"))
35+
{
36+
Assert.Equal(OptimizationLevel.Debug, DotNetCompilationServiceFactory.OptimizationLevel);
37+
}
38+
}
39+
40+
[Fact]
41+
public void OptimizationLevel_WhenRemote_ReturnsRelease()
42+
{
43+
ResetOptimizationFlag();
44+
45+
using (var variables = new TestScopedEnvironmentVariables(EnvironmentSettingNames.AzureWebsiteInstanceId, "123"))
46+
{
47+
Assert.Equal(OptimizationLevel.Release, DotNetCompilationServiceFactory.OptimizationLevel);
48+
}
49+
}
50+
51+
[Fact]
52+
public void OptimizationLevel_WhenRemoteWithRemoteDebugging_ReturnsDebug()
53+
{
54+
ResetOptimizationFlag();
55+
56+
var variables = new Dictionary<string, string>
57+
{
58+
{ EnvironmentSettingNames.AzureWebsiteInstanceId, "123" },
59+
{ EnvironmentSettingNames.RemoteDebuggingPort, "1234" }
60+
};
61+
62+
using (new TestScopedEnvironmentVariables(variables))
63+
{
64+
Assert.Equal(OptimizationLevel.Debug, DotNetCompilationServiceFactory.OptimizationLevel);
65+
}
66+
}
67+
68+
private void ResetOptimizationFlag()
69+
{
70+
DotNetCompilationServiceFactory.SetOptimizationLevel(null);
71+
}
72+
}
73+
}

test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@
413413
<Compile Include="CSharpEndToEndTests.cs" />
414414
<Compile Include="CSharpFunctionSignatureTests.cs" />
415415
<Compile Include="Description\DotNet\CSharp\CSharpCompilationTests.cs" />
416+
<Compile Include="Description\DotNet\DotNetCompilationServiceFactoryTests.cs" />
416417
<Compile Include="Description\DotNet\FunctionAssemblyLoaderTests.cs" />
417418
<Compile Include="Description\DotNet\DotNetFunctionInvokerTests.cs" />
418419
<Compile Include="Description\DotNet\PackageManagerTests.cs" />

0 commit comments

Comments
 (0)