@@ -8,17 +8,29 @@ interface
8
8
Classes
9
9
, SysUtils
10
10
, fpjson
11
+ , fpjsonrtti
12
+ , fgl
11
13
;
12
14
13
15
const
14
16
cJSONHyperfineResult = ' results[0]' ;
15
17
16
18
type
19
+
20
+ { TDoubleList }
21
+
22
+ TDoubleList = class (specialize TFPGList<Double>)
23
+ public
24
+ function AvgValue : Double;
25
+ function AvgValueWithOutMinMax : Double;
26
+ end ;
27
+
28
+ TIntegerList = specialize TFPGList<Integer>;
29
+
17
30
{ EResultNotAJSONObject }
18
31
EResultNotAJSONObject = Exception;
19
32
20
33
{ THyperfineResult }
21
- TArrayOfTime = array of Double;
22
34
THyperfineResult = class (TObject)
23
35
private
24
36
FCommand: String;
@@ -29,7 +41,8 @@ THyperfineResult = class(TObject)
29
41
FSystem: Double;
30
42
FMin: Double;
31
43
FMax: Double;
32
- FTimes: TArrayOfTime;
44
+ FTimes: TDoubleList;
45
+ FExitCodes: TIntegerList;
33
46
34
47
procedure setFromJSONData (const AJSONData: TJSONData);
35
48
procedure setFromJSONObject (const AJSONObject: TJSONObject);
@@ -39,53 +52,87 @@ THyperfineResult = class(TObject)
39
52
constructor Create(const AJSONData: TJSONData);
40
53
41
54
destructor Destroy; override;
42
-
43
- property Command : String
55
+ published
56
+ property command : String
44
57
read FCommand
45
58
write FCommand;
46
- property Mean : Double
59
+ property mean : Double
47
60
read fMean
48
61
write FMean;
49
- property StandardDeviation : Double
62
+ property stddev : Double
50
63
read FStandardDeviation
51
64
write FStandardDeviation;
52
- property Median : Double
65
+ property median : Double
53
66
read FMedian
54
67
write FMedian;
55
- property User : Double
68
+ property user : Double
56
69
read FUser
57
70
write FUser;
58
- property System : Double
71
+ property system : Double
59
72
read FSystem
60
73
write FSystem;
61
- property Min : Double
74
+ property min : Double
62
75
read FMin
63
76
write FMin;
64
- property Max : Double
77
+ property max : Double
65
78
read FMax
66
79
write FMax;
67
- property Times: TArrayOfTime
80
+ property times: TDoubleList
68
81
read FTimes
69
82
write FTimes;
70
- published
83
+ property exit_codes: TIntegerList
84
+ read FExitCodes
85
+ write FExitCodes;
71
86
end ;
72
87
73
88
implementation
74
89
75
- const
76
- cJSONCommand = ' command' ;
77
- cJSONMean = ' mean' ;
78
- cJSONStandardDeviation = ' stddev' ;
79
- cJSONMedian = ' median' ;
80
- cJSONUser = ' user' ;
81
- cJSONSystem = ' system' ;
82
- cJSONMin = ' min' ;
83
- cJSONMax = ' max' ;
84
- cJSONTimes = ' times' ;
85
-
86
90
resourcestring
87
91
rsExceptionNotAJSONObject = ' JSON Data is not an object' ;
88
92
93
+ function CompareDouble (const d1, d2: Double): Integer;
94
+ begin
95
+ if d1 = d2 then Result:= 0
96
+ else if d1 < d2 then Result:= -1
97
+ else Result:= 1 ;
98
+ end ;
99
+
100
+ { TDoubleList }
101
+
102
+ function TDoubleList.AvgValue : Double;
103
+ var
104
+ i: Integer;
105
+ sum: Double;
106
+ begin
107
+ Result := 0 ;
108
+ if Count = 0 then
109
+ exit;
110
+ sum := 0 ;
111
+ for i := 0 to Count - 1 do
112
+ sum := sum + Items[i];
113
+ Result := sum / Count;
114
+ end ;
115
+
116
+ function TDoubleList.AvgValueWithOutMinMax : Double;
117
+ var
118
+ sortedlist: TDoubleList;
119
+ i: Integer;
120
+ sum: Double;
121
+ begin
122
+ Result := 0 ;
123
+ if Count <= 2 then
124
+ exit;
125
+ sum := 0 ;
126
+ sortedlist := TDoubleList.Create;
127
+ for i := 0 to Count - 1 do
128
+ sortedlist.Add(Items[i]);
129
+ sortedlist.Sort(@CompareDouble);
130
+ for i := 1 to sortedlist.Count - 2 do
131
+ sum := sum + sortedlist[i];
132
+ Result := sum / (sortedlist.Count - 2 );
133
+ sortedlist.Free;
134
+ end ;
135
+
89
136
{ THyperfineResult }
90
137
91
138
constructor THyperfineResult.Create;
@@ -98,7 +145,8 @@ constructor THyperfineResult.Create;
98
145
FSystem:= 0.0 ;
99
146
FMin:= 0.0 ;
100
147
FMax:= 0.0 ;
101
- SetLength(FTimes, 0 );
148
+ FTimes := TDoubleList.Create;
149
+ FExitCodes := TIntegerList.Create;
102
150
end ;
103
151
104
152
constructor THyperfineResult.Create(const AJSONData: TJSONData);
@@ -109,6 +157,8 @@ constructor THyperfineResult.Create(const AJSONData: TJSONData);
109
157
110
158
destructor THyperfineResult.Destroy;
111
159
begin
160
+ FTimes.Free;
161
+ FExitCodes.Free;
112
162
inherited Destroy;
113
163
end ;
114
164
@@ -123,22 +173,13 @@ procedure THyperfineResult.setFromJSONData(const AJSONData: TJSONData);
123
173
124
174
procedure THyperfineResult.setFromJSONObject (const AJSONObject: TJSONObject);
125
175
var
126
- timesObject: TJSONArray;
127
- index: Integer;
176
+ jds: TJSONDestreamer;
128
177
begin
129
- FCommand:= AJSONObject.Get(cJSONCommand, FCommand);
130
- FMean:= AJSONObject.Get(cJSONMean, FMean);
131
- FStandardDeviation:= AJSONObject.Get(cJSONStandardDeviation, FStandardDeviation);
132
- FMedian:= AJSONObject.Get(cJSONMedian, FMedian);
133
- FUser:= AJSONObject.Get(cJSONUser, FUser);
134
- FSystem:= AJSONObject.Get(cJSONSystem, FSystem);
135
- FMin:= AJSONObject.Get(cJSONMin, FMin);
136
- FMax:= AJSONObject.Get(cJSONMax, FMax);
137
- timesObject:= AJSONObject.Arrays[cJSONTimes];
138
- SetLength(FTimes, timesObject.Count);
139
- for index:= 0 to Pred(timesObject.Count) do
140
- begin
141
- FTimes[index]:= timesObject[index].AsFloat;
178
+ jds := TJSONDestreamer.Create(nil );
179
+ try
180
+ jds.JSONToObject(AJSONObject.AsJSON, Self);
181
+ finally
182
+ jds.Free;
142
183
end ;
143
184
end ;
144
185
0 commit comments