<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fernando Soler David on Medium]]></title>
        <description><![CDATA[Stories by Fernando Soler David on Medium]]></description>
        <link>https://medium.com/@soler_29918?source=rss-4c236b9aa0d3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*_p7sIsHjoFfkiMWRSQFg2w.jpeg</url>
            <title>Stories by Fernando Soler David on Medium</title>
            <link>https://medium.com/@soler_29918?source=rss-4c236b9aa0d3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:12:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@soler_29918/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JVM Memory, Mockito and a trip]]></title>
            <link>https://medium.com/strands-tech-corner/jvm-memory-mockito-trip-57d755b14b5c?source=rss-4c236b9aa0d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/57d755b14b5c</guid>
            <category><![CDATA[profiling]]></category>
            <category><![CDATA[load-testing]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Fernando Soler David]]></dc:creator>
            <pubDate>Tue, 10 Sep 2019 08:30:32 GMT</pubDate>
            <atom:updated>2019-11-19T11:36:04.389Z</atom:updated>
            <content:encoded><![CDATA[<p>Most of us are familiar with <a href="https://site.mockito.org/"><strong>Mockito</strong></a>. Mockito is an open-source library widely used for unit testing. As its name suggests, it is a mock library for Java that allows the creation of objects of given class or interface. Once created, the mock will remember all interactions. Then, you can selectively verify whatever iterations you are interested in within your unit test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*6FnftUCJeI5F_426tdT2hA.jpeg" /><figcaption>Cartoon by Rick London and Rich Diesslin</figcaption></figure><p>Load testing is an important phase of the release process of each component at Strands. The load testing phase is at least executed once. This process is a simulation of multiple users/events using a given component at the same time and working with it concurrently. The following phases are identified:</p><p><strong><em>a) Performance testing </em></strong><br>We gradually increase the load by adding more and more virtual users/events to the test and checking the performance parameters of the system at any test phase.</p><p>The main things we monitor are:</p><ul><li><em>Latency, average response time</em></li><li><em>Throughput, number of processed requests per second</em></li><li><em>Error rate</em></li></ul><p><strong><em>b) Capacity testing </em></strong><br>This type of test answers the most common question in load testing: “how many concurrent users can the component handle while maintaining good response time and error rate?” Again, we add virtual users gradually, but in this case, we know the performance criteria in advance and just need to check that they are observed.</p><p><strong><em>c) Stress testing </em></strong><br>Every system has a capacity limit. When the load goes beyond it, the component starts to respond very slowly and even to produce errors. When this limit is reached, we check if the component is handling the stress correctly: does it produce smooth overload notifications without crashing? When the load is reduced back to a regular level, the component returns to normal operation retaining the performance characteristics.</p><p><strong><em>d) Endurance testing</em></strong><br>This type of testing (also called “soak testing”) is used to check if the system can handle the load for a long time or a large number of transactions. It usually reveals various types of resource allocation problems. For example, a small memory leak isn’t evident on a quick test but it will influence the performance after a long time. For endurance testing, it’s recommended to use changing the periodic load to provoke resource reallocation.</p><p>I have given a general overview of the load testing phase to emphasize that it is an important process within Strands. Now that we are on the same page, Let’s go through an imaginary example.</p><p>Our manager shows up nervous and starts explaining that one of our clients in the is experimenting an <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html"><strong><em>OutOfMemoryError</em></strong></a> after running the component several hours in a test environment. The application is probably holding too many objects and has run out of memory. The first thing we think is “that’s not possible. We’d run the endurance test for a whole weekend and the JVM heap was all the time within a range.” However, the customer is always right, so the issue has to be some corner case you haven’t covered.</p><p>In the following days we focused on the finding out the source of this issue. We tried to reproduce the same environment as the customer:</p><ul><li>Limited the maximum memory to 2GB: -Xmx2GB</li><li>Generate with JMeter the same amount of events/second</li><li>Use similar hardware (CPU, Memory,..)</li><li>Run the jar/war within the same Application Server/Container</li></ul><p>Our main goal was to know what kind of objects were consuming large amounts of memory. There are several ways to do that. One approach is to generate <strong><em>Heap Dumps</em></strong> of the application. A heap dump is a snapshot of the heap that can be used to analyze the distribution of objects. The heap dump can be triggered from the command line using jcmp or jmap. If you are profiling the application, the profilers usually have the option to capture the snapshot. Finally, if you are running a<strong><em> Spring Boot</em></strong> application, the <strong><em>Spring Actuators</em></strong> library provides a REST endpoint to generate the heap dump. In order to analyze the heap dump, you can use some tools like Eclipse Memory Analyzer Tool, JProfiler or some online services.<br> <br>We were using <a href="https://www.ej-technologies.com/products/jprofiler/overview.html"><strong>JProfiler</strong></a> as a tool for profiling and analyzing the different heap dumps. After analyzing all heap dumps, we weren’t able to identify any type of Object consuming large amounts of data. The JVM heap was stable all the time within a range. We weren’t able to reproduce the <strong><em>OutOfMemoryError</em></strong> in our environment. The problem must be something else.</p><p>After 3 days, the manager came back and informed us that a member of the team had to travel to the client’s offices and try to identify the issue from within the customers’ infrastructure.</p><p>We accepted that there was no other choice than traveling there asap, so there we went. Once there, with help from their IT team, we were able to run our tests and profiling tools on their environment and application, finding out some objects consuming up to 1Gb. How could that be? In our monitoring tasks, we’d never detected it. How did that happen? Well, as you can imagine, it turned out we were storing a lot of Mockito objects.</p><p>Let’s try to understand how the Mockito object appeared in the source code of the application. The first thing would be to explain how business units are organized in Strands. There are two main teams: Engineering and Professional Services. The Engineering team is focused on product development. This team rarely has contact with customers unless they’re working on the development of a new product. On the other hand, the Professional Services team is in contact with the customer and is responsible for the integration of Strands’ products in its infrastructure. From time to time, this team needs to make some customizations over the default functionality.</p><p>How is the customization implemented? Our development is based on the <strong>S.O.L.I.D.</strong> principles. Our classes are designed with the <strong><em>Dependency Inversion Principle </em></strong>in mind. This design system says that our classes should depend on abstractions, not on concrete classes. Instead of being dependent on the implementation details, our classes are dependent on the interfaces. Thanks to that, if having a class with different behavior is needed, we just have to implement the interface. Let’s say we have the following interface:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*hOqHp0_J3-2VwO79YzTE1w.png" /><figcaption>Image 2: Interface definition of a message service</figcaption></figure><p>The project already provides some implementations: <em>JMSMessageService</em>, <em>KafkaMessageService</em>, <em>MailMessageService</em>, etc. However, this customer asked for a different behavior on that point in the data flow. They didn’t want to send any message. A mock implementation of the contract was needed. An example would’ve been:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*iZmMm_r-vZcT6Lz2zzts1g.png" /><figcaption>Image 3: Bean definition of an empty implementation of the MessageService interface</figcaption></figure><p>What happened, in reality, was the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*JSB_KDNmu1TazqZZfqC3pw.png" /><figcaption>Image 4: Bean definition of a mockito implementation of the MessageService interface</figcaption></figure><p>Ok. This is a mock class that doesn’t do anything. However, it was forgotten that the Mockito class keeps track of all the “sendMessage” method calls. As a result, this object keeps growing until an <em>OutOfMemory</em> occurs. Fixing it was a matter of modifying a line of code.</p><p>We learnt a couple things:</p><blockquote><strong>Always do the full pipeline of testing in the version delivered to the customer, independently of if it had customizations or not.</strong></blockquote><blockquote><strong>Add more constrains in Sonar and some custom rules to display violations on the project’s quality dashboard using the Mockito package.</strong></blockquote><p>I had the pleasure to the visit the customer and learn a thing or two. In the end, everything worked out fine. When I got there, we identified and fixed the issue in a couple hours, so the meetings were mainly focused on future functionalities, which was productive and exciting. Plus, I had some spare time to watch an NFL game, eat some local burgers and go sightseeing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57d755b14b5c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/strands-tech-corner/jvm-memory-mockito-trip-57d755b14b5c">JVM Memory, Mockito and a trip</a> was originally published in <a href="https://medium.com/strands-tech-corner">Strands Tech Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimisation of SQL inserts into RDBMS]]></title>
            <link>https://medium.com/strands-tech-corner/optimisation-of-sql-inserts-into-rdbms-13d5ef90556d?source=rss-4c236b9aa0d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/13d5ef90556d</guid>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[batch]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Fernando Soler David]]></dc:creator>
            <pubDate>Mon, 09 Sep 2019 15:42:52 GMT</pubDate>
            <atom:updated>2019-09-16T15:14:22.097Z</atom:updated>
            <content:encoded><![CDATA[<h3>Optimization of SQL inserts into RDBMS</h3><p>Here at Strands, we use a Spring Boot application that can read CSV files from a directory, aggregate and enrich records, and persist them into an RDBMS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IuxMOrvWctw8eY6aYwFiew.png" /><figcaption>Image 1: Batch Insert Diagram</figcaption></figure><p>CSV files might have millions of rows, and each customer can generate as many files as needed. Consequently, the ability to process, aggregate and persist all the data as quickly as possible is highly sought after. <br><br>In this post, we are going to focus on how to optimize the persistence of the data into the RDBMS. Every time an insertion takes place, 1,000 records are read, and data is enriched and persisted; so how can we improve the speed of this process?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mE_WSHSOpcOayj69F6fneg.jpeg" /></figure><p><strong>JPA</strong></p><p>In the Spring ecosystem, the first thing to do would be to group all the records in a single collection. These records will be represented as Entities, which will then be mapped into an SQL table. Next, the Entity Manager can be used to persist the data into the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*q2-2U-yEIllaYmfTEjIikQ.png" /><figcaption>Image 2: EntityManager persisting in a collection.</figcaption></figure><p>As you can see, the <strong><em>EntityManager</em></strong> doesn’t provide any method that accepts a collection as an argument. We want to insert all the records in one batch, but would that happen after doing the commit?</p><p>Let’s find out by enabling <strong><em>Hibernate Statistics</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wmGfPsgLZe-1T6j-5gipgA.png" /><figcaption>Image 3: Hibernate stats with batch mode disable</figcaption></figure><p>Unfortunately, if we don’t do anything else, JPA/Hibernate will execute an SQL statement for each of the records, which will have an impact on performance. Luckily, the following feature allows executing a collection of records as a single batch insert:<br><br><strong>spring.jpa.properties.hibernate.jdbc.batch_size=25</strong><br><br>Specifically, this feature dictates that every batch insert should include 25 records. Let’s run the test again and check the hibernate stats.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HDg_d5LDxpYDz8R2Qduz5g.png" /><figcaption>Image 4: Hibernate stats with batch mode enable</figcaption></figure><p>Since we are processing 1,500 records, and every batch comprises 25 records, a total of 60 batch inserts will be executed.</p><p>While this is certainly an improvement — the process is now 18 times faster — , the records still have not been inserted in just one SQL batch, which has an impact on the execution time.</p><p>Let&#39;s try again, this time with a batch size of 1,500 records, and see if we can improve performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a6dMhcoJU2eCRoqLSvb40A.png" /><figcaption>Image 5: Hibernate Stats with a batch size of 1,500</figcaption></figure><p>Now that we have set up a batch size that matches the number of entities to be persisted, only one batch insert is going to be executed. As a result, the process is now 35 times faster than before, when 60 batch inserts had to be executed. <br><br>In conclusion, the closer the batch size is to the total number of entities, the more performance will improve.</p><p>However, the batch size of the entity manager cannot be modified in a dynamic way. This is a serious limitation in a scenario where the size of the collection of entities is different every time.</p><p>If this is our case — and only in those instances where performance is particularly important — , we can use the JDBC driver.<br><br><br><strong>JDBC</strong><br><br>When working with prepared statements, it helps to build batch inserts with different sizes, which can be done dynamically. The only drawback is that we are losing the wizardry of the ORM, where the SQL queries are built magically and entities are mapped to each of the columns.</p><p>If you are prepared to build a batch insert this way, below you can see an example of how to do it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1PEScp3Sk1yWJSuUvwa9Ag.png" /><figcaption>Image 6: JDBC Batch insert code</figcaption></figure><p>Working with JDBC has yet another benefit that JPA doesn’t provide. If an error takes place during the batch execution, a <strong><em>BatchUpdateException </em></strong>is launched. Take a look into the documentation of this class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fe2DogTrz1ZwfFUdELR_Pg.png" /><figcaption>Image 7: Javadoc from BatchUpdateException class</figcaption></figure><p>This is very useful when there is a <strong><em>ConstraintViolation</em></strong>. One of the records is causing the execution of the batch to fail. With JPA, the only solution is to save each of the Entities individually until we find out which one is causing the SQL Exception. However, using the JDBC API and thanks to the <strong><em>BatchUpdateException</em></strong>, we can figure out which record caused the exception, so we can remove it from the collection and execute the batch insert again.<br><br>In this post, we have presented different approaches to reduce the number of iterations through the database, while persisting data in batch mode.</p><p>In the end, given that performance was a critical requirement of this application, we decided to use the JDBC API, which gave us more control over the JDBC batch inserts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13d5ef90556d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/strands-tech-corner/optimisation-of-sql-inserts-into-rdbms-13d5ef90556d">Optimisation of SQL inserts into RDBMS</a> was originally published in <a href="https://medium.com/strands-tech-corner">Strands Tech Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>