Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FeatureParser performance #505

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
### Fixed
- Cache the NUnit feature scan results to improve performance on large solutions ([503](https://github.com/picklesdoc/pickles/pull/503)) (by [@tlecomte](https://github.com/tlecomte))
- Use lookups in JSONDocumentation Builder to improve performance on large solutions ([504](https://github.com/picklesdoc/pickles/pull/504)) (by [@tlecomte](https://github.com/tlecomte))
- Cache LanguageServices and GherkinDialectProvider in FeatureParser to improve performance on large solutions ([505](https://github.com/picklesdoc/pickles/pull/505)) (by [@tlecomte](https://github.com/tlecomte))

### Security

Expand Down
3 changes: 2 additions & 1 deletion src/Pickles/Pickles.Test/ObjectModel/Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ internal Mapper CreateMapper(string defaultLanguage = "en")

internal Mapper CreateMapper(IConfiguration configuration, string defaultLanguage = "en")
{
var mapper = new Mapper(configuration, defaultLanguage);
var languageServices = new LanguageServices(defaultLanguage);
var mapper = new Mapper(configuration, languageServices);
return mapper;
}

Expand Down
28 changes: 26 additions & 2 deletions src/Pickles/Pickles/FeatureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Gherkin;
using PicklesDoc.Pickles.ObjectModel;

using TextReader = System.IO.TextReader;
Expand All @@ -32,6 +34,10 @@ public class FeatureParser

private readonly DescriptionProcessor descriptionProcessor = new DescriptionProcessor();

private readonly LanguageServicesRegistry languageServicesRegistry = new LanguageServicesRegistry();

private readonly IDictionary<string, IGherkinDialectProvider> dialectProviderCache = new Dictionary<string, IGherkinDialectProvider>();

public FeatureParser(IConfiguration configuration)
{
this.configuration = configuration;
Expand All @@ -41,14 +47,16 @@ public Feature Parse(TextReader featureFileReader)
{
var language = this.DetermineLanguage();
var gherkinParser = new Gherkin.Parser();
var dialectProvider = this.GetDialectProvider(language);

try
{
Gherkin.Ast.GherkinDocument gherkinDocument = gherkinParser.Parse(
new Gherkin.TokenScanner(featureFileReader),
new Gherkin.TokenMatcher(new CultureAwareDialectProvider(language)));
new Gherkin.TokenMatcher(dialectProvider));

Feature result = new Mapper(this.configuration, gherkinDocument.Feature.Language).MapToFeature(gherkinDocument);
var languageServices = this.languageServicesRegistry.GetLanguageServicesForLanguage(gherkinDocument.Feature.Language);
Feature result = new Mapper(this.configuration, languageServices).MapToFeature(gherkinDocument);
result = this.RemoveFeatureWithExcludeTags(result);

if (result != null)
Expand All @@ -62,6 +70,22 @@ public Feature Parse(TextReader featureFileReader)
}
}

private IGherkinDialectProvider GetDialectProvider(string language)
{
IGherkinDialectProvider dialectProvider;
if (this.dialectProviderCache.ContainsKey(language))
{
dialectProvider = this.dialectProviderCache[language];
}
else
{
dialectProvider = new CultureAwareDialectProvider(language);
this.dialectProviderCache[language] = dialectProvider;
}

return dialectProvider;
}

private string DetermineLanguage()
{
string language = null;
Expand Down
15 changes: 14 additions & 1 deletion src/Pickles/Pickles/LanguageServicesRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@
// limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;

namespace PicklesDoc.Pickles
{
public class LanguageServicesRegistry : ILanguageServicesRegistry
{
private static readonly IDictionary<string, ILanguageServices> LanguageServices = new Dictionary<string, ILanguageServices>();

public ILanguageServices GetLanguageServicesForLanguage(string language)
{
return new LanguageServices(language ?? DefaultLanguage);
language = language ?? DefaultLanguage;

if (LanguageServices.ContainsKey(language))
{
return LanguageServices[language];
}

var services = new LanguageServices(language);
LanguageServices[language] = services;
return services;
}

public string DefaultLanguage
Expand Down
4 changes: 2 additions & 2 deletions src/Pickles/Pickles/ObjectModel/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public class Mapper

private readonly ILanguageServices languageServices;

public Mapper(IConfiguration configuration, string featureLanguage)
public Mapper(IConfiguration configuration, ILanguageServices languageServices)
{
this.configuration = configuration;
this.languageServices = new LanguageServices(featureLanguage);
this.languageServices = languageServices;
}

public string MapToString(G.TableCell cell)
Expand Down