lukas-krecan / JsonUnit

Compare JSON in your Unit Tests
Apache License 2.0
871 stars 114 forks source link

Performance issue while comparing big jsons with similar keys #782

Closed dongelci closed 2 weeks ago

dongelci commented 3 weeks ago

Hi,

This is a following issue wiremock/wiremock#2705, which I opened at wiremock since the issue is happening in json-unit code. wiremock is using json-unit for a while comparing stubs with requests. We use wiremock to stub external systems and have case-to-case big jsons in request body, which consists of similar keys in arrays. Since wiremock started using json-unit, response times increased enormously, which cause tests to fail. Root cause of performance issue is happening at invocation of diff.similar(). https://github.com/wiremock/wiremock/blob/2755c2fd5088d68a9d11426c71afbcc565c3913f/src/main/java/com/github/tomakehurst/wiremock/matching/EqualToJsonPattern.java#L96 I provided a unit test in forked project, which you can check. Jsons are anonymized. NormalisedNumberComparator is added only to keep configuration same as in wiremock. https://github.com/dongelci/JsonUnit/tree/gh-782 In unit test I am expecting diff.similar() method to finish in miliseconds level but needs nearly 7-8s. With configuration in wiremock response time is around 10s. If we have 7 stubs (expecteds) in wiremock, this time will be muliplied and request ends up with a response time over 40s. You can also check mappingShouldResponseBeforeTimeoutBigJsonWithCommonKeys test case in MappingsAcceptanceTest. https://github.com/dongelci/wiremock/blob/71fff1c12b521f5af2e1d0cd74142a1bf0c5c309/src/test/java/com/github/tomakehurst/wiremock/MappingsAcceptanceTest.java#L250

Questions:

  1. Is there a problem with configuration in wiremock?
  2. If no problem with configuration, could you please provide a fix?

Thanks

lukas-krecan commented 2 weeks ago

Hi, thanks for reporting. I will look into it.

lukas-krecan commented 2 weeks ago

There were two main inefficencies, I have a fix here https://github.com/lukas-krecan/JsonUnit/pull/783

lukas-krecan commented 2 weeks ago

Released as 3.3.0, let me know if it helped

dongelci commented 2 weeks ago

Now request time is around 2s instead of 40-55s. I confirm significant improvement in wiremock.

Thanks for fast reply and fix.