private async Task <List <BenchmarkTestContent> > PrepareTestData(BenchmarkOption benchmarkOption)
        {
            var list = new List <BenchmarkTestContent>();

            if (!String.IsNullOrEmpty(benchmarkOption.Source))
            {
                // explicit test file
                list.Add(new BenchmarkTestContent(benchmarkOption.Source, false, BenchmarkContent.UserDefined));
                return(list);
            }

            // generate test data
            if ((benchmarkOption.Content & BenchmarkContent.Ascii) == BenchmarkContent.Ascii)
            {
                var testAsciiDataFolder = TestFileHelper.NewFolder();

                await Task.WhenAll(
                    Enumerable.Range(0, benchmarkOption.NumberOfFiles)
                    .Select(i => TestFileHelper.NewFile(testAsciiDataFolder, benchmarkOption.FileSize, true)));

                list.Add(new BenchmarkTestContent(testAsciiDataFolder, true, BenchmarkContent.Ascii));
            }

            if ((benchmarkOption.Content & BenchmarkContent.Binary) == BenchmarkContent.Binary)
            {
                var testBinaryDataFolder = TestFileHelper.NewFolder();

                await Task.WhenAll(
                    Enumerable.Range(0, benchmarkOption.NumberOfFiles)
                    .Select(i => TestFileHelper.NewFile(testBinaryDataFolder, benchmarkOption.FileSize)));

                list.Add(new BenchmarkTestContent(testBinaryDataFolder, true, BenchmarkContent.Binary));
            }
            return(list);
        }
        public IEnumerable <BenchmarkIteration> GenerateBenchmarkIteration(BenchmarkOption benchmarkOption, IEnumerable <BenchmarkTestContent> testData)
        {
            var algos = new[] {
                CompressionAlgo.Lz4,
                CompressionAlgo.Brotli,
                CompressionAlgo.Zstd
            };

            var withEncryption = benchmarkOption.Encrypt == null ? new[] { false, true } : new[] { benchmarkOption.Encrypt.Value };

            foreach (var data in testData)
            {
                foreach (CompressionAlgo algo in algos.Where(a => ShouldTestAlgo(benchmarkOption, a)))
                {
                    foreach (bool encrypt in withEncryption)
                    {
                        foreach (int ratio in GetBenchmarkRatios(benchmarkOption, algo))
                        {
                            yield return(new BenchmarkIteration
                            {
                                Algo = algo,
                                CompressionRatio = ratio,
                                Encryption = encrypt,
                                Content = data,
                            });
                        }
                    }
                }
            }
        }
        private static IEnumerable <int> GetBenchmarkRatios(BenchmarkOption benchmarkOption, CompressionAlgo algo)
        {
            if (String.IsNullOrEmpty(benchmarkOption.Ratios))
            {
                return(Enumerable.Range(1, MaxRatio(algo)));
            }

            var ratios = UserDefinedRatios(benchmarkOption.Ratios, algo).ToList();

            return(Enumerable.Range(ratios.Min(), ratios.Max() - ratios.Min() + 1));
        }
Beispiel #4
0
        private static bool ShouldBenchmark(AlgorithmContainer algo, BenchmarkOption benchmarkOption)
        {
            switch (benchmarkOption)
            {
            case BenchmarkOption.ZeroOnly:
                return(algo.BenchmarkNeeded);

            case BenchmarkOption.ReBecnhOnly:
                return(algo.IsReBenchmark);

            case BenchmarkOption.ZeroOrReBenchOnly:
                return(algo.BenchmarkNeeded || algo.IsReBenchmark);
            }
            return(true);
        }
        private static bool ShouldTestAlgo(BenchmarkOption benchmarkOption, CompressionAlgo algo)
        {
            switch (algo)
            {
            case CompressionAlgo.Lz4:
                return((benchmarkOption.Algorithm & BenchmarkCompressionAlgo.Lz4) == BenchmarkCompressionAlgo.Lz4);

            case CompressionAlgo.Brotli:
                return((benchmarkOption.Algorithm & BenchmarkCompressionAlgo.Brotli) == BenchmarkCompressionAlgo.Brotli);

            case CompressionAlgo.Zstd:
                return((benchmarkOption.Algorithm & BenchmarkCompressionAlgo.Zstd) == BenchmarkCompressionAlgo.Zstd);

            default:
                throw new ArgumentOutOfRangeException(nameof(algo), algo, "Unknown algo");
            }
        }
