Skip to content

Commit d97bc20

Browse files
committed
Perf Test Simplified
1 parent 5c11336 commit d97bc20

2 files changed

Lines changed: 90 additions & 74 deletions

File tree

Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.techatpark.sjson;
22

3-
import com.fasterxml.jackson.databind.JsonNode;
43
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.google.gson.JsonElement;
4+
import com.google.gson.Gson;
65
import com.google.gson.JsonParser;
76
import com.techatpark.sjson.util.TestDataProvider;
87
import org.github.jamm.MemoryMeter;
98
import org.json.JSONObject;
9+
import org.junit.jupiter.api.AfterAll;
1010
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.TestInstance;
1112
import org.junit.jupiter.params.ParameterizedTest;
1213
import org.junit.jupiter.params.provider.MethodSource;
1314

@@ -16,114 +17,134 @@
1617
import java.io.IOException;
1718
import java.io.StringReader;
1819
import java.nio.file.Path;
20+
import java.util.Map;
1921
import java.util.Set;
2022

21-
23+
/**
24+
* Reads Json Objects as Map<String,Object> using various parsers,
25+
* Compare them with Json.parse() by Memory usage and performance
26+
*/
27+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2228
class PerformanceTest {
2329

24-
public static final String ANSI_RESET = "\u001B[0m";
25-
public static final String ANSI_RED = "\u001B[31m";
26-
public static final String ANSI_GREEN = "\u001B[32m";
27-
public static final String ANSI_WHITE = "\u001B[37m";
30+
private static final String ANSI_RESET = "\u001B[0m";
31+
private static final String ANSI_RED = "\u001B[31m";
32+
private static final String ANSI_GREEN = "\u001B[32m";
33+
private static final String ANSI_WHITE = "\u001B[37m";
34+
35+
private final ObjectMapper jackson = new ObjectMapper();
36+
private final Gson gson = new Gson();
37+
38+
private final MemoryMeter meter = MemoryMeter.builder().build();
2839

40+
long totalOursTime = 0;
41+
long totalOrgJsonTime = 0;
42+
long totalJacksonTime = 0;
43+
long totalGsonTime = 0;
2944

30-
public final ObjectMapper jackson = new ObjectMapper();
45+
long totalOursSize = 0;
46+
long totalOrgJsonSize = 0;
47+
long totalJacksonSize = 0;
48+
long totalGsonSize = 0;
49+
50+
int fileCount = 0;
51+
3152

3253
@ParameterizedTest
3354
@MethodSource("jsonFilesProvider")
3455
void testRead(Path path) throws IOException {
35-
MemoryMeter meter = MemoryMeter.builder().build();
36-
Object ourJsonObject;
37-
JSONObject orgJSONObject;
38-
JsonNode jacksonJsonNode;
39-
JsonElement gsonObject;
40-
41-
long start;
56+
fileCount++;
4257

43-
long jacksonsTime, jsonTime, gsonTime, oursTime;
44-
long jacksonsSize, jsonSize, gsonSize, oursSize;
58+
// Our Json
59+
long start = System.nanoTime();
60+
Object ourJsonObject = Json.read(new BufferedReader(new FileReader(path.toFile())));
61+
long oursTime = System.nanoTime() - start;
62+
long oursSize = meter.measureDeep(ourJsonObject);
4563

46-
// Measure SJson
64+
// Org JSON
4765
start = System.nanoTime();
48-
ourJsonObject = Json.read(new BufferedReader(new FileReader(path.toFile())));
49-
oursTime = System.nanoTime() - start;
50-
oursSize = meter.measureDeep(ourJsonObject);
66+
Map orgJsonObject = new JSONObject(new BufferedReader(new FileReader(path.toFile()))).toMap();
67+
long orgjsonTime = System.nanoTime() - start;
68+
long orgjsonSize = meter.measureDeep(orgJsonObject);
5169

52-
// Measure Org Json
70+
// Jackson
5371
start = System.nanoTime();
54-
orgJSONObject = new JSONObject(new BufferedReader(new FileReader(path.toFile())));
55-
jsonTime = System.nanoTime() - start - oursTime;
56-
jsonSize = meter.measureDeep(orgJSONObject);
72+
Map jacksonJson = jackson.readValue(new BufferedReader(new FileReader(path.toFile())), Map.class);
73+
long jacksonsTime = System.nanoTime() - start;
74+
long jacksonsSize = meter.measureDeep(jacksonJson);
5775

58-
// Measure Jackson
76+
// Gson
5977
start = System.nanoTime();
60-
jacksonJsonNode = jackson.readTree(new BufferedReader(new FileReader(path.toFile())));
61-
jacksonsTime = System.nanoTime() - start - oursTime;
62-
jacksonsSize = meter.measureDeep(jacksonJsonNode);
78+
Map gsonJson = gson.fromJson(new BufferedReader(new FileReader(path.toFile())), Map.class);
79+
long gsonTime = System.nanoTime() - start;
80+
long gsonSize = meter.measureDeep(gsonJson);
6381

64-
// Measure Gson
65-
start = System.nanoTime();
66-
gsonObject = JsonParser.parseReader(new BufferedReader(new FileReader(path.toFile())));
67-
gsonTime = System.nanoTime() - start - oursTime;
68-
gsonSize = meter.measureDeep(gsonObject);
82+
// Accumulate totals
83+
totalOursTime += oursTime;
84+
totalOrgJsonTime += orgjsonTime;
85+
totalJacksonTime += jacksonsTime;
86+
totalGsonTime += gsonTime;
87+
88+
totalOursSize += oursSize;
89+
totalOrgJsonSize += orgjsonSize;
90+
totalJacksonSize += jacksonsSize;
91+
totalGsonSize += gsonSize;
6992

7093
// Check Correctness of the parser
71-
Assertions.assertEquals(gsonObject,
94+
Assertions.assertEquals(JsonParser.parseReader(new StringReader(jackson.writeValueAsString(gsonJson))),
7295
JsonParser.parseReader(new StringReader(jackson.writeValueAsString(ourJsonObject))),
7396
"Reverse JSON Failed for " + path);
7497

98+
// Print formatted table
7599
System.out.format("%33s%20s%20s%20s%10s%20s%20s%20s\n",
76100
ANSI_RESET + path.getFileName(),
77-
getSizeDisplay(jsonSize, oursSize),
78-
getSizeDisplay(jacksonsSize, oursSize),
79-
getSizeDisplay(gsonSize, oursSize),
101+
getMemoryDisplay(orgjsonSize, oursSize),
102+
getMemoryDisplay(jacksonsSize, oursSize),
103+
getMemoryDisplay(gsonSize, oursSize),
104+
ANSI_WHITE + "|",
105+
getTimeDisplay(orgjsonTime, oursTime),
106+
getTimeDisplay(jacksonsTime, oursTime),
107+
getTimeDisplay(gsonTime, oursTime)
108+
);
109+
}
110+
111+
@AfterAll
112+
void printAverages() {
113+
System.out.println(ANSI_RESET + "\nAverage Memory and Time Usage (nanoseconds and bytes):");
114+
System.out.format("%33s%20s%20s%20s%10s%20s%20s%20s\n",
115+
ANSI_RESET + "Average",
116+
getMemoryDisplay(totalOrgJsonSize / fileCount, totalOursSize / fileCount),
117+
getMemoryDisplay(totalJacksonSize / fileCount, totalOursSize / fileCount),
118+
getMemoryDisplay(totalGsonSize / fileCount, totalOursSize / fileCount),
80119
ANSI_WHITE + "|",
81-
getTimeDisplay(jsonTime),
82-
getTimeDisplay(jacksonsTime),
83-
getTimeDisplay(gsonTime)
120+
getTimeDisplay(totalOrgJsonTime / fileCount, totalOursTime / fileCount),
121+
getTimeDisplay(totalJacksonTime / fileCount, totalOursTime / fileCount),
122+
getTimeDisplay(totalGsonTime / fileCount, totalOursTime / fileCount)
84123
);
85124
}
86125

87-
/**
88-
* Provides paths to JSON files for parameterized tests.
89-
*
90-
* @return Stream of paths to JSON files
91-
* @throws IOException if there is an issue listing files
92-
*/
93126
private static Set<Path> jsonFilesProvider() throws IOException {
94127
return TestDataProvider.getJSONObjectFiles();
95128
}
96129

97-
/**
98-
* Get Timing in a Color Coded Format.
99-
* @param time
100-
* @return time as text
101-
*/
102-
private String getTimeDisplay(final long time) {
130+
private String getTimeDisplay(final long time, final long oursTime) {
103131
StringBuilder builder = new StringBuilder();
104-
if(time < 0) {
105-
builder.append(ANSI_RED);
106-
}
107-
else {
132+
long gap = time - oursTime;
133+
if (gap > 0) {
108134
builder.append(ANSI_GREEN);
135+
} else {
136+
builder.append(ANSI_RED);
109137
}
110138
return builder.append(time).toString();
111139
}
112140

113-
/**
114-
* Get Size in a Color Coded Format.
115-
* @param size
116-
* @return size as text
117-
*/
118-
private String getSizeDisplay(final long size,final long ourSize) {
141+
private String getMemoryDisplay(final long size, final long ourSize) {
119142
StringBuilder builder = new StringBuilder();
120-
long gap = size-ourSize;
121-
if(gap < 0) {
122-
builder.append(ANSI_RED);
123-
}
124-
else {
143+
if (size > ourSize) {
125144
builder.append(ANSI_GREEN);
145+
} else {
146+
builder.append(ANSI_RED);
126147
}
127-
return builder.append(gap).toString();
148+
return builder.append(size).toString();
128149
}
129150
}

src/test/resources/samples/product.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)