-
Notifications
You must be signed in to change notification settings - Fork 1
/
CPUBenchmark.dart
152 lines (128 loc) · 4.48 KB
/
CPUBenchmark.dart
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'dart:math';
class CPUBenchmark {
final int _defaultIterations = 1000000;
final int _numThreads = Platform.numberOfProcessors;
Future<double> runSingleThreaded() async {
double score = 1.0;
score *= await _runMathOperations();
score *= await _runStringManipulation();
score *= await _runListOperations();
score *= await _runBitManipulation();
return score;
}
Future<double> runMultiThreaded() async {
List<Future<double>> tasks = [];
for (int i = 0; i < _numThreads; i++) {
tasks.add(_runIsolate());
}
List<double> results = await Future.wait(tasks);
return results.reduce((a, b) => a * b);
}
Future<Map<String, String>> runBenchmark({int numTrials = 3}) async {
List<double> singleThreadScores = [];
List<double> multiThreadScores = [];
for (int i = 0; i < numTrials; i++) {
singleThreadScores.add(await runSingleThreaded());
multiThreadScores.add(await runMultiThreaded());
}
double singleThreadMedian = _calculateMedian(singleThreadScores);
double multiThreadMedian = _calculateMedian(multiThreadScores);
double totalScore = singleThreadMedian * multiThreadMedian;
return {
'Single-threaded': _formatScore(singleThreadMedian),
'Multi-threaded': _formatScore(multiThreadMedian),
'Total': _formatScore(totalScore),
};
}
double _calculateMedian(List<double> scores) {
scores.sort();
int middle = scores.length ~/ 2;
if (scores.length % 2 == 0) {
return (scores[middle - 1] + scores[middle]) / 2;
} else {
return scores[middle];
}
}
Future<double> _runMathOperations() async {
int iterations = _defaultIterations;
Stopwatch stopwatch = Stopwatch()..start();
for (int i = 0; i < iterations; i++) {
double result = sqrt(i) + log(i + 1) + sin(i) + cos(i) + tan(i);
if (result == double.infinity) break;
}
stopwatch.stop();
return iterations / stopwatch.elapsedMicroseconds;
}
Future<double> _runStringManipulation() async {
int iterations = _defaultIterations ~/ 10;
Stopwatch stopwatch = Stopwatch()..start();
String testString = 'Hello, World! ' * 100;
for (int i = 0; i < iterations; i++) {
String result = testString.replaceAll('o', 'x')
.toUpperCase()
.split(' ')
.reversed
.join('-');
if (result.isEmpty) break;
}
stopwatch.stop();
return iterations / stopwatch.elapsedMicroseconds;
}
Future<double> _runListOperations() async {
int iterations = _defaultIterations ~/ 100;
Stopwatch stopwatch = Stopwatch()..start();
List<int> testList = List.generate(1000, (index) => index);
for (int i = 0; i < iterations; i++) {
List<int> result = testList.where((element) => element % 2 == 0)
.map((e) => e * 2)
.toList()
..sort();
if (result.isEmpty) break;
}
stopwatch.stop();
return iterations / stopwatch.elapsedMicroseconds;
}
Future<double> _runBitManipulation() async {
int iterations = _defaultIterations;
Stopwatch stopwatch = Stopwatch()..start();
int value = 1;
for (int i = 0; i < iterations; i++) {
value = (value << 1) | (value >> 1);
value ^= i;
value &= 0xFFFFFFFF;
}
stopwatch.stop();
return iterations / stopwatch.elapsedMicroseconds;
}
Future<double> _runIsolate() async {
ReceivePort receivePort = ReceivePort();
await Isolate.spawn(_isolateEntryPoint, receivePort.sendPort);
double result = await receivePort.first as double;
return result;
}
static void _isolateEntryPoint(SendPort sendPort) async {
CPUBenchmark benchmark = CPUBenchmark();
double score = 1.0;
score *= await benchmark._runMathOperations();
score *= await benchmark._runStringManipulation();
score *= await benchmark._runListOperations();
score *= await benchmark._runBitManipulation();
sendPort.send(score);
}
String _formatScore(double score) {
if (score < 1024) {
return '${score.toStringAsFixed(2)}B';
} else if (score < 1024 * 1024) {
return '${(score / 1024).toStringAsFixed(2)}KB';
} else if (score < 1024 * 1024 * 1024) {
return '${(score / (1024 * 1024)).toStringAsFixed(2)}MB';
} else if (score < 1024 * 1024 * 1024 * 1024) {
return '${(score / (1024 * 1024 * 1024)).toStringAsFixed(2)}GB';
} else {
return '${(score / (1024 * 1024 * 1024 * 1024)).toStringAsFixed(2)}TB';
}
}
}