private void EncoderMemUsageTemplate(LzmaCompLevel level, bool extreme)
        {
            void PrintMemUsage(ulong usage, int threads = 0)
            {
                char   extremeChar = extreme ? 'e' : ' ';
                uint   purePreset  = (uint)level;
                string msg;

                if (threads == 0)
                {
                    msg = $"Encoder Mem Usage (p{purePreset}{extremeChar}) = {usage / (1024 * 1024) + 1}MB ({usage}B)";
                }
                else
                {
                    msg = $"Encoder Mem Usage (p{purePreset}{extremeChar}, {threads}T) = {usage / (1024 * 1024) + 1}MB ({usage}B)";
                }
                Console.WriteLine(msg);
            }

            ulong single = XZInit.EncoderMemUsage(level, extreme);
            ulong multi1 = XZInit.EncoderMultiMemUsage(level, extreme, 1);
            ulong multi2 = XZInit.EncoderMultiMemUsage(level, extreme, 2);

            PrintMemUsage(single);
            PrintMemUsage(multi1, 1);
            PrintMemUsage(multi2, 2);

            Assert.AreNotEqual(ulong.MaxValue, single);
            Assert.AreNotEqual(ulong.MaxValue, multi1);
            Assert.AreNotEqual(ulong.MaxValue, multi2);
            Assert.IsTrue(single < multi1);
        }
Example #2
0
        internal static uint ToPreset(LzmaCompLevel level, bool extremeFlag)
        {
            uint preset  = (uint)level;
            uint extreme = extremeFlag ? (1u << 31) : 0u;

            return(preset | extreme);
        }
Example #3
0
        /// <summary>
        /// Calculate approximate decoder memory usage of a preset
        /// </summary>
        /// <returns>
        /// Number of bytes of memory required to decompress a file that was compressed using the given preset.
        /// If an error occurs, for example due to unsupported preset, UINT64_MAX is returned.
        /// </returns>
        public static ulong DecoderMemUsage(LzmaCompLevel level, bool extremeFlag)
        {
            Manager.EnsureLoaded();

            uint preset = XZCompressOptions.ToPreset(level, extremeFlag);

            return(Lib.LzmaEasyDecoderMemUsage(preset));
        }
Example #4
0
        /// <summary>
        /// Calculate approximate memory usage of multithreaded .xz encoder
        /// </summary>
        /// <returns>
        /// Number of bytes of memory required for encoding with the given options.
        /// If an error occurs, for example due to unsupported preset or filter chain, UINT64_MAX is returned.
        /// </returns>
        public static ulong EncoderMultiMemUsage(LzmaCompLevel level, bool extremeFlag, int threads)
        {
            Manager.EnsureLoaded();

            uint   preset = XZCompressOptions.ToPreset(level, extremeFlag);
            LzmaMt mtOpts = LzmaMt.Create(preset, threads);

            return(Lib.LzmaStreamEncoderMtMemUsage(mtOpts));
        }
        private void DecoderMemUsageTemplate(LzmaCompLevel level, bool extreme)
        {
            void PrintMemUsage(ulong usage)
            {
                char extremeChar = extreme ? 'e' : ' ';
                uint purePreset  = (uint)level;

                Console.WriteLine($"Decoder Mem Usage (p{purePreset}{extremeChar}) = {usage / (1024 * 1024) + 1}MB ({usage}B)");
            }

            ulong usage = XZInit.DecoderMemUsage(level, extreme);

            PrintMemUsage(usage);
            Assert.AreNotEqual(ulong.MaxValue, usage);
        }
        private static void CompressTemplate(string sampleFileName, bool useSpan, bool success, int threads, LzmaCompLevel level, bool extreme)
        {
            string destDir = Path.GetTempFileName();

            File.Delete(destDir);
            Directory.CreateDirectory(destDir);
            try
            {
                string tempDecompFile = Path.Combine(destDir, Path.GetFileName(sampleFileName));
                string tempXzFile     = tempDecompFile + ".xz";

                XZCompressOptions compOpts = new XZCompressOptions
                {
                    Level       = level,
                    ExtremeFlag = extreme,
                    LeaveOpen   = true,
                };
                XZThreadedCompressOptions threadOpts = new XZThreadedCompressOptions
                {
                    Threads = threads,
                };

                string sampleFile = Path.Combine(TestSetup.SampleDir, sampleFileName);
                using (FileStream xzCompFs = new FileStream(tempXzFile, FileMode.Create, FileAccess.Write, FileShare.None))
                    using (FileStream sampleFs = new FileStream(sampleFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (XZStream xzs = new XZStream(xzCompFs, compOpts, threadOpts))
                        {
#if !NETFRAMEWORK
                            if (useSpan)
                            {
                                byte[] buffer = new byte[64 * 1024];

                                int bytesRead;
                                do
                                {
                                    bytesRead = sampleFs.Read(buffer.AsSpan());
                                    xzs.Write(buffer.AsSpan(0, bytesRead));
                                } while (0 < bytesRead);
                            }
                            else
#endif
                            {
                                sampleFs.CopyTo(xzs);
                            }

                            xzs.Flush();
                            xzs.GetProgress(out ulong finalIn, out ulong finalOut);

                            Assert.AreEqual(sampleFs.Length, xzs.TotalIn);
                            Assert.AreEqual(xzCompFs.Length, xzs.TotalOut);
                            Assert.AreEqual((ulong)sampleFs.Length, finalIn);
                            Assert.AreEqual((ulong)xzCompFs.Length, finalOut);
                        }

                Assert.IsTrue(TestHelper.RunXZ(tempXzFile) == 0);

                byte[] decompDigest;
                byte[] originDigest;
                using (FileStream fs = new FileStream(sampleFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (HashAlgorithm hash = SHA256.Create())
                    {
                        originDigest = hash.ComputeHash(fs);
                    }
                }

                using (FileStream fs = new FileStream(tempDecompFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (HashAlgorithm hash = SHA256.Create())
                    {
                        decompDigest = hash.ComputeHash(fs);
                    }
                }

                Assert.IsTrue(originDigest.SequenceEqual(decompDigest));
                Assert.IsTrue(success);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception)
            {
                Assert.IsFalse(success);
            }
#pragma warning restore CA1031 // Do not catch general exception types
            finally
            {
                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
            }
        }