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 package org.slf4j.helpers;
26
27 import junit.framework.AssertionFailedError;
28
29
30
31
32
33
34
35
36
37 public class BogoPerf {
38
39 private static long NANOS_IN_ONE_SECOND = 1000 * 1000 * 1000;
40 private static int INITIAL_N = 1000;
41 private static int LAST_N = 100;
42 private static int SLACK_FACTOR = 3;
43
44 static {
45
46 computeBogoIPS(INITIAL_N);
47 double bogo_ips = computeBogoIPS(INITIAL_N);
48 System.out.println("Host runs at " + bogo_ips + " BIPS");
49 }
50
51
52
53
54
55
56
57
58
59
60
61 private static double computeBogoIPS(int N) {
62 long begin = System.nanoTime();
63
64 for (int i = 0; i < N; i++) {
65 bogoInstruction();
66 }
67 long end = System.nanoTime();
68
69
70 double D = end - begin;
71
72 double avgDPIS = D / N;
73
74
75
76 double bogoIPS = NANOS_IN_ONE_SECOND / avgDPIS;
77
78
79 return bogoIPS;
80 }
81
82 private static void bogoInstruction() {
83
84 MyRandom myRandom = new MyRandom(100);
85 int len = 150;
86 int[] intArray = new int[len];
87 for (int i = 0; i < len; i++) {
88 intArray[i] = myRandom.nextInt();
89 }
90
91 BubbleSort.sort(intArray);
92 }
93
94
95
96
97
98
99 public static double currentBIPS() {
100 return computeBogoIPS(LAST_N);
101 }
102
103 static double min(double a, double b) {
104 return (a <= b) ? a : b;
105 }
106
107
108
109
110
111
112
113
114
115
116 public static void assertDuration(double currentDuration,
117 long referenceDuration, double referenceBIPS) throws AssertionFailedError {
118 double ajustedDuration = adjustExpectedDuration(referenceDuration,
119 referenceBIPS);
120 if (currentDuration > ajustedDuration * SLACK_FACTOR) {
121 throw new AssertionFailedError("current duration " + currentDuration
122 + " exceeded expected " + ajustedDuration + " (adjusted reference), "
123 + referenceDuration + " (raw reference)");
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136 public static void assertPerformance(double currentPerformance,
137 long referencePerformance, double referenceBIPS)
138 throws AssertionFailedError {
139 double ajustedPerf = adjustExpectedPerformance(referencePerformance,
140 referenceBIPS);
141 if (currentPerformance * SLACK_FACTOR < ajustedPerf) {
142 throw new AssertionFailedError(currentPerformance + " below expected "
143 + ajustedPerf + " (adjusted), " + referencePerformance + " (raw)");
144 }
145 }
146
147 private static double adjustExpectedPerformance(long referenceDuration,
148 double referenceBIPS) {
149 double currentBIPS = currentBIPS();
150 return referenceDuration * (currentBIPS / referenceBIPS);
151 }
152
153 private static double adjustExpectedDuration(long referenceDuration,
154 double referenceBIPS) {
155 double currentBIPS = currentBIPS();
156 System.out.println("currentBIPS=" + currentBIPS + " BIPS");
157 return referenceDuration * (referenceBIPS / currentBIPS);
158 }
159 }