TestResultProcessor.cs 1.65 KB
Newer Older
gaoqiong's avatar
gaoqiong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json;
using Xunit.Abstractions;

namespace Microsoft.ML.OnnxRuntime.Tests.Devices
{
    public class TestResultProcessor
    {
        ConcurrentBag<TestResult> _results = new ConcurrentBag<TestResult>();

        public ConcurrentBag<TestResult> Results
        {
            get => _results == null ? (_results = new ConcurrentBag<TestResult>()) : _results;
            private set => _results = value;
        }

        internal void RecordResult(TestResult test)
            => Results.Add(test);

        public void RecordResult(ITestResultMessage testResult, ITestCase testCase, TestOutcome outcome)
        {
            try
            {
                RecordResult(new TestResult
                {
                    TestId = testCase.UniqueID,
                    TestName = testCase.DisplayName,
                    Output = testResult.Output,
                    TestOutcome = outcome,
                    Duration = TimeSpan.FromSeconds((double)testResult.ExecutionTime).ToString("c", CultureInfo.InvariantCulture)
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.Message);
            }
        }

        public TestResultSummary GetResults()
            => new TestResultSummary(Results.ToList());

        public string GetSerializedResults()
        {
            var resultSummary = GetResults();
            var serializedResultSummary = JsonConvert.SerializeObject(resultSummary, Formatting.Indented);
            return serializedResultSummary;
        }
    }
}