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, long referenceDuration, double referenceBIPS) throws AssertionFailedError {
117 double ajustedDuration = adjustExpectedDuration(referenceDuration, referenceBIPS);
118 if (currentDuration > ajustedDuration * SLACK_FACTOR) {
119 throw new AssertionFailedError("current duration " + currentDuration + " exceeded expected " + ajustedDuration + " (adjusted reference), "
120 + referenceDuration + " (raw reference)");
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133 public static void assertPerformance(double currentPerformance, long referencePerformance, double referenceBIPS) throws AssertionFailedError {
134 double ajustedPerf = adjustExpectedPerformance(referencePerformance, referenceBIPS);
135 if (currentPerformance * SLACK_FACTOR < ajustedPerf) {
136 throw new AssertionFailedError(currentPerformance + " below expected " + ajustedPerf + " (adjusted), " + referencePerformance + " (raw)");
137 }
138 }
139
140 private static double adjustExpectedPerformance(long referenceDuration, double referenceBIPS) {
141 double currentBIPS = currentBIPS();
142 return referenceDuration * (currentBIPS / referenceBIPS);
143 }
144
145 private static double adjustExpectedDuration(long referenceDuration, double referenceBIPS) {
146 double currentBIPS = currentBIPS();
147 System.out.println("currentBIPS=" + currentBIPS + " BIPS");
148 return referenceDuration * (referenceBIPS / currentBIPS);
149 }
150 }