コード例 #1
0
ファイル: TextEngine.cs プロジェクト: Streammz/morestachio
        public MorestachioDocumentInfo TokenizeAndParse(ParserOptions parsingOptions)
        {
            var errors   = new List <IMorestachioError>();
            var profiler = new PerformanceProfiler(parsingOptions.ProfileExecution);
            Queue <TokenPair> tokens;

            using (profiler.Begin("Tokenize"))
            {
                tokens = new Queue <TokenPair>(Tokenizer.Tokenize(parsingOptions, errors, profiler));
            }

            //if there are any errors do not parse the template
            MorestachioDocumentInfo documentInfo;

            if (errors.Any())
            {
                documentInfo = new MorestachioDocumentInfo(parsingOptions, null, errors);
            }
            else
            {
                documentInfo = Parse(parsingOptions, tokens);
            }
            documentInfo.Profiler = profiler;
            return(documentInfo);
        }
コード例 #2
0
        public async Task <DistributorData> Compose(
            MailData mailData,
            MorestachioDocumentInfo parsedTemplate,
            CompiledExpression compiledAddressExpression,
            CompiledExpression compiledSubjectExpression,
            CompiledExpression compiledNameExpression,
            CompiledExpression compiledFromAddressExpression,
            CompiledExpression compiledFromNameExpression)
        {
            var context = new ContextObject(parsedTemplate?.ParserOptions ?? new ParserOptions(), ".", null, mailData);

            mailData.MailInfo.ToName = compiledNameExpression != null
                                ? (await compiledNameExpression(context, new ScopeData())).Value.ToString()
                                : null;
            mailData.MailInfo.ToAddress = compiledAddressExpression != null
                                ? (await compiledAddressExpression(context, new ScopeData())).Value.ToString()
                                : null;
            mailData.MailInfo.FromAddress = compiledFromAddressExpression != null
                                ? (await compiledFromAddressExpression(context, new ScopeData())).Value.ToString()
                                : null;
            mailData.MailInfo.FromName = compiledFromNameExpression != null
                                ? (await compiledFromNameExpression(context, new ScopeData())).Value.ToString()
                                : null;
            mailData.MailInfo.Subject = compiledSubjectExpression != null
                                ? (await compiledSubjectExpression(context, new ScopeData())).Value.ToString()
                                : null;

            return(new DistributorData(mailData.MailInfo,
                                       parsedTemplate != null ? (await parsedTemplate.CreateAsync(mailData)) : null));
        }
コード例 #3
0
        /// <summary>
        ///		Renders the current <see cref="FluentApiContext.CurrentNode"/>
        /// </summary>
        /// <param name="data"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public MorestachioDocumentFluentApi Render(object data, Action <Stream> template)
        {
            var documentInfo = new MorestachioDocumentInfo(Context.Options, Context.CurrentNode.Item);
            var morestachioDocumentResult = documentInfo.Create(data);

            template(morestachioDocumentResult.Stream);
            return(this);
        }
コード例 #4
0
        public async Task TestRuns(string variation, int modelDepth, int sizeOfTemplate, int inserts, int runs)
        {
            var model        = ConstructModelAndPath(modelDepth);
            var baseTemplate = Enumerable.Range(1, 5)
                               .Aggregate("", (seed, current) => seed += " {{" + model.Item2 + "}}");

            while (baseTemplate.Length <= sizeOfTemplate)
            {
                baseTemplate += model.Item2 + "\r\n";
            }

            MorestachioDocumentInfo template = null;

            //make sure this class is JIT'd before we start timing.
            Parser.ParseWithOptions(new ParserOptions("asdf"));

            var       totalTime = Stopwatch.StartNew();
            var       parseTime = Stopwatch.StartNew();
            Stopwatch renderTime;

            for (var i = 0; i < runs; i++)
            {
                template = Parser.ParseWithOptions(new ParserOptions(baseTemplate, () => Stream.Null));
            }

            parseTime.Stop();

            var tmp = template.CreateAndStringifyAsync(model.Item1);

            renderTime = Stopwatch.StartNew();
            for (var i = 0; i < runs; i++)
            {
                var morestachioDocumentResult = await template.CreateAsync(model.Item1);

                using (var f = morestachioDocumentResult.Stream)
                {
                }
            }

            renderTime.Stop();
            totalTime.Stop();

            var modelPerformanceCounterEntity = new PerformanceCounter.ModelPerformanceCounterEntity(variation)
            {
                TimePerRun        = new TimeSpan(totalTime.ElapsedTicks / runs),
                RunOver           = runs,
                ModelDepth        = modelDepth,
                SubstitutionCount = inserts,
                TemplateSize      = sizeOfTemplate,
                ParseTime         = parseTime.Elapsed,
                RenderTime        = renderTime.Elapsed,
                TotalTime         = totalTime.Elapsed
            };

            PerformanceCounter.PerformanceCounters.Add(modelPerformanceCounterEntity);
            Console.WriteLine(PerformanceCounter.ModelPerformanceCounterEntity.Header(" | "));
            Console.WriteLine(modelPerformanceCounterEntity.PrintAsCsv(" | "));
        }
コード例 #5
0
        public override async ValueTask <MorestachioDocumentInfo> GetTemplate(HttpContext context)
        {
            if (CacheTemplate)
            {
                return(_templateCache ?? (_templateCache = await base.GetTemplate(context)));
            }

            return(await base.GetTemplate(context));
        }
コード例 #6
0
        private async Task SendSingleItem(Action <SendMailTaskProgress> progress,
                                          MailData mailData,
                                          MorestachioDocumentInfo parsedTemplate,
                                          CompiledExpression compiledAddressExpression,
                                          CompiledExpression compiledSubjectExpression,
                                          CompiledExpression compiledNameExpression,
                                          CompiledExpression compiledFromAddressExpression,
                                          CompiledExpression compiledFromNameExpression,
                                          IMailDistributorState state)
        {
            DistributorData distributorData = null;

            try
            {
                distributorData = await Compose(mailData, parsedTemplate, compiledAddressExpression,
                                                compiledSubjectExpression,
                                                compiledNameExpression,
                                                compiledFromAddressExpression,
                                                compiledFromNameExpression);

                var sendMailResult = await MailDistributor.SendMail(distributorData, state);

                if (!sendMailResult.Success)
                {
                    progress(new SendMailTaskProgress(distributorData.MailInfo.ToAddress, sendMailResult.ErrorText));
                }
                else
                {
                    progress(new SendMailTaskProgress(distributorData.MailInfo.ToAddress));
                }
            }
            catch (Exception e)
            {
                progress(new SendMailTaskProgress(distributorData?.MailInfo.ToAddress, e.Message));
            }
        }
コード例 #7
0
        public async Task ProfileTest()
        {
            string variation      = "Template Size";
            int    modelDepth     = 5;
            int    sizeOfTemplate = 100000;
            int    inserts        = 5;
            int    runs           = 1;

            var model        = PerfHarness.ConstructModelAndPath(modelDepth);
            var baseTemplate = Enumerable.Range(1, 5)
                               .Aggregate("", (seed, current) => seed += " {{" + model.Item2 + "}}");

            while (baseTemplate.Length <= sizeOfTemplate)
            {
                baseTemplate += model.Item2 + "\r\n";
            }

            MorestachioDocumentInfo template        = null;
            TokenizerResult         tokenizerResult = null;

            //make sure this class is JIT'd before we start timing.
            //await Parser.ParseWithOptionsAsync(new ParserOptions("asdf"));

            var totalTime      = Stopwatch.StartNew();
            var tokenizingTime = Stopwatch.StartNew();

            for (var i = 0; i < runs; i++)
            {
                var options          = new ParserOptions(baseTemplate, () => Stream.Null);
                var tokenzierContext = new TokenzierContext(new List <int>(), options.CultureInfo);
                tokenizerResult = await Tokenizer.Tokenize(options, tokenzierContext);
            }

            tokenizingTime.Stop();

            //var parseTime = Stopwatch.StartNew();
            //for (var i = 0; i < runs; i++)
            //{
            //	var options = new ParserOptions(baseTemplate, () => Stream.Null);
            //	template = new MorestachioDocumentInfo(options, Parser.Parse(tokenizerResult, options));
            //}

            //parseTime.Stop();

            //var tmp = await template.CreateAndStringifyAsync(model.Item1);

            //var renderTime = Stopwatch.StartNew();
            //for (var i = 0; i < runs; i++)
            //{
            //	var morestachioDocumentResult = await template.CreateAsync(model.Item1);
            //	morestachioDocumentResult.Stream.Dispose();
            //}

            //renderTime.Stop();
            //totalTime.Stop();

            //var compileTime = Stopwatch.StartNew();
            //CompilationResult compilationResult = null;
            //for (var i = 0; i < runs; i++)
            //{
            //	compilationResult = template.Compile();
            //}

            //compileTime.Stop();

            //var compiledRenderTime = Stopwatch.StartNew();
            //for (var i = 0; i < runs; i++)
            //{
            //	var morestachioDocumentResult = await compilationResult(model.Item1, CancellationToken.None);
            //	morestachioDocumentResult.Stream.Dispose();
            //}

            //compiledRenderTime.Stop();
        }
コード例 #8
0
 public void Setup()
 {
     _template = Parser.ParseWithOptions(new ParserOptions(GetTemplate(), null, Encoding.UTF8));
     _data     = GetData();
 }
コード例 #9
0
        public async Task TestRuns(string variation, int modelDepth, int sizeOfTemplate, int inserts, int runs)
        {
            var model        = ConstructModelAndPath(modelDepth);
            var baseTemplate = Enumerable.Range(1, 5)
                               .Aggregate("", (seed, current) => seed += " {{" + model.Item2 + "}}");

            while (baseTemplate.Length <= sizeOfTemplate)
            {
                baseTemplate += model.Item2 + "\r\n";
            }

            MorestachioDocumentInfo template        = null;
            TokenizerResult         tokenizerResult = null;

            //make sure this class is JIT'd before we start timing.
            (await Parser.ParseWithOptionsAsync(new ParserOptions("asdf"))).Create(new object()).Stream.Dispose();

            var totalTime      = Stopwatch.StartNew();
            var tokenizingTime = Stopwatch.StartNew();

            for (var i = 0; i < runs; i++)
            {
                var options          = new ParserOptions(baseTemplate, () => Stream.Null);
                var tokenzierContext = new TokenzierContext(new List <int>(), options.CultureInfo);
                tokenizerResult = await Tokenizer.Tokenize(options, tokenzierContext);
            }

            tokenizingTime.Stop();

            var parseTime = Stopwatch.StartNew();

            for (var i = 0; i < runs; i++)
            {
                var options = new ParserOptions(baseTemplate, () => Stream.Null);
                template = new MorestachioDocumentInfo(options, Parser.Parse(tokenizerResult, options));
            }

            parseTime.Stop();

            var tmp = await template.CreateAndStringifyAsync(model.Item1);

            var renderTime = Stopwatch.StartNew();

            for (var i = 0; i < runs; i++)
            {
                var morestachioDocumentResult = await template.CreateAsync(model.Item1);

                morestachioDocumentResult.Stream.Dispose();
            }

            renderTime.Stop();
            totalTime.Stop();

            var compileTime = Stopwatch.StartNew();
            CompilationResult compilationResult = null;

            for (var i = 0; i < runs; i++)
            {
                compilationResult = template.Compile();
            }

            compileTime.Stop();

            var compiledRenderTime = Stopwatch.StartNew();

            for (var i = 0; i < runs; i++)
            {
                var morestachioDocumentResult = await compilationResult(model.Item1, CancellationToken.None);

                morestachioDocumentResult.Stream.Dispose();
            }

            compiledRenderTime.Stop();

            var modelPerformanceCounterEntity = new PerformanceCounter.ModelPerformanceCounterEntity(variation)
            {
                TimePerRun = new TimeSpan((tokenizingTime.ElapsedTicks / runs) +
                                          (parseTime.ElapsedTicks / runs) +
                                          (renderTime.ElapsedTicks / runs)),
                RunOver            = runs,
                ModelDepth         = modelDepth,
                SubstitutionCount  = inserts,
                TemplateSize       = sizeOfTemplate,
                TokenizingTime     = tokenizingTime.Elapsed,
                ParseTime          = parseTime.Elapsed,
                RenderTime         = renderTime.Elapsed,
                TotalTime          = totalTime.Elapsed,
                CompilerTime       = compileTime.Elapsed,
                CompiledRenderTime = compiledRenderTime.Elapsed
            };

            PerformanceCounter.PerformanceCounters.Add(modelPerformanceCounterEntity);
            //Console.WriteLine(PerformanceCounter.ModelPerformanceCounterEntity.Header(" | "));
            //Console.WriteLine(modelPerformanceCounterEntity.PrintAsCsv(" | "));
        }
コード例 #10
0
        /// <summary>
        ///		Creates a new Fluent api
        /// </summary>
        internal MorestachioDocumentFluentApi(MorestachioDocumentInfo documentInfo)
        {
            var rootNode = CreateNodes(null, documentInfo.Document, out _);

            Context = new FluentApiContext(rootNode, rootNode, documentInfo.ParserOptions);
        }