Skip to content

Commit

Permalink
- Fixed importer to get benchmark working
Browse files Browse the repository at this point in the history
- Fixed Benchmark project
  • Loading branch information
twenzel committed Jul 27, 2018
1 parent 29cc162 commit d88da06
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/dotless.Benchmark/Input/functions.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#functions {
color: color("evil red"); // #660000
width: increment(15);
height: undefined("self");
border-width: add(2, 3);
color: color("#660000"); //
width: increment(15);
height: undefined("self");
border-width: add(2, 3);
}
40 changes: 25 additions & 15 deletions src/dotless.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using dotless.Core;

namespace dotlessjs.Compiler
namespace dotless.Benchmark
{
using System.Collections.Generic;

Expand All @@ -23,23 +23,33 @@ public static void Main(string[] args)
(file, engine) => Enumerable
.Range(0, rounds)
.Select(i =>
{
var starttime = DateTime.Now;
engine.TransformToCss(contents[file], file);
var duration = (DateTime.Now - starttime);
return duration.Milliseconds;
})
{
engine.ResetImports();
var starttime = DateTime.Now;
engine.TransformToCss(contents[file], file);
var duration = (DateTime.Now - starttime);
return duration.Milliseconds;
})
.Sum();

Console.WriteLine("Press Enter to begin benchmark");
Console.ReadLine();

Console.WriteLine("Running each test {0} times\n", rounds);

RunBenchmark(files, contents, rounds, runTest);

while(Console.ReadKey().Key == ConsoleKey.R)
RunBenchmark(files, contents, rounds, runTest);

}

private static void RunBenchmark(string[] files, Dictionary<string, string> contents, int rounds, Func<string, ILessEngine, int> runTest)
{
var engines = new ILessEngine[]
{
{
new LessEngine()
};
};


Console.Write(" File | Size |");
Expand All @@ -49,33 +59,33 @@ public static void Main(string[] args)
Console.Write('|');
}
Console.WriteLine();
Console.WriteLine(new string('-', 35 + 30*engines.Length));
Console.WriteLine(new string('-', 35 + 30 * engines.Length));

foreach (var file in files)
{
var size = rounds*contents[file].Length/1024d;
var size = rounds * contents[file].Length / 1024d;
Console.Write("{0} | {1,8:#,##0.00} Kb | ", file.PadRight(18), size);

var times = new List<double>();
foreach (var engine in engines)
{
try
{
var time = runTest(file, engine)/1000d;
var time = runTest(file, engine) / 1000d;
times.Add(time);
Console.Write("{0,8:#.00} s {1,10:#,##0.00} Kb/s | ", time, size/time);
Console.Write("{0,8:#.00} s {1,10:#,##0.00} Kb/s | ", time, size / time);
}
catch
{
Console.Write("Failied | ");
Console.Write("Failed | ");
}
}
if (times.Count == 2)
Console.Write("{0,4:0.#}x", times[0] / times[1]);
Console.WriteLine();
}

// Console.Read();
Console.WriteLine("Done. Press 'r' to repeat or any key to exit");
}
}
}
3 changes: 2 additions & 1 deletion src/dotless.Benchmark/dotless.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>net461</TargetFrameworks>
<Company>dotless project</Company>
<Product>dotless.Benchmark</Product>
<Copyright>Copyright © dotless 2010-2012</Copyright>
<Version>1.5.3.0</Version>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/dotless.Core/Engine/LessEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ public string TransformToCss(string source, string fileName)

public IEnumerable<string> GetImports()
{
return Parser.Importer.Imports.Distinct();
return Parser.Importer.GetImports();
}

public void ResetImports()
{
Parser.Importer.Imports.Clear();
Parser.Importer.ResetImports();
}

}
Expand Down
16 changes: 11 additions & 5 deletions src/dotless.Core/Importers/IImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public interface IImporter
/// </summary>
ImportAction Import(Import import);

/// <summary>
/// A list of imports
/// </summary>
List<string> Imports { get; }

/// <summary>
/// A method set by the parser implementation in order to get a new parser for use in importing
/// </summary>
Expand All @@ -39,6 +34,17 @@ public interface IImporter
string CurrentDirectory { get; set; }

IDisposable BeginScope(Import parent);

/// <summary>
/// Resets the imports.
/// </summary>
void ResetImports();

/// <summary>
/// Gets the all already imported files
/// </summary>
/// <returns></returns>
IEnumerable<string> GetImports();
}

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions src/dotless.Core/Importers/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ public string AlterUrl(string url, List<string> pathList)
return url;
}

public void ResetImports()
{
Imports.Clear();
_rawImports.Clear();
}

public IEnumerable<string> GetImports()
{
return Imports.Distinct();
}

private class ImportScope : IDisposable {
private readonly Importer importer;

Expand Down

0 comments on commit d88da06

Please sign in to comment.