Exemple #1
0
        public override string Execute(string args)
        {
            var pluginInstance = ProfilerPlugin.Instance;
            var registrations  = HarmonyProfiling.GetAllRegistrations();

            if (!pluginInstance.IsProfiling)
            {
                return(Error("Profiling is not running"));
            }

            pluginInstance.IsProfiling = false;
            var measureTypes = registrations.Values.Where(d => d.Measurements.Count > 0).Select(c => c.MeasureType).Distinct();

            JObject data = new JObject();

            foreach (var measureType in measureTypes)
            {
                StringBuilder sb          = new StringBuilder();
                JArray        measureList = new JArray();

                bool anyCallsMeasured = false;
                foreach (var measurableMethod in registrations.Values.Where(d => d.MeasureType.Equals(measureType, StringComparison.OrdinalIgnoreCase)))
                {
                    var assemblyName = measurableMethod.Method.DeclaringType?.Assembly?.GetName()?.Name?.StripUtf8() ?? "<unknown>";
                    var measurements = measurableMethod.Measurements;
                    if (!measurements.Any())
                    {
                        continue;
                    }

                    string methodName = measurableMethod.Method.GetFullName().StripUtf8();

                    //calculate & log averages
                    JObject obj = new JObject
                    {
                        { "assemblyName", assemblyName },
                        { "methodName", methodName },
                        { "avg", measurements.Average() },
                        { "min", measurements.Min() },
                        { "max", measurements.Max() },
                        { "calls", measurements.Count }
                    };
                    measureList.Add(obj);

                    anyCallsMeasured = true;
                }

                if (anyCallsMeasured)
                {
                    data.Add(measureType, measureList);
                }
            }

            HarmonyProfiling.ClearRegistrations();
            return(Success(data));
        }
Exemple #2
0
        public void Execute(IRocketPlayer caller, string[] command)
        {
            var pluginInstance = ProfilerPlugin.Instance;
            var registrations  = HarmonyProfiling.GetAllRegistrations();

            if (!pluginInstance.IsProfiling)
            {
                UnturnedChat.Say(caller, "Profiling is not running", Color.red);
                return;
            }

            string fileName = "Profiler-" + DateTime.Now.Ticks + ".log";

            pluginInstance.IsProfiling = false;
            using (var logger = new StreamWriter(fileName))
            {
                var measureTypes = registrations.Values.Where(d => d.Measurements.Count > 0).Select(c => c.MeasureType).Distinct();

                foreach (var measureType in measureTypes)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine($"{measureType}:");

                    bool anyCallsMeasured = false;
                    foreach (var measurableMethod in registrations.Values.Where(d => d.MeasureType.Equals(measureType, StringComparison.OrdinalIgnoreCase)))
                    {
                        var assemblyName = measurableMethod.Method.DeclaringType?.Assembly?.GetName()?.Name?.StripUtf8() ?? "<unknown>";
                        var measurements = measurableMethod.Measurements;
                        if (!measurements.Any())
                        {
                            continue;
                        }

                        string methodName = measurableMethod.Method.GetFullName().StripUtf8();

                        //calculate & log averages
                        sb.AppendLine($"\t {assemblyName} {methodName} (avg: {measurements.Average():0}ms, min: {measurements.Min():0}ms, max: {measurements.Max():0}ms, calls: {measurements.Count})");
                        anyCallsMeasured = true;
                    }

                    if (anyCallsMeasured)
                    {
                        logger.Write(sb.ToString());
                        logger.WriteLine();
                    }
                }

                logger.Close();
            }

            HarmonyProfiling.ClearRegistrations();
            UnturnedChat.Say(caller, $"Profiling stopped. Saved as {fileName}.");
        }