Example #1
0
        private static ulong GetCompressRating(uint dictionarySize, ulong elapsedTime, ulong size)
        {
            ulong t = LzmaBench.GetLogSize(dictionarySize) - (18 << LzmaBench.kSubBits);
            ulong numCommandsForOne = 1060 + ((t * t * 10) >> (2 * LzmaBench.kSubBits));
            ulong numCommands       = size * numCommandsForOne;

            return(LzmaBench.MyMultDiv64(numCommands, elapsedTime));
        }
Example #2
0
        private static void PrintResults(uint dictionarySize, ulong elapsedTime, ulong size, bool decompressMode, ulong secondSize)
        {
            ulong speed = LzmaBench.MyMultDiv64(size, elapsedTime);

            LzmaBench.PrintValue(speed / 1024);
            Console.Write(" KB/s  ");
            ulong rating;

            if (decompressMode)
            {
                rating = LzmaBench.GetDecompressRating(elapsedTime, size, secondSize);
            }
            else
            {
                rating = LzmaBench.GetCompressRating(dictionarySize, elapsedTime, size);
            }

            LzmaBench.PrintRating(rating);
        }
Example #3
0
        public static int LzmaBenchmark(int numIterations, uint dictionarySize)
        {
            if (numIterations <= 0)
            {
                return(0);
            }

            if (dictionarySize < 1 << 18)
            {
                Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
                return(1);
            }

            Console.Write("\n       Compressing                Decompressing\n\n");
            Encoder encoder = new Encoder();
            Decoder decoder = new Decoder();

            CoderPropId[] propIDs =
            {
                CoderPropId.DictionarySize
            };
            object[] properties =
            {
                (Int32)dictionarySize
            };
            uint kBufferSize           = dictionarySize + LzmaBench.kAdditionalSize;
            uint kCompressedBufferSize = kBufferSize / 2 + LzmaBench.kCompressedAdditionalSize;

            encoder.SetCoderProperties(propIDs, properties);
            MemoryStream propStream = new MemoryStream();

            encoder.WriteCoderProperties(propStream);
            byte[] propArray         = propStream.ToArray();
            CBenchRandomGenerator rg = new CBenchRandomGenerator();

            rg.Set(kBufferSize);
            rg.Generate();
            CRC crc = new CRC();

            crc.Init();
            crc.Update(rg.Buffer, 0, rg.BufferSize);
            CProgressInfo progressInfo = new CProgressInfo();

            progressInfo.ApprovedStart = dictionarySize;
            ulong        totalBenchSize      = 0;
            ulong        totalEncodeTime     = 0;
            ulong        totalDecodeTime     = 0;
            ulong        totalCompressedSize = 0;
            MemoryStream inStream            = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize);
            MemoryStream compressedStream    = new MemoryStream((int)kCompressedBufferSize);
            CrcOutStream crcOutStream        = new CrcOutStream();

            for (int i = 0; i < numIterations; i++)
            {
                progressInfo.Init();
                inStream.Seek(0, SeekOrigin.Begin);
                compressedStream.Seek(0, SeekOrigin.Begin);
                encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
                TimeSpan sp2            = DateTime.UtcNow - progressInfo.Time;
                ulong    encodeTime     = (UInt64)sp2.Ticks;
                long     compressedSize = compressedStream.Position;
                if (progressInfo.InSize == 0)
                {
                    throw new Exception("Internal ERROR 1282");
                }

                ulong decodeTime = 0;
                for (int j = 0; j < 2; j++)
                {
                    compressedStream.Seek(0, SeekOrigin.Begin);
                    crcOutStream.Init();
                    decoder.SetDecoderProperties(propArray);
                    ulong    outSize   = kBufferSize;
                    DateTime startTime = DateTime.UtcNow;
                    decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null);
                    TimeSpan sp = DateTime.UtcNow - startTime;
                    decodeTime = (ulong)sp.Ticks;
                    if (crcOutStream.GetDigest() != crc.GetDigest())
                    {
                        throw new Exception("CRC Error");
                    }
                }

                ulong benchSize = kBufferSize - (UInt64)progressInfo.InSize;
                LzmaBench.PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
                Console.Write("     ");
                LzmaBench.PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize);
                Console.WriteLine();
                totalBenchSize      += benchSize;
                totalEncodeTime     += encodeTime;
                totalDecodeTime     += decodeTime;
                totalCompressedSize += (ulong)compressedSize;
            }

            Console.WriteLine("---------------------------------------------------");
            LzmaBench.PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
            Console.Write("     ");
            LzmaBench.PrintResults(dictionarySize, totalDecodeTime, kBufferSize * (UInt64)numIterations, true, totalCompressedSize);
            Console.WriteLine("    Average");
            return(0);
        }
