Example #1
0
        public Task Performance()
        {
            var from  = 0;
            var froms = HttpContext.Request.Query["from"];

            if (froms.Count > 1)
            {
                throw new ArgumentException($"Query string value 'from' must appear exactly once");
            }
            if (froms.Count > 0 && int.TryParse(froms[0], out from) == false)
            {
                throw new ArgumentException($"Query string value 'from' must be a number");
            }

            var stats = GetIndexesToReportOn()
                        .Select(x => new IndexPerformanceStats
            {
                IndexName   = x.Name,
                IndexId     = x.IndexId,
                Performance = x.GetIndexingPerformance(from)
            })
                        .ToArray();

            JsonOperationContext context;

            using (Database.DocumentsStorage.ContextPool.AllocateOperationContext(out context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartArray();

                    var isFirst = true;
                    foreach (var stat in stats)
                    {
                        if (isFirst == false)
                        {
                            writer.WriteComma();
                        }

                        isFirst = false;

                        writer.WriteStartObject();

                        writer.WritePropertyName(nameof(stat.IndexName));
                        writer.WriteString((stat.IndexName));
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(stat.IndexId));
                        writer.WriteInteger(stat.IndexId);
                        writer.WriteComma();

                        writer.WritePropertyName(nameof(stat.Performance));
                        writer.WriteStartArray();
                        var isFirstInternal = true;
                        foreach (var performance in stat.Performance)
                        {
                            if (isFirstInternal == false)
                            {
                                writer.WriteComma();
                            }

                            isFirstInternal = false;

                            writer.WriteIndexingPerformanceStats(context, performance);
                        }
                        writer.WriteEndArray();

                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                }

            return(Task.CompletedTask);
        }