    001    package net.minecraft.profiler;
    002
    003    import java.util.ArrayList;
    004    import java.util.Collections;
    005    import java.util.HashMap;
    006    import java.util.Iterator;
    007    import java.util.List;
    008    import java.util.Map;
    009
    010    public class Profiler
    011    {
    012        /** List of parent sections */
    013        private final List sectionList = new ArrayList();
    014
    015        /** List of timestamps (System.nanoTime) */
    016        private final List timestampList = new ArrayList();
    017
    018        /** Flag profiling enabled */
    019        public boolean profilingEnabled = false;
    020
    021        /** Current profiling section */
    022        private String profilingSection = "";
    023
    024        /** Profiling map */
    025        private final Map profilingMap = new HashMap();
    026
    027        /**
    028         * Clear profiling.
    029         */
    030        public void clearProfiling()
    031        {
    032            this.profilingMap.clear();
    033            this.profilingSection = "";
    034            this.sectionList.clear();
    035        }
    036
    037        /**
    038         * Start section
    039         */
    040        public void startSection(String par1Str)
    041        {
    042            if (this.profilingEnabled)
    043            {
    044                if (this.profilingSection.length() > 0)
    045                {
    046                    this.profilingSection = this.profilingSection + ".";
    047                }
    048
    049                this.profilingSection = this.profilingSection + par1Str;
    050                this.sectionList.add(this.profilingSection);
    051                this.timestampList.add(Long.valueOf(System.nanoTime()));
    052            }
    053        }
    054
    055        /**
    056         * End section
    057         */
    058        public void endSection()
    059        {
    060            if (this.profilingEnabled)
    061            {
    062                long var1 = System.nanoTime();
    063                long var3 = ((Long)this.timestampList.remove(this.timestampList.size() - 1)).longValue();
    064                this.sectionList.remove(this.sectionList.size() - 1);
    065                long var5 = var1 - var3;
    066
    067                if (this.profilingMap.containsKey(this.profilingSection))
    068                {
    069                    this.profilingMap.put(this.profilingSection, Long.valueOf(((Long)this.profilingMap.get(this.profilingSection)).longValue() + var5));
    070                }
    071                else
    072                {
    073                    this.profilingMap.put(this.profilingSection, Long.valueOf(var5));
    074                }
    075
    076                if (var5 > 100000000L)
    077                {
    078                    System.out.println("Something\'s taking too long! \'" + this.profilingSection + "\' took aprox " + (double)var5 / 1000000.0D + " ms");
    079                }
    080
    081                this.profilingSection = !this.sectionList.isEmpty() ? (String)this.sectionList.get(this.sectionList.size() - 1) : "";
    082            }
    083        }
    084
    085        /**
    086         * Get profiling data
    087         */
    088        public List getProfilingData(String par1Str)
    089        {
    090            if (!this.profilingEnabled)
    091            {
    092                return null;
    093            }
    094            else
    095            {
    096                long var3 = this.profilingMap.containsKey("root") ? ((Long)this.profilingMap.get("root")).longValue() : 0L;
    097                long var5 = this.profilingMap.containsKey(par1Str) ? ((Long)this.profilingMap.get(par1Str)).longValue() : -1L;
    098                ArrayList var7 = new ArrayList();
    099
    100                if (par1Str.length() > 0)
    101                {
    102                    par1Str = par1Str + ".";
    103                }
    104
    105                long var8 = 0L;
    106                Iterator var10 = this.profilingMap.keySet().iterator();
    107
    108                while (var10.hasNext())
    109                {
    110                    String var11 = (String)var10.next();
    111
    112                    if (var11.length() > par1Str.length() && var11.startsWith(par1Str) && var11.indexOf(".", par1Str.length() + 1) < 0)
    113                    {
    114                        var8 += ((Long)this.profilingMap.get(var11)).longValue();
    115                    }
    116                }
    117
    118                float var21 = (float)var8;
    119
    120                if (var8 < var5)
    121                {
    122                    var8 = var5;
    123                }
    124
    125                if (var3 < var8)
    126                {
    127                    var3 = var8;
    128                }
    129
    130                Iterator var20 = this.profilingMap.keySet().iterator();
    131                String var12;
    132
    133                while (var20.hasNext())
    134                {
    135                    var12 = (String)var20.next();
    136
    137                    if (var12.length() > par1Str.length() && var12.startsWith(par1Str) && var12.indexOf(".", par1Str.length() + 1) < 0)
    138                    {
    139                        long var13 = ((Long)this.profilingMap.get(var12)).longValue();
    140                        double var15 = (double)var13 * 100.0D / (double)var8;
    141                        double var17 = (double)var13 * 100.0D / (double)var3;
    142                        String var19 = var12.substring(par1Str.length());
    143                        var7.add(new ProfilerResult(var19, var15, var17));
    144                    }
    145                }
    146
    147                var20 = this.profilingMap.keySet().iterator();
    148
    149                while (var20.hasNext())
    150                {
    151                    var12 = (String)var20.next();
    152                    this.profilingMap.put(var12, Long.valueOf(((Long)this.profilingMap.get(var12)).longValue() * 999L / 1000L));
    153                }
    154
    155                if ((float)var8 > var21)
    156                {
    157                    var7.add(new ProfilerResult("unspecified", (double)((float)var8 - var21) * 100.0D / (double)var8, (double)((float)var8 - var21) * 100.0D / (double)var3));
    158                }
    159
    160                Collections.sort(var7);
    161                var7.add(0, new ProfilerResult(par1Str, 100.0D, (double)var8 * 100.0D / (double)var3));
    162                return var7;
    163            }
    164        }
    165
    166        /**
    167         * End current section and start a new section
    168         */
    169        public void endStartSection(String par1Str)
    170        {
    171            this.endSection();
    172            this.startSection(par1Str);
    173        }
    174
    175        public String getNameOfLastSection()
    176        {
    177            return this.sectionList.size() == 0 ? "[UNKNOWN]" : (String)this.sectionList.get(this.sectionList.size() - 1);
    178        }
    179    }