Beispiel #6
0
        public void GenerateOptions()
        {
            var options = new BenchmarkOption
            {
                Content       = BenchmarkContent.Both,
                Algorithm     = BenchmarkCompressionAlgo.All,
                FileSize      = 2048,
                NumberOfFiles = 2,
                Ratios        = "1",
                Cleanup       = true
            };
            var content = new List <BenchmarkTestContent>
            {
                new BenchmarkTestContent("", true, BenchmarkContent.Ascii),
                new BenchmarkTestContent("", true, BenchmarkContent.Binary),
            };
            var helper     = new BenchmarkIterationGenerator();
            var iterations = helper.GenerateBenchmarkIteration(options, content).ToList();

            Assert.Equal(12, iterations.Count);
        }
        public async Task <IEnumerable <BenchmarkIteration> > PrepareIteration(BenchmarkOption benchmarkOption)
        {
            var data = await PrepareTestData(benchmarkOption);

            return(GenerateBenchmarkIteration(benchmarkOption, data).ToList());
        }
        public static TccCommand ParseCommandLine(this string[] args)
        {
            TccCommand command;

            var parsed = CommandLine.Parser.Default.ParseArguments <CompressCmdOptions, DecompressOptions, BenchmarkOptions>(args);

            try
            {
                command = parsed.MapResult(
                    (CompressCmdOptions opts) =>
                {
                    var option = new CompressOption
                    {
                        Algo             = opts.Algorithm,
                        CompressionRatio = opts.Ratio,
                        DestinationDir   = opts.Output,
                        FailFast         = opts.FailFast,
                        Verbose          = opts.Verbose,
                        SourceDirOrFile  = opts.Source.FirstOrDefault(),
                        BlockMode        = opts.Individual ? BlockMode.Individual : BlockMode.Explicit,
                        Threads          = ExtractThreads(opts)
                    };

                    ExtractPasswordInfo(opts, option, Mode.Compress);

                    return(new TccCommand
                    {
                        Mode = Mode.Compress,
                        Option = option
                    });
                },
                    (DecompressOptions opts) =>
                {
                    var option = new DecompressOption
                    {
                        DestinationDir  = opts.Output,
                        FailFast        = opts.FailFast,
                        Verbose         = opts.Verbose,
                        SourceDirOrFile = opts.Source.FirstOrDefault(),
                        Threads         = ExtractThreads(opts)
                    };

                    ExtractPasswordInfo(opts, option, Mode.Decompress);

                    return(new TccCommand
                    {
                        Mode = Mode.Decompress,
                        Option = option
                    });
                },
                    (BenchmarkOptions opts) =>
                {
                    bool IsExplicitMode()
                    {
                        return(!args.Any(i => BenchmarkOptions.AutoTestDataOptions.Contains(i)));
                    }

                    var option = new BenchmarkOption
                    {
                        Algorithm          = opts.Algorithm,
                        Ratios             = opts.Ratios,
                        Encrypt            = opts.Encrypt,
                        Source             = IsExplicitMode() ? opts.Source : null,
                        Content            = opts.Content,
                        NumberOfFiles      = opts.NumberOfFiles,
                        FileSize           = opts.FileSize,
                        Threads            = opts.Threads,
                        OutputCompressed   = opts.OutputCompressed,
                        OutputDecompressed = opts.OutputDecompressed,
                        Cleanup            = opts.Cleanup
                    };
                    return(new TccCommand
                    {
                        Mode = Mode.Benchmark,
                        BenchmarkOption = option
                    });
                },
                    errs => new TccCommand {
                    ReturnCode = 1
                });
            }
            catch (CommandLineException ae)
            {
                Console.Out.WriteLine(ae.Message);
                return(new TccCommand {
                    ReturnCode = 1
                });
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.ToString());
                return(new TccCommand {
                    ReturnCode = 1
                });
            }
            return(command);
        }
        public async Task <OperationSummary> RunBenchmark(BenchmarkOption benchmarkOption)
        {
            var operationSummaries = new List <OperationSummary>();
            var keysFolder         = TestFileHelper.NewFolder();

            var iterations = await _iterationGenerator.PrepareIteration(benchmarkOption);

            var threads = benchmarkOption.Threads == 0 ? Environment.ProcessorCount : benchmarkOption.Threads;

            foreach (var iteration in iterations)
            {
                PasswordMode pm = iteration.Encryption ? PasswordMode.PublicKey : PasswordMode.None;
                var          compressedFolder = TestFileHelper.NewFolder(benchmarkOption.OutputCompressed);
                var          outputFolder     = TestFileHelper.NewFolder(benchmarkOption.OutputDecompressed);

                // compress
                var compressOption = new CompressOption
                {
                    Algo             = iteration.Algo,
                    CompressionRatio = iteration.CompressionRatio,
                    BlockMode        = BlockMode.Individual,
                    SourceDirOrFile  = iteration.Content.Source,
                    DestinationDir   = compressedFolder,
                    Threads          = threads,
                    PasswordOption   = await _benchmarkOptionHelper.GenerateCompressPasswordOption(pm, keysFolder)
                };

                OperationSummary resultCompress = await _tarCompressCrypt.Compress(compressOption);

                if (_cancellationTokenSource.IsCancellationRequested)
                {
                    await Cleanup();

                    return(null);
                }
                operationSummaries.Add(resultCompress);
                resultCompress.ThrowOnError();

                // decompress
                var decompressOption = new DecompressOption
                {
                    SourceDirOrFile = compressedFolder,
                    DestinationDir  = outputFolder,
                    Threads         = threads,
                    PasswordOption  = _benchmarkOptionHelper.GenerateDecompressPasswordOption(pm, keysFolder)
                };

                OperationSummary resultDecompress = await _tarCompressCrypt.Decompress(decompressOption);

                if (_cancellationTokenSource.IsCancellationRequested)
                {
                    await Cleanup();

                    return(null);
                }

                operationSummaries.Add(resultDecompress);
                resultDecompress.ThrowOnError();

                StringBuilder sb = FormatResultSummary(iteration, resultCompress, resultDecompress);

                Console.Out.WriteLine(sb.ToString());

                async Task Cleanup()
                {
                    if (benchmarkOption.Cleanup)
                    {
                        await "del /f /s /q * > NUL".Run(compressedFolder, CancellationToken.None);
                        Directory.Delete(compressedFolder, true);
                        await "del /f /s /q * > NUL".Run(outputFolder, CancellationToken.None);
                        Directory.Delete(outputFolder, true);
                    }
                }

                await Cleanup();
            }

            return(new OperationSummary(operationSummaries.SelectMany(i => i.OperationBlocks), 0, default));
        }