Example #4
0
 private static void PrintRating(ulong rating)
 {
     LzmaBench.PrintValue(rating / 1000000);
     Console.Write(" MIPS");
 }
Example #5
0
 private static ulong GetTotalRating(uint dictionarySize, ulong elapsedTimeEn, ulong sizeEn, ulong elapsedTimeDe, ulong inSizeDe, ulong outSizeDe)
 {
     return((LzmaBench.GetCompressRating(dictionarySize, elapsedTimeEn, sizeEn) + LzmaBench.GetDecompressRating(elapsedTimeDe, inSizeDe, outSizeDe)) / 2);
 }
Example #6
0
        private static ulong GetDecompressRating(ulong elapsedTime, ulong outSize, ulong inSize)
        {
            ulong numCommands = inSize * 220 + outSize * 20;

            return(LzmaBench.MyMultDiv64(numCommands, elapsedTime));
        }
Example #7
0
        private static int Start(string[] args)
        {
            Console.WriteLine("\nLZMA# 4.61  2008-11-23\n");
            if (args.Length == 0)
            {
                LzmaAlone.PrintHelp();
                return(0);
            }

            SwitchForm[] kSwitchForms = new SwitchForm[13];
            int          sw           = 0;

            kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false);
            kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false);
            kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1);
            kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1);
            kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1);
            kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1);
            kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1);
            kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1);
            kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1);
            kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false);
            kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false);
            kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false);
            kSwitchForms[sw++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1);
            Parser parser = new Parser(sw);

            try
            {
                parser.ParseStrings(kSwitchForms, args);
            }
            catch
            {
                return(LzmaAlone.IncorrectCommand());
            }

            if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs)
            {
                LzmaAlone.PrintHelp();
                return(0);
            }

            ArrayList nonSwitchStrings = parser.NonSwitchStrings;
            int       paramIndex       = 0;

            if (paramIndex >= nonSwitchStrings.Count)
            {
                return(LzmaAlone.IncorrectCommand());
            }

            string command = (string)nonSwitchStrings[paramIndex++];

            command = command.ToLower();
            bool dictionaryIsDefined = false;
            int  dictionary          = 1 << 21;

            if (parser[(int)Key.Dictionary].ThereIs)
            {
                int dicLog;
                if (!LzmaAlone.GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog))
                {
                    LzmaAlone.IncorrectCommand();
                }

                dictionary          = 1 << dicLog;
                dictionaryIsDefined = true;
            }

            string mf = "bt4";

            if (parser[(int)Key.MatchFinder].ThereIs)
            {
                mf = (string)parser[(int)Key.MatchFinder].PostStrings[0];
            }

            mf = mf.ToLower();
            if (command == "b")
            {
                const int kNumDefaultItereations = 10;
                int       numIterations          = kNumDefaultItereations;
                if (paramIndex < nonSwitchStrings.Count)
                {
                    if (!LzmaAlone.GetNumber((string)nonSwitchStrings[paramIndex++], out numIterations))
                    {
                        numIterations = kNumDefaultItereations;
                    }
                }

                return(LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary));
            }

            string train = string.Empty;

            if (parser[(int)Key.Train].ThereIs)
            {
                train = (string)parser[(int)Key.Train].PostStrings[0];
            }

            bool encodeMode = false;

            if (command == "e")
            {
                encodeMode = true;
            }
            else if (command == "d")
            {
                encodeMode = false;
            }
            else
            {
                LzmaAlone.IncorrectCommand();
            }

            bool   stdInMode  = parser[(int)Key.StdIn].ThereIs;
            bool   stdOutMode = parser[(int)Key.StdOut].ThereIs;
            Stream inStream   = null;

            if (stdInMode)
            {
                throw new Exception("Not implemeted");
            }

            if (paramIndex >= nonSwitchStrings.Count)
            {
                LzmaAlone.IncorrectCommand();
            }

            string inputName = (string)nonSwitchStrings[paramIndex++];

            inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read);
            FileStream outStream = null;

            if (stdOutMode)
            {
                throw new Exception("Not implemeted");
            }

            if (paramIndex >= nonSwitchStrings.Count)
            {
                LzmaAlone.IncorrectCommand();
            }

            string outputName = (string)nonSwitchStrings[paramIndex++];

            outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write);
            FileStream trainStream = null;

            if (train.Length != 0)
            {
                trainStream = new FileStream(train, FileMode.Open, FileAccess.Read);
            }

            if (encodeMode)
            {
                if (!dictionaryIsDefined)
                {
                    dictionary = 1 << 23;
                }

                int posStateBits   = 2;
                int litContextBits = 3; // for normal files

                // UInt32 litContextBits = 0; // for 32-bit data
                int litPosBits = 0;

                // UInt32 litPosBits = 2; // for 32-bit data
                int  algorithm    = 2;
                int  numFastBytes = 128;
                bool eos          = parser[(int)Key.EOS].ThereIs || stdInMode;
                if (parser[(int)Key.Mode].ThereIs)
                {
                    if (!LzmaAlone.GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm))
                    {
                        LzmaAlone.IncorrectCommand();
                    }
                }

                if (parser[(int)Key.FastBytes].ThereIs)
                {
                    if (!LzmaAlone.GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes))
                    {
                        LzmaAlone.IncorrectCommand();
                    }
                }

                if (parser[(int)Key.LitContext].ThereIs)
                {
                    if (!LzmaAlone.GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits))
                    {
                        LzmaAlone.IncorrectCommand();
                    }
                }

                if (parser[(int)Key.LitPos].ThereIs)
                {
                    if (!LzmaAlone.GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits))
                    {
                        LzmaAlone.IncorrectCommand();
                    }
                }

                if (parser[(int)Key.PosBits].ThereIs)
                {
                    if (!LzmaAlone.GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits))
                    {
                        LzmaAlone.IncorrectCommand();
                    }
                }

                CoderPropId[] propIDs =
                {
                    CoderPropId.DictionarySize, CoderPropId.PosStateBits, CoderPropId.LitContextBits, CoderPropId.LitPosBits, CoderPropId.Algorithm, CoderPropId.NumFastBytes, CoderPropId.MatchFinder, CoderPropId.EndMarker
                };
                object[] properties =
                {
                    dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos
                };
                Encoder encoder = new Encoder();
                encoder.SetCoderProperties(propIDs, properties);
                encoder.WriteCoderProperties(outStream);
                long fileSize;
                if (eos || stdInMode)
                {
                    fileSize = -1;
                }
                else
                {
                    fileSize = inStream.Length;
                }

                for (int i = 0; i < 8; i++)
                {
                    outStream.WriteByte((Byte)(fileSize >> (8 * i)));
                }

                if (trainStream != null)
                {
                    CDoubleStream doubleStream = new CDoubleStream();
                    doubleStream.s1        = trainStream;
                    doubleStream.s2        = inStream;
                    doubleStream.fileIndex = 0;
                    inStream = doubleStream;
                    long trainFileSize = trainStream.Length;
                    doubleStream.skipSize = 0;
                    if (trainFileSize > dictionary)
                    {
                        doubleStream.skipSize = trainFileSize - dictionary;
                    }

                    trainStream.Seek(doubleStream.skipSize, SeekOrigin.Begin);
                    encoder.SetTrainSize((uint)(trainFileSize - doubleStream.skipSize));
                }

                encoder.Code(inStream, outStream, -1, -1, null);
            }
            else if (command == "d")
            {
                byte[] properties = new byte[5];
                if (inStream.Read(properties, 0, 5) != 5)
                {
                    throw new Exception("input .lzma is too short");
                }

                Decoder decoder = new Decoder();
                decoder.SetDecoderProperties(properties);
                if (trainStream != null)
                {
                    if (!decoder.Train(trainStream))
                    {
                        throw new Exception("can't train");
                    }
                }

                long outSize = 0;
                for (int i = 0; i < 8; i++)
                {
                    int v = inStream.ReadByte();
                    if (v < 0)
                    {
                        throw new Exception("Can't Read 1");
                    }

                    outSize |= (long)(byte)v << (8 * i);
                }

                long compressedSize = inStream.Length - inStream.Position;
                decoder.Code(inStream, outStream, compressedSize, outSize, null);
            }
            else
            {
                throw new Exception("Command Error");
            }

            return(0);
        }