/// <summary>
 /// <para>Set sizes format in output.</para>
 /// <para><c>Short</c> 54.8 MB</para>
 /// <para><c>Precise</c> 54829125</para>
 /// <para><c>Combined</c> 54.8 MB (54829125)</para>
 /// </summary>
 /// <param name="format"></param>
 /// <returns></returns>
 public HeapSnapshotCollector SetSizeFormat(SizeFormat format)
 {
     sizeFormat = format;
     return(this);
 }
        public static string Format(this SizeFormat format, long size)
        {
            if (format == SizeFormat.Precise)
            {
                return(size.ToString());
            }

            long quantumSize  = 1;
            int  postfixIndex = 0;

            while (quantumSize * 1024 < size && postfixIndex < 4)
            {
                quantumSize *= 1024;
                postfixIndex++;
            }

            double value = 1.0 * size / quantumSize;
            string shortString;

            if (postfixIndex == 0)
            {
                shortString = size.ToString(CultureInfo.InvariantCulture);
            }
            else if (value >= 9.995)
            {
                shortString = value.ToString("F1", CultureInfo.InvariantCulture);
            }
            else
            {
                shortString = value.ToString("F2", CultureInfo.InvariantCulture);
            }
            shortString += " ";

            switch (postfixIndex)
            {
            case 0:
                shortString += "bytes";
                break;

            case 1:
                shortString += "KB";
                break;

            case 2:
                shortString += "MB";
                break;

            case 3:
                shortString += "GB";
                break;

            case 4:
                shortString += "TB";
                break;

            default:
                shortString += "Unknown Qualifier";
                break;
            }

            if (format == SizeFormat.Short)
            {
                return(shortString);
            }
            else
            {
                return(shortString + " (" + size + ")");
            }
        }
Example #3
0
        public void MegaBytes(long kilobytes)
        {
            var text = SizeFormat.Readable(kilobytes * 1024);

            Assert.True(Regex.Match(text, @"^(\d){1,4} MB").Success, text);
        }
Example #4
0
        public void GigaBytes(long megabytes)
        {
            var text = SizeFormat.Readable(megabytes * 1024 * 1024);

            Assert.True(Regex.Match(text, @"^(\d){1,4} GB").Success, text);
        }
Example #5
0
        public void KiloBytes(int bytes)
        {
            var text = SizeFormat.Readable(bytes);

            Assert.True(Regex.Match(text, @"^(\d){1,4} KB").Success, text);
        }