Skip to content

Commit 0904796

Browse files
committed
Convert classes in statistics.js and math.js to use JS class syntax.
1 parent 327ef6c commit 0904796

File tree

3 files changed

+117
-119
lines changed

3 files changed

+117
-119
lines changed

MotionMark/resources/runner/motionmark.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,18 @@
235235
var experimentResult = {};
236236
result[Strings.json.controller] = experimentResult;
237237
experimentResult[Strings.json.measurements.average] = averageComplexity.mean();
238-
experimentResult[Strings.json.measurements.concern] = averageComplexity.concern(Experiment.defaults.CONCERN);
238+
experimentResult[Strings.json.measurements.concern] = averageComplexity.concern(Experiment.DEFAULT_CONCERN);
239239
experimentResult[Strings.json.measurements.stdev] = averageComplexity.standardDeviation();
240240
experimentResult[Strings.json.measurements.percent] = averageComplexity.percentage();
241241

242242
experimentResult = {};
243243
result[Strings.json.frameLength] = experimentResult;
244244
experimentResult[Strings.json.measurements.average] = 1000 / averageFrameLength.mean();
245-
experimentResult[Strings.json.measurements.concern] = averageFrameLength.concern(Experiment.defaults.CONCERN);
245+
experimentResult[Strings.json.measurements.concern] = averageFrameLength.concern(Experiment.DEFAULT_CONCERN);
246246
experimentResult[Strings.json.measurements.stdev] = averageFrameLength.standardDeviation();
247247
experimentResult[Strings.json.measurements.percent] = averageFrameLength.percentage();
248248

249-
result[Strings.json.score] = averageComplexity.score(Experiment.defaults.CONCERN);
249+
result[Strings.json.score] = averageComplexity.score(Experiment.DEFAULT_CONCERN);
250250
result[Strings.json.scoreLowerBound] = result[Strings.json.score] - averageFrameLength.standardDeviation();
251251
result[Strings.json.scoreUpperBound] = result[Strings.json.score] + averageFrameLength.standardDeviation();
252252
}

MotionMark/resources/statistics.js

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2015-2017 Apple Inc. All rights reserved.
2+
* Copyright (C) 2015-2024 Apple Inc. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -22,17 +22,17 @@
2222
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2323
* THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
25-
Pseudo =
26-
{
27-
initialRandomSeed: 49734321,
28-
randomSeed: 49734321,
2925

30-
resetRandomSeed: function()
26+
class Pseudo {
27+
static initialRandomSeed = 49734321;
28+
static randomSeed = 49734321;
29+
30+
static resetRandomSeed()
3131
{
3232
Pseudo.randomSeed = Pseudo.initialRandomSeed;
33-
},
33+
}
3434

35-
random: function()
35+
static random()
3636
{
3737
var randomSeed = Pseudo.randomSeed;
3838
randomSeed = ((randomSeed + 0x7ed55d16) + (randomSeed << 12)) & 0xffffffff;
@@ -44,42 +44,41 @@ Pseudo =
4444
Pseudo.randomSeed = randomSeed;
4545
return (randomSeed & 0xfffffff) / 0x10000000;
4646
}
47-
};
47+
}
4848

49-
Statistics =
50-
{
51-
sampleMean: function(numberOfSamples, sum)
49+
class Statistics {
50+
static sampleMean(numberOfSamples, sum)
5251
{
5352
if (numberOfSamples < 1)
5453
return 0;
5554
return sum / numberOfSamples;
56-
},
55+
}
5756

5857
// With sum and sum of squares, we can compute the sample standard deviation in O(1).
5958
// See https://rniwa.com/2012-11-10/sample-standard-deviation-in-terms-of-sum-and-square-sum-of-samples/
60-
unbiasedSampleStandardDeviation: function(numberOfSamples, sum, squareSum)
59+
static unbiasedSampleStandardDeviation(numberOfSamples, sum, squareSum)
6160
{
6261
if (numberOfSamples < 2)
6362
return 0;
6463
return Math.sqrt((squareSum - sum * sum / numberOfSamples) / (numberOfSamples - 1));
65-
},
64+
}
6665

67-
geometricMean: function(values)
66+
static geometricMean(values)
6867
{
6968
if (!values.length)
7069
return 0;
7170
var roots = values.map(function(value) { return Math.pow(value, 1 / values.length); })
7271
return roots.reduce(function(a, b) { return a * b; });
73-
},
72+
}
7473

7574
// Cumulative distribution function
76-
cdf: function(value, mean, standardDeviation)
75+
static cdf(value, mean, standardDeviation)
7776
{
7877
return 0.5 * (1 + Statistics.erf((value - mean) / (Math.sqrt(2 * standardDeviation * standardDeviation))));
79-
},
78+
}
8079

8180
// Approximation of Gauss error function, Abramowitz and Stegun 7.1.26
82-
erf: function(value)
81+
static erf(value)
8382
{
8483
var sign = (value >= 0) ? 1 : -1;
8584
value = Math.abs(value);
@@ -94,92 +93,89 @@ Statistics =
9493
var t = 1.0 / (1.0 + p * value);
9594
var y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-value * value);
9695
return sign * y;
97-
},
96+
}
9897

