File tree Expand file tree Collapse file tree 3 files changed +50
-4
lines changed Expand file tree Collapse file tree 3 files changed +50
-4
lines changed Original file line number Diff line number Diff line change 11using AngleSharp ;
22using AngleSharp . Html . Parser ;
33using BenchmarkDotNet . Attributes ;
4+ using BenchmarkDotNet . Configs ;
5+ using BenchmarkDotNet . Jobs ;
46using BenchmarkDotNet . Running ;
7+ using BenchmarkDotNet . Toolchains . InProcess . NoEmit ;
58
69public static class Program
710{
811 public static void Main ( )
912 {
10- BenchmarkRunner . Run < Realistic > ( ) ;
13+ // Some local environments may run into issues with Windows Defender or
14+ // SentinelOne (and others) when running a benchmark. This ensures we
15+ // keep our toolchain within our process and stops the above apps from blocking
16+ // our benchmark process, but can slow the execution time.
17+ var avSafeConfig = DefaultConfig . Instance
18+ . AddJob (
19+ Job . ShortRun
20+ . WithToolchain ( InProcessNoEmitToolchain . Instance )
21+ . WithIterationCount ( 100 )
22+ ) ;
23+
24+ BenchmarkRunner . Run < Realistic > ( avSafeConfig ) ;
1125 }
1226}
1327
14- [ SimpleJob ( invocationCount : 100 , iterationCount : 100 ) ]
1528[ MemoryDiagnoser ]
1629public class Realistic
1730{
@@ -47,7 +60,7 @@ public void MoveCssInline_AllFlags()
4760
4861<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />
4962<title>PreMailer Benchmark</title>
50-
63+
5164</head>
5265
5366<body bgcolor=""#123"">
Original file line number Diff line number Diff line change 1+ using PreMailer . Net . Extensions ;
12using System ;
23using System . IO ;
34using System . Net ;
@@ -8,7 +9,7 @@ namespace PreMailer.Net.Downloaders
89 public class WebDownloader : IWebDownloader
910 {
1011 private static IWebDownloader _sharedDownloader ;
11-
12+ private const string CssMimeType = "text/css" ;
1213 public static IWebDownloader SharedDownloader
1314 {
1415 get
@@ -29,8 +30,16 @@ public static IWebDownloader SharedDownloader
2930 public string DownloadString ( Uri uri )
3031 {
3132 var request = WebRequest . Create ( uri ) ;
33+ request . Headers . Add ( HttpRequestHeader . Accept , CssMimeType ) ;
34+
3235 using ( var response = request . GetResponse ( ) )
3336 {
37+ // We only support this operation for CSS file/content types coming back
38+ // from the response. If we get something different, throw with the unsupported
39+ // content type in the message
40+ if ( response . ParseContentType ( ) != CssMimeType )
41+ throw new NotSupportedException ( $ "The Uri type is giving a response in unsupported content type '{ response . ContentType } '.") ;
42+
3443 switch ( response )
3544 {
3645 case HttpWebResponse httpWebResponse :
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Net ;
3+
4+ namespace PreMailer . Net . Extensions
5+ {
6+ public static class WebResponseExtensions
7+ {
8+ public static string ParseContentType ( this WebResponse response )
9+ {
10+ if ( response == null )
11+ throw new NullReferenceException ( "Malformed response detected when parsing WebResponse Content-Type" ) ;
12+
13+ if ( string . IsNullOrEmpty ( response . ContentType ) )
14+ throw new NullReferenceException ( "Malformed Content-Type response detected when parsing WebResponse" ) ;
15+
16+ var results = response . ContentType . Split ( ';' ) ;
17+
18+ if ( results . Length == 0 )
19+ throw new FormatException ( "Malformed Content-Type response detected when parsing WebResponse" ) ;
20+
21+ return results [ 0 ] ;
22+ }
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments