<commit_before>﻿using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace DIAttributeRegistrar.Performance
{
    [Config(typeof(BaseBenchmarkConfig))]
    public class AssemblyScanBenchmark
    {
        private const int OperationsPerInvoke = 50000;
        
        [GlobalSetup]
        public void GlobalSetup()
        {
        }
        
        [Benchmark(Baseline = true, OperationsPerInvoke = OperationsPerInvoke)]
        public void WithSpecifiedAssemblies()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            IEnumerable<Assembly> assembliesToSearch = Enumerable.Repeat(this.GetType().GetTypeInfo().Assembly, 1);
            AttributeRegistrar attributeRegistrar = new AttributeRegistrar();

            for (int i = 0; i < OperationsPerInvoke; i++)
            {
                attributeRegistrar.RegisterTypes(serviceCollection, assembliesToSearch, null);
                serviceCollection.BuildServiceProvider();
            }
            
        }
        
        [Register]
        private class AssemblyScanBenchmark_TestClassA
        {
        }
    }
}<commit_msg>New banchmark test were added.<commit_after>﻿using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace DIAttributeRegistrar.Performance
{
    [Config(typeof(BaseBenchmarkConfig))]
    public class AssemblyScanBenchmark
    {
        private const int OperationsPerInvoke = 50000;
        
        [GlobalSetup]
        public void GlobalSetup()
        {
        }
        
        [Benchmark(Baseline = true, OperationsPerInvoke = OperationsPerInvoke)]
        public void WithSpecifiedAssemblies()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            Func<IEnumerable<Assembly>> getSpecifiedAssemblies = () =>
            {
                return Enumerable.Repeat(this.GetType().GetTypeInfo().Assembly, 1);
            };
            AttributeRegistrar attributeRegistrar = new AttributeRegistrar(getSpecifiedAssemblies);

            for (int i = 0; i < OperationsPerInvoke; i++)
            {
                attributeRegistrar.RegisterTypes(serviceCollection, null);
                serviceCollection.BuildServiceProvider();
            }
            
        }

        [Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
        public void GoingThroughAllAssemblies()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            Func<IEnumerable<Assembly>> getAllAssemblies = () =>
            {
                return DependencyContext
                    .Default
                    .RuntimeLibraries
                    .SelectMany(l => l.GetDefaultAssemblyNames(DependencyContext.Default))
                    .Select(Assembly.Load);
            };
            AttributeRegistrar attributeRegistrar = new AttributeRegistrar(getAllAssemblies);

            for (int i = 0; i < OperationsPerInvoke; i++)
            {
                attributeRegistrar.RegisterTypes(serviceCollection, null);
                serviceCollection.BuildServiceProvider();
            }

        }

        [Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
        public void GoingThroughAssembliesWithReference()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            Func<IEnumerable<Assembly>> getAssembliesWithReference = () =>
            {
                var currentAssemblyFullName = this.GetType().GetTypeInfo().Assembly.FullName;
                return DependencyContext
                    .Default
                    .RuntimeLibraries
                    .SelectMany(l => l.GetDefaultAssemblyNames(DependencyContext.Default))
                    .Select(Assembly.Load)
                    .Where(t => 
                        t.GetReferencedAssemblies()
                            .Select(Assembly.Load)
                            .Any(ra => 0 == String.Compare(ra.FullName, currentAssemblyFullName, StringComparison.OrdinalIgnoreCase)));
            };
            AttributeRegistrar attributeRegistrar = new AttributeRegistrar(getAssembliesWithReference);

            for (int i = 0; i < OperationsPerInvoke; i++)
            {
                attributeRegistrar.RegisterTypes(serviceCollection, null);
                serviceCollection.BuildServiceProvider();
            }

        }

        [Register]
        private class AssemblyScanBenchmark_TestClassA
        {
        }
    }
}
{'fraction_non_alphanumeric': 0.07642105263157895, 'fraction_numerical': 0.0035789473684210526, 'mean_word_length': 2.089076723016905, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368805', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1090, 'n_words': 201}