99-
largestDeviationPercentage: function(low, mean, high)
98+
static largestDeviationPercentage(low, mean, high)
10099
{
101100
return Math.max(Math.abs(low / mean - 1), (high / mean - 1));
102101
}
103-
};
102+
}
103+
104+
class Experiment {
105+
static DEFAULT_CONCERN = 5;
106+
static DEFAULT_CONCERN_SIZE = 100;
104107

105-
Experiment = Utilities.createClass(
106-
function(includeConcern)
108+
constructor(includeConcern)
107109
{
108110
if (includeConcern)
109-
this._maxHeap = Heap.createMaxHeap(Experiment.defaults.CONCERN_SIZE);
111+
this._maxHeap = Heap.createMaxHeap(Experiment.DEFAULT_CONCERN_SIZE);
110112
this.reset();
111-
}, {
113+
}
112114

113-
reset: function()
115+
reset()
114116
{
115117
this._sum = 0;
116118
this._squareSum = 0;
117119
this._numberOfSamples = 0;
118120
if (this._maxHeap)
119121
this._maxHeap.init();
120-
},
122+
}
121123

122124
get sampleCount()
123125
{
124126
return this._numberOfSamples;
125-
},
127+
}
126128

127-
sample: function(value)
129+
sample(value)
128130
{
129131
this._sum += value;
130132
this._squareSum += value * value;
131133
if (this._maxHeap)
132134
this._maxHeap.push(value);
133135
++this._numberOfSamples;
134-
},
136+
}
135137

136-
mean: function()
138+
mean()
137139
{
138140
return Statistics.sampleMean(this._numberOfSamples, this._sum);
139-
},
141+
}
140142

141-
standardDeviation: function()
143+
standardDeviation()
142144
{
143145
return Statistics.unbiasedSampleStandardDeviation(this._numberOfSamples, this._sum, this._squareSum);
144-
},
146+
}
145147

146-
cdf: function(value)
148+
cdf(value)
147149
{
148150
return Statistics.cdf(value, this.mean(), this.standardDeviation());
149-
},
151+
}
150152

151-
percentage: function()
153+
percentage()
152154
{
153155
var mean = this.mean();
154156
return mean ? this.standardDeviation() * 100 / mean : 0;
155-
},
157+
}
156158

157-
concern: function(percentage)
159+
concern(percentage)
158160
{
159161
if (!this._maxHeap)
160162
return this.mean();
161163

162164
var size = Math.ceil(this._numberOfSamples * percentage / 100);
163165
var values = this._maxHeap.values(size);
164166
return values.length ? values.reduce(function(a, b) { return a + b; }) / values.length : 0;
165-
},
167+
}
166168

167-
score: function(percentage)
169+
score(percentage)
168170
{
169171
return Statistics.geometricMean([this.mean(), Math.max(this.concern(percentage), 1)]);
170172
}
171-
});
172-
173-
Experiment.defaults =
174-
{
175-
CONCERN: 5,
176-
CONCERN_SIZE: 100,
177-
};
173+
}
178174

179-
Regression = Utilities.createClass(
175+
class Regression {
180176
// `samples` is [ [ complexity, frameLength ], [ complexity, frameLength ], ... ]
181177
// All samples are analyzed. startIndex, endIndex are just stored for use by the caller.
182-
function(samples, startIndex, endIndex, options)
178+
constructor(samples, startIndex, endIndex, options)
183179
{
184180
const desiredFrameLength = options.desiredFrameLength;
185181
var profile;
@@ -214,14 +210,14 @@ Regression = Utilities.createClass(
214210
this.n1 = profile.n1;
215211
this.n2 = profile.n2;
216212
this.error = profile.error;
217-
}, {
213+
}
218214

219-
valueAt: function(complexity)
215+
valueAt(complexity)
220216
{
221217
if (this.n1 == 1 || complexity > this.complexity)
222218
return this.s2 + this.t2 * complexity;
223219
return this.s1 + this.t1 * complexity;
224-
},
220+
}
225221

226222
// A generic two-segment piecewise regression calculator. Based on Kundu/Ubhaya
227223
//
@@ -234,7 +230,7 @@ Regression = Utilities.createClass(
234230
//
235231
// x is assumed to be complexity, y is frame length. Can be used for pure complexity-FPS
236232
// analysis or for ramp controllers since complexity monotonically decreases with time.
237-
_calculateRegression: function(samples, options)
233+
_calculateRegression(samples, options)
238234
{
239235
const complexityIndex = 0;
240236
const frameLengthIndex = 1;
@@ -387,10 +383,8 @@ Regression = Utilities.createClass(
387383
n2: n2_best
388384
};
389385
}
390-
});
391386

392-
Utilities.extendObject(Regression, {
393-
bootstrap: function(samples, iterationCount, processResample, confidencePercentage)
387+
static bootstrap(samples, iterationCount, processResample, confidencePercentage)
394388
{
395389
var sampleLength = samples.length;
396390
var resample = new Array(sampleLength);
@@ -418,4 +412,4 @@ Utilities.extendObject(Regression, {
418412
confidencePercentage: confidencePercentage
419413
};
420414
}
421-
});
415+
}

0 commit comments

Comments
 (0)