Skip to content

Commit 10bb35a

Browse files
committed
More insights into comentary for #157
1 parent 2447bc4 commit 10bb35a

File tree

1 file changed

+114
-26
lines changed

1 file changed

+114
-26
lines changed

src/test/java/com/networknt/schema/ThresholdMixinPerfTest.java

Lines changed: 114 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.networknt.schema;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.BigIntegerNode;
45
import com.fasterxml.jackson.databind.node.DecimalNode;
56
import com.fasterxml.jackson.databind.node.DoubleNode;
7+
import com.fasterxml.jackson.databind.node.LongNode;
68
import com.fasterxml.jackson.databind.node.TextNode;
9+
import org.junit.Before;
710
import org.junit.Ignore;
811
import org.junit.Test;
912

10-
import java.lang.reflect.InvocationTargetException;
1113
import java.math.BigDecimal;
14+
import java.math.BigInteger;
15+
16+
import static java.lang.String.format;
17+
import static java.lang.System.out;
1218

1319
@Ignore
1420
public class ThresholdMixinPerfTest {
21+
private static long thresholdIntegral = Long.MAX_VALUE - 1;
22+
23+
24+
private final LongNode maximumLong = new LongNode(thresholdIntegral);
25+
private final BigIntegerNode maximumBigInt = new BigIntegerNode(BigInteger.valueOf(thresholdIntegral));
26+
27+
private final LongNode valueLong = new LongNode(Long.MAX_VALUE);
28+
private final BigIntegerNode valueBigInt = new BigIntegerNode(BigInteger.valueOf(Long.MAX_VALUE));
1529

1630
// private final double threshold = Double.MAX_VALUE - 1;
1731
private final double threshold = 1797693.134E+5D;
@@ -29,62 +43,108 @@ public class ThresholdMixinPerfTest {
2943
private final int executeTimes = 200000;
3044
private final boolean excludeEqual = false;
3145

32-
@Test
33-
public void testDoubleVsBigDecimalOnCompareTimeViaMixins() throws InvocationTargetException, IllegalAccessException {
46+
double baseTimeForDouble;
47+
private double baseTimeForLong;
3448

35-
double baseTime = getAvgTimeViaMixin(asDouble, valueDouble, executeTimes);
36-
System.out.println(String.format("Base execution time (comparing two DoubleNodes) %f ns \n", baseTime));
49+
@Before
50+
public void baseTimeEstimate() {
51+
baseTimeForDouble = getAvgTimeViaMixin(asDouble, valueDouble, executeTimes);
52+
out.println(format("Base execution time (comparing two DoubleNodes) %f ns", baseTimeForDouble));
3753

54+
baseTimeForLong = getAvgTimeViaMixin(asLong, valueLong, executeTimes);
55+
out.println(format("Base execution time (comparing two LongeNodes) %f ns \n", baseTimeForDouble));
56+
}
57+
58+
@Test
59+
public void currentTimeEstimate() {
60+
out.println("Estimating time for current implementation:");
3861
double currentAvgTimeOnDouble = getAvgTimeViaMixin(currentImplementationDouble, valueDouble, executeTimes);
39-
System.out.println(String.format("Current double on double execution time %f ns, %f times slower", currentAvgTimeOnDouble, (currentAvgTimeOnDouble/baseTime)));
62+
out.println(format("Current double on double execution time %f ns, %f times slower", currentAvgTimeOnDouble, (currentAvgTimeOnDouble / baseTimeForDouble)));
4063

4164
double currentAvgTimeOnDecimal = getAvgTimeViaMixin(currentImplementationDouble, valueDecimal, executeTimes);
42-
System.out.println(String.format("Current double on decimal execution time %f ns, %f times slower", currentAvgTimeOnDecimal, (currentAvgTimeOnDecimal/baseTime)));
65+
out.println(format("Current double on decimal execution time %f ns, %f times slower", currentAvgTimeOnDecimal, (currentAvgTimeOnDecimal / baseTimeForDouble)));
4366

4467
double currentAvgTimeOnText = getAvgTimeViaMixin(currentImplementationDouble, valueTextual, executeTimes);
45-
System.out.println(String.format("Current double on text execution time %f ns, %f times slower", currentAvgTimeOnText, (currentAvgTimeOnText/baseTime)));
68+
out.println(format("Current double on text execution time %f ns, %f times slower", currentAvgTimeOnText, (currentAvgTimeOnText / baseTimeForDouble)));
4669

4770
double currentAvgTimeDecimalOnDouble = getAvgTimeViaMixin(currentImplementationDecimal, valueDouble, executeTimes);
48-
System.out.println(String.format("Current decimal on double execution time %f ns, %f times slower", currentAvgTimeDecimalOnDouble, (currentAvgTimeDecimalOnDouble/baseTime)));
71+
out.println(format("Current decimal on double execution time %f ns, %f times slower", currentAvgTimeDecimalOnDouble, (currentAvgTimeDecimalOnDouble / baseTimeForDouble)));
4972

5073
double currentAvgTimeDecimalOnDecimal = getAvgTimeViaMixin(currentImplementationDecimal, valueDecimal, executeTimes);
51-
System.out.println(String.format("Current decimal on decimal execution time %f ns, %f times slower", currentAvgTimeDecimalOnDecimal, (currentAvgTimeDecimalOnDecimal/baseTime)));
74+
out.println(format("Current decimal on decimal execution time %f ns, %f times slower", currentAvgTimeDecimalOnDecimal, (currentAvgTimeDecimalOnDecimal / baseTimeForDouble)));
5275

5376
double currentAvgTimeDecimalOnText = getAvgTimeViaMixin(currentImplementationDecimal, valueTextual, executeTimes);
54-
System.out.println(String.format("Current decimal on text execution time %f ns, %f times slower", currentAvgTimeDecimalOnText, (currentAvgTimeDecimalOnText/baseTime)));
77+
out.println(format("Current decimal on text execution time %f ns, %f times slower", currentAvgTimeDecimalOnText, (currentAvgTimeDecimalOnText / baseTimeForDouble)));
5578

56-
System.out.println(String.format("Cumulative average: %f\n\n", (currentAvgTimeOnDouble+currentAvgTimeOnDecimal+currentAvgTimeOnText+currentAvgTimeDecimalOnDouble+currentAvgTimeDecimalOnDecimal+currentAvgTimeDecimalOnText)/6.0d));
79+
out.println(format("Cumulative average: %f\n\n", (currentAvgTimeOnDouble + currentAvgTimeOnDecimal + currentAvgTimeOnText + currentAvgTimeDecimalOnDouble + currentAvgTimeDecimalOnDecimal + currentAvgTimeDecimalOnText) / 6.0d));
80+
}
5781

82+
@Test
83+
public void allInOneAproachTimeEstimate() {
84+
out.println("Estimating time threshold value agnostic mixin (aka allInOne):");
5885
double allInOneDoubleOnDouble = getAvgTimeViaMixin(allInOneDouble, valueDouble, executeTimes);
59-
System.out.println(String.format("AllInOne double on double execution time %f ns, %f times slower", allInOneDoubleOnDouble, (allInOneDoubleOnDouble/baseTime)));
86+
out.println(format("AllInOne double on double execution time %f ns, %f times slower", allInOneDoubleOnDouble, (allInOneDoubleOnDouble/ baseTimeForDouble)));
6087

6188
double allInOneDoubleOnDecimal = getAvgTimeViaMixin(allInOneDouble, valueDecimal, executeTimes);
62-
System.out.println(String.format("AllInOne double on decimal execution time %f ns, %f times slower", allInOneDoubleOnDecimal, (allInOneDoubleOnDecimal/baseTime)));
89+
out.println(format("AllInOne double on decimal execution time %f ns, %f times slower", allInOneDoubleOnDecimal, (allInOneDoubleOnDecimal/ baseTimeForDouble)));
6390

6491
double allInOneDoubleOnText = getAvgTimeViaMixin(allInOneDouble, valueTextual, executeTimes);
65-
System.out.println(String.format("AllInOne double on text execution time %f ns, %f times slower", allInOneDoubleOnText, (allInOneDoubleOnText/baseTime)));
92+
out.println(format("AllInOne double on text execution time %f ns, %f times slower", allInOneDoubleOnText, (allInOneDoubleOnText/ baseTimeForDouble)));
6693

6794
double allInOneDecimalOnDouble = getAvgTimeViaMixin(allInOneDecimal, valueDouble, executeTimes);
68-
System.out.println(String.format("AllInOne decimal on double execution time %f ns, %f times slower", allInOneDecimalOnDouble, (allInOneDecimalOnDouble/baseTime)));
95+
out.println(format("AllInOne decimal on double execution time %f ns, %f times slower", allInOneDecimalOnDouble, (allInOneDecimalOnDouble/ baseTimeForDouble)));
6996

7097
double allInOneDecimalOnDecimal = getAvgTimeViaMixin(allInOneDecimal, valueDecimal, executeTimes);
71-
System.out.println(String.format("AllInOne decimal on decimal execution time %f ns, %f times slower", allInOneDecimalOnDecimal, (allInOneDecimalOnDecimal/baseTime)));
98+
out.println(format("AllInOne decimal on decimal execution time %f ns, %f times slower", allInOneDecimalOnDecimal, (allInOneDecimalOnDecimal/ baseTimeForDouble)));
7299

73100
double allInOneDecimalOnText = getAvgTimeViaMixin(allInOneDecimal, valueTextual, executeTimes);
74-
System.out.println(String.format("AllInOne decimal on text execution time %f ns, %f times slower", allInOneDecimalOnText, (allInOneDecimalOnText/baseTime)));
101+
out.println(format("AllInOne decimal on text execution time %f ns, %f times slower", allInOneDecimalOnText, (allInOneDecimalOnText/ baseTimeForDouble)));
102+
103+
out.println(format("Cumulative average: %f\n\n", (allInOneDoubleOnDouble+allInOneDoubleOnDecimal+allInOneDoubleOnText+allInOneDecimalOnDouble+allInOneDecimalOnDecimal+allInOneDecimalOnText)/6.0d));
104+
}
75105

76-
System.out.println(String.format("Cumulative average: %f\n\n", (allInOneDoubleOnDouble+allInOneDoubleOnDecimal+allInOneDoubleOnText+allInOneDecimalOnDouble+allInOneDecimalOnDecimal+allInOneDecimalOnText)/6.0d));
106+
@Test
107+
public void specificCaseForEachThresholdValue() {
108+
out.println("Estimating time for specific cases:");
109+
double doubleValueAvgTime = getAvgTimeViaMixin(typedThreshold, valueDouble, executeTimes);
110+
out.println(format("Typed threshold execution time %f ns, %f times slower", doubleValueAvgTime, (doubleValueAvgTime/ baseTimeForDouble)));
77111

78-
double typedThresholdOnDouble = getAvgTimeViaMixin(typedThreshold, valueDouble, executeTimes);
79-
System.out.println(String.format("Typed threshold execution time %f ns, %f times slower", typedThresholdOnDouble, (typedThresholdOnDouble/baseTime)));
112+
double decimalValueAvgTime = getAvgTimeViaMixin(typedThreshold, valueDecimal, executeTimes);
113+
out.println(format("Typed threshold execution time %f ns, %f times slower", decimalValueAvgTime, (decimalValueAvgTime/ baseTimeForDouble)));
80114

81-
double typedThresholdOnDecimal = getAvgTimeViaMixin(typedThreshold, valueDecimal, executeTimes);
82-
System.out.println(String.format("Typed threshold execution time %f ns, %f times slower", typedThresholdOnDecimal, (typedThresholdOnDecimal/baseTime)));
115+
double textValueAvgTime = getAvgTimeViaMixin(typedThreshold, valueTextual, executeTimes);
116+
out.println(format("Typed threshold execution time %f ns, %f times slower", textValueAvgTime, (textValueAvgTime/ baseTimeForDouble)));
83117

84-
double typedThresholdOnText = getAvgTimeViaMixin(typedThreshold, valueTextual, executeTimes);
85-
System.out.println(String.format("Typed threshold execution time %f ns, %f times slower", typedThresholdOnText, (typedThresholdOnText/baseTime)));
118+
out.println(format("Cumulative average: %f\n\n", (doubleValueAvgTime+decimalValueAvgTime+textValueAvgTime)/3.0d));
119+
}
86120

87-
System.out.println(String.format("Cumulative average: %f\n\n", (typedThresholdOnDouble+typedThresholdOnDecimal+typedThresholdOnText)/3.0d));
121+
@Test
122+
public void noMixinsFloatingTimeEstimate() {
123+
out.println("Estimating time no mixins at all (floating point values):");
124+
double doubleValueAvgTime = getAvgTimeViaMixin(oneMixinForIntegerAndNumber, valueDecimal, executeTimes);
125+
out.println(format("No mixins with double value time %f ns, %f times slower", doubleValueAvgTime, (doubleValueAvgTime/ baseTimeForDouble)));
126+
127+
double decimalValueAvgTime = getAvgTimeViaMixin(oneMixinForIntegerAndNumber, valueDecimal, executeTimes);
128+
out.println(format("No mixins with decimal value time %f ns, %f times slower", decimalValueAvgTime, (decimalValueAvgTime/ baseTimeForDouble)));
129+
130+
double textValueAvgTime = getAvgTimeViaMixin(oneMixinForIntegerAndNumber, valueTextual, executeTimes);
131+
out.println(format("No mixins with text value time %f ns, %f times slower", textValueAvgTime, (textValueAvgTime/ baseTimeForDouble)));
132+
out.println(format("Cumulative average: %f\n\n",
133+
(doubleValueAvgTime+decimalValueAvgTime+textValueAvgTime)/3.0d));
134+
}
135+
136+
@Test
137+
public void noMixinsIntegralTimeEstimate() {
138+
double longValueAvgTime = getAvgTimeViaMixin(oneMixinForIntegerAndNumber, new LongNode((long)value), executeTimes);
139+
out.println(format("No mixins with long value time %f ns, %f times slower", longValueAvgTime, (longValueAvgTime/ baseTimeForLong)));
140+
141+
double bigIntValueAvgTime = getAvgTimeViaMixin(oneMixinForIntegerAndNumber, new BigIntegerNode(BigInteger.valueOf((long)value)), executeTimes);
142+
out.println(format("No mixins with big int value time %f ns, %f times slower", bigIntValueAvgTime, (bigIntValueAvgTime/ baseTimeForLong)));
143+
144+
double textIntValueAvgTime = getAvgTimeViaMixin(oneMixinForIntegerAndNumber, new TextNode(String.valueOf((long)value)), executeTimes);
145+
out.println(format("No mixins with text value time %f ns, %f times slower", textIntValueAvgTime, (textIntValueAvgTime/ baseTimeForLong)));
146+
out.println(format("Cumulative average: %f\n\n",
147+
(longValueAvgTime+bigIntValueAvgTime+textIntValueAvgTime)/3.0d));
88148
}
89149

90150
ThresholdMixin allInOneDouble = new AllInOneThreshold(maximumDouble, false);
@@ -147,6 +207,20 @@ public String thresholdValue() {
147207
}
148208
};
149209

210+
ThresholdMixin asLong = new ThresholdMixin() {
211+
@Override
212+
public boolean crossesThreshold(JsonNode node) {
213+
long lm = maximumLong.longValue();
214+
long val = node.longValue();
215+
return lm < val || (excludeEqual && lm == val);
216+
}
217+
218+
@Override
219+
public String thresholdValue() {
220+
return maximumText;
221+
}
222+
};
223+
150224
ThresholdMixin typedThreshold = new ThresholdMixin() {
151225
@Override
152226
public boolean crossesThreshold(JsonNode node) {
@@ -228,6 +302,20 @@ public String thresholdValue() {
228302
}
229303
};
230304

305+
ThresholdMixin oneMixinForIntegerAndNumber = new ThresholdMixin() {
306+
@Override
307+
public boolean crossesThreshold(JsonNode node) {
308+
BigDecimal value = new BigDecimal(node.asText());
309+
int compare = value.compareTo(max);
310+
return compare > 0 || (excludeEqual && compare == 0);
311+
}
312+
313+
@Override
314+
public String thresholdValue() {
315+
return null;
316+
}
317+
};
318+
231319
private double getAvgTimeViaMixin(ThresholdMixin mixin, JsonNode value, int iterations) {
232320
boolean excludeEqual = false;
233321
long totalTime = 0;

0 commit comments

Comments
 (0)