Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve performance #60

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arex-compare-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>arex-compare-parent</artifactId>
<groupId>com.arextest</groupId>
<version>0.2.15</version>
<version>0.2.16</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import com.arextest.diff.model.log.NodeEntity;
import com.arextest.diff.utils.ListUti;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Created by rchen9 on 2022/7/25.
Expand All @@ -29,8 +30,8 @@ public static void arrayCompare(Object obj1, Object obj2, CompareContext compare
compareContext);
}

List<Integer> leftComparedIndexes = new ArrayList<>();
List<Integer> rightComparedIndexes = new ArrayList<>();
Set<Integer> leftComparedIndexes = new HashSet<>();
Set<Integer> rightComparedIndexes = new HashSet<>();

// decide to use which indexSelector
IndexSelector indexSelector = IndexSelectorFactory.getIndexSelector(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.arextest.diff.compare.feature;

import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.List;
import java.util.Set;

public class GeneralIndexSelector implements IndexSelector {

@Override
public int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftComparedIndex,
public int findCorrespondLeftIndex(int curRightIndex, Set<Integer> leftComparedIndex,
ArrayNode obj1Array, ArrayNode obj2Array) {
if (obj1Array == null || obj1Array.isEmpty()) {
return -1;
Expand All @@ -18,7 +18,7 @@ public int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftCompared
}

@Override
public int findCorrespondRightIndex(int curLeftIndex, List<Integer> rightComparedIndex,
public int findCorrespondRightIndex(int curLeftIndex, Set<Integer> rightComparedIndex,
ArrayNode obj1Array, ArrayNode obj2Array) {
if (obj2Array == null || obj2Array.isEmpty()) {
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.arextest.diff.model.exception.FindErrorException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.List;
import java.util.Set;

public interface IndexSelector {

int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftComparedIndex,
int findCorrespondLeftIndex(int curRightIndex, Set<Integer> leftComparedIndex,
ArrayNode obj1Array, ArrayNode obj2Array) throws FindErrorException, Exception;

int findCorrespondRightIndex(int curLeftIndex, List<Integer> rightComparedIndex,
int findCorrespondRightIndex(int curLeftIndex, Set<Integer> rightComparedIndex,
ArrayNode obj1Array, ArrayNode obj2Array) throws Exception;

String judgeLeftIndexStandard(int leftIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.arextest.diff.handler.log.register.LogRegister;
import com.arextest.diff.model.compare.CompareContext;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ListKeyIndexSelector implements IndexSelector {

Expand All @@ -24,7 +24,7 @@ public ListKeyIndexSelector(Map<Integer, String> indexKeysLeft,
}

@Override
public int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftComparedIndexes,
public int findCorrespondLeftIndex(int curRightIndex, Set<Integer> leftComparedIndexes,
ArrayNode obj1Array, ArrayNode obj2Array) throws Exception {
int correspondLeftIndex = -1;
if (indexKeysRight != null) {
Expand All @@ -33,11 +33,12 @@ public int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftCompared
int cnt = 0;
boolean alreadyFind = false;
if (indexKeysLeft != null) {
for (Integer index : indexKeysLeft.keySet()) {
if (rightKey.equals(indexKeysLeft.get(index))) {
for (Map.Entry<Integer, String> entry : indexKeysLeft.entrySet()) {
if (rightKey.equals(entry.getValue())) {
cnt++;
if (correspondLeftIndex == -1 && !leftComparedIndexes.contains(index) && !alreadyFind) {
correspondLeftIndex = index;
if (correspondLeftIndex == -1 && !leftComparedIndexes.contains(entry.getKey())
&& !alreadyFind) {
correspondLeftIndex = entry.getKey();
alreadyFind = true;
}
}
Expand All @@ -54,7 +55,7 @@ public int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftCompared
}

@Override
public int findCorrespondRightIndex(int curLeftIndex, List<Integer> rightComparedIndexes,
public int findCorrespondRightIndex(int curLeftIndex, Set<Integer> rightComparedIndexes,
ArrayNode obj1Array, ArrayNode obj2Array) throws Exception {
// when indexKeyLef is null, obj1Array must be empty. but the indexKeyLeft also judge null;
int correspondRightIndex = -1;
Expand All @@ -64,16 +65,18 @@ public int findCorrespondRightIndex(int curLeftIndex, List<Integer> rightCompare
int cnt = 0;
boolean alreadyFind = false;
if (indexKeysRight != null) {
for (Integer index : indexKeysRight.keySet()) {
if (leftKey.equals(indexKeysRight.get(index))) {
for (Map.Entry<Integer, String> entry : indexKeysRight.entrySet()) {
if (leftKey.equals(entry.getValue())) {
cnt++;
if (correspondRightIndex == -1 && !rightComparedIndexes.contains(index)
if (correspondRightIndex == -1 && !rightComparedIndexes.contains(entry.getKey())
&& !alreadyFind) {
correspondRightIndex = index;
correspondRightIndex = entry.getKey();
alreadyFind = true;
}
}
}


}

if (cnt > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PrimitiveArrayIndexSelector implements IndexSelector {

Expand All @@ -22,7 +23,7 @@ public PrimitiveArrayIndexSelector(CompareContext compareContext) {
}

@Override
public int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftComparedIndex,
public int findCorrespondLeftIndex(int curRightIndex, Set<Integer> leftComparedIndex,
ArrayNode obj1Array, ArrayNode obj2Array) {
JsonNode jsonNode = obj2Array.get(curRightIndex);
if (leftIndexKeys.containsKey(jsonNode)) {
Expand All @@ -37,7 +38,7 @@ public int findCorrespondLeftIndex(int curRightIndex, List<Integer> leftCompared
}

@Override
public int findCorrespondRightIndex(int curLeftIndex, List<Integer> rightComparedIndex,
public int findCorrespondRightIndex(int curLeftIndex, Set<Integer> rightComparedIndex,
ArrayNode obj1Array, ArrayNode obj2Array) {
JsonNode jsonNode = obj1Array.get(curLeftIndex);
if (rightIndexKeys.containsKey(jsonNode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.arextest.diff.model.TransformConfig.TransformMethod;
import com.arextest.diff.model.log.NodeEntity;
import com.arextest.diff.utils.TransformUtil;
import com.arextest.diff.utils.JacksonHelperUtil;
import com.arextest.diff.utils.ListUti;
import com.arextest.diff.utils.StringUtil;
import com.arextest.diff.utils.TransformUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand All @@ -17,7 +17,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.MutablePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -61,7 +60,7 @@ public void getJSONParse(Object obj, Object preObj) {
ObjectNode jsonObject = (ObjectNode) obj;
List<String> names = JacksonHelperUtil.getNames(jsonObject);
for (String fieldName : names) {
currentNode.add(new NodeEntity(fieldName, 0));
currentNode.add(new NodeEntity(nameToLower ? fieldName.toLowerCase() : fieldName, 0));
Object objFieldValue = jsonObject.get(fieldName);
getJSONParse(objFieldValue, obj);
ListUti.removeLast(currentNode);
Expand All @@ -78,16 +77,19 @@ public void getJSONParse(Object obj, Object preObj) {
} else {

String value = ((JsonNode) obj).asText();
// TODO: 2022/9/20 improve the method to speed up
List<String> nodePath = nameToLower ? ListUti.convertToStringList(currentNode).stream()
.map(String::toLowerCase).collect(Collectors.toList())
: ListUti.convertToStringList(currentNode);

MutablePair<JsonNode, Boolean> objectBooleanPair = null;
if (transFormConfigMap != null && transFormConfigMap.containsKey(nodePath)) {
List<TransformMethod> transformMethodList = this.transFormConfigMap.get(nodePath);
objectBooleanPair = processCompress(value, this.pluginJarUrl, transformMethodList, preObj);
} else {
if (transFormConfigMap == null || transFormConfigMap.isEmpty()) {
objectBooleanPair = processStringParse(value, preObj);
} else {
List<String> nodePath = ListUti.convertToStringList(currentNode);
if (transFormConfigMap.containsKey(nodePath)) {
List<TransformMethod> transformMethodList = this.transFormConfigMap.get(nodePath);
objectBooleanPair = processCompress(value, this.pluginJarUrl, transformMethodList,
preObj);
} else {
objectBooleanPair = processStringParse(value, preObj);
}
}

if (objectBooleanPair.getKey() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
Expand All @@ -15,11 +14,8 @@ public class JacksonHelperUtil {
public static ObjectMapper objectMapper = new ObjectMapper();

public static List<String> getNames(ObjectNode objectNode) {
List<String> result = new ArrayList<>();
Iterator<String> stringIterator = objectNode.fieldNames();
while (stringIterator.hasNext()) {
result.add(stringIterator.next());
}
List<String> result = new ArrayList<>(objectNode.size());
objectNode.fieldNames().forEachRemaining(result::add);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ public static void nameConvert(Object object) {

for (String fieldName : names) {
JsonNode obj1FieldValue = jsonObj1.get(fieldName);
jsonObj1.set(fieldName.toLowerCase(), obj1FieldValue);
nameConvert(obj1FieldValue);
}
for (String fieldName : names) {
if (containsUpper(fieldName)) {
String lowerCase = fieldName.toLowerCase();
jsonObj1.set(lowerCase, obj1FieldValue);
if (fieldName != lowerCase) {
jsonObj1.remove(fieldName);
}
nameConvert(obj1FieldValue);
}
} else if (object instanceof ArrayNode) {
ArrayNode obj1Array = (ArrayNode) object;
Expand All @@ -35,12 +34,5 @@ public static void nameConvert(Object object) {
nameConvert(element);
}
}

}

public static boolean containsUpper(String name) {
return name.chars().anyMatch(
(int ch) -> Character.isUpperCase((char) ch)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testFindCorrespondLeftIndex() throws JsonProcessingException {
ArrayNode obj2 = objectMapper.readValue("[3,1,2]", ArrayNode.class);

int correspondLeftIndex = primitiveArrayIndexSelector.findCorrespondLeftIndex(0,
Collections.emptyList(), obj1, obj2);
Collections.emptySet(), obj1, obj2);
Assertions.assertEquals(2, correspondLeftIndex);
}

Expand All @@ -44,7 +44,7 @@ public void testFindCorrespondRightIndex() throws JsonProcessingException {
ArrayNode obj2 = objectMapper.readValue("[3,1,2]", ArrayNode.class);

int correspondRightIndex = primitiveArrayIndexSelector.findCorrespondRightIndex(0,
Collections.emptyList(), obj1, obj2);
Collections.emptySet(), obj1, obj2);
Assertions.assertEquals(1, correspondRightIndex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import com.arextest.diff.model.DecompressConfig;
import com.arextest.diff.model.enumeration.CategoryType;
import com.arextest.diff.model.enumeration.DiffResultCode;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -613,5 +617,37 @@ public void testStringEqualsWithArexPrefix() {
Assertions.assertEquals(0, result.getCode());
}

@Disabled
@Test
public void testCpuUsed() throws IOException {
CompareSDK compareSDK = new CompareSDK();
compareSDK.getGlobalOptions().putNameToLower(true).putNullEqualsEmpty(true).putIgnoreNodeSet(
Collections.singleton("timestamp"));

CompareOptions compareOptions = CompareOptions.options().putListSortConfig(
Arrays.asList("fareinfolist"),
Arrays.asList(Arrays.asList("id"))
);

String baseMsg = readStringFromFile("./baseDecompress.txt");
String testMsg = readStringFromFile("./testDecompress.txt");
CompareResult result = compareSDK.compare(baseMsg, testMsg, compareOptions);
Assertions.assertEquals(0, result.getCode());

}

// reading a string from a file
public static String readStringFromFile(String filePath) throws IOException {
FileInputStream fis = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
sb.append(new String(buffer, 0, bytesRead));
}
fis.close();
return sb.toString();
}


}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.arextest</groupId>
<artifactId>arex-compare-parent</artifactId>
<packaging>pom</packaging>
<version>0.2.15</version>
<version>0.2.16</version>
<modules>
<module>arex-compare-extension</module>
<module>arex-compare-core</module>
Expand Down
Loading