<?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 German Suárez on Medium]]></title>
        <description><![CDATA[Stories by German Suárez on Medium]]></description>
        <link>https://medium.com/@gersua?source=rss-37ffde982da8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*aRoszNFFrf_n9_ic.jpg</url>
            <title>Stories by German Suárez on Medium</title>
            <link>https://medium.com/@gersua?source=rss-37ffde982da8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 08:29:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gersua/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[How to right-sizing your AWS Lambdas with Lambda Power Tuning]]></title>
            <link>https://medium.com/@gersua/how-to-right-sizing-your-aws-lambdas-with-lambda-power-tuning-5ff660693193?source=rss-37ffde982da8------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ff660693193</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[German Suárez]]></dc:creator>
            <pubDate>Thu, 26 Jan 2023 17:26:15 GMT</pubDate>
            <atom:updated>2023-01-26T17:26:15.323Z</atom:updated>
            <content:encoded><![CDATA[<h4>Get the best cost-benefit from your lambdas</h4><figure><img alt="AWS Lambda Logo" src="https://cdn-images-1.medium.com/max/1024/0*inUHh1FOsaYYfzo2.png" /><figcaption><a href="https://en.wikipedia.org/wiki/AWS_Lambda#/media/File:Amazon_Lambda_architecture_logo.svg">https://en.wikipedia.org/wiki/AWS_Lambda#/media/File:Amazon_Lambda_architecture_logo.svg</a></figcaption></figure><p>IIt’s not a secret that lambdas are the preferred deployment model for many developers. The idea of not having to worry about provisioning the underlying infrastructure and only being billed by millisecond utilization makes lambda ideal for many use cases.</p><p>In addition, there are plenty of tools for their development, including SAM and serverless.</p><p>It’s worth saying that lambdas aren’t a silver bullet, and there are cases where other deployment alternatives suit better, like Kubernetes.</p><p>However, today we will focus on lambdas, and the AWS lambda power tuning state machine tool that will allow us to right-size our lambdas.</p><h3>A bit of context</h3><p>Lambdas run on top of Firecracker, a lightweight virtualization runtime. The promise is to be as secure as VMs, and as light as containers taking the best of both worlds.</p><p>The original intention of the lambda runtime was to be fully serverless, without providing any configuration options to developers. Although that changed due to developers needing more flexibility and control, for instance, setting a minimum of RAM.</p><p>The current configuration allows setting the execution runtime like Node, Python, or Java and the amount of the desired RAM or processor architecture.</p><p>No other configurations are available. That means there are no options to define networking bandwidth or CPU power. Nonetheless, Amazon will automatically increase those two when more memory is allocated.</p><p>In other words, a function running with 1GB of RAM will outperform one with 512MB because the former will have more RAM, CPU, and network power. The price per millisecond will also increase, though.</p><p>The tricky question is, what’s the best RAM option for a lambda function so we can get the best performance/cost?</p><h3>Profiling functions</h3><p>Normally, the best way to answer the question above is by profiling the application. That gives the insights and understanding of the behavior and resources needed by the application or in this case, the function.</p><p>So yeah, your best tool will always be knowing your application. Fortunately, Alex Casalboni developed the <strong>aws-lambda-power-tuning,</strong> as the repo states <em>“AWS Lambda Power Tuning is a state machine powered by AWS Step Functions that help you optimize your Lambda functions for cost and/or performance in a data-driven way.”</em></p><p>So, let’s develop a simple application we can run in lambda and see how to use this profiling tool.</p><h3>Pre-requisites</h3><p>serverless framework and python are used for this article. For more information please go to <a href="https://www.serverless.com/framework/docs/getting-started">https://www.serverless.com/framework/docs/getting-started</a></p><h3>The function</h3><p>Let’s start reviewing the function’s code. This is a simple python method that calculates the nth Fibonacci number of the series given the num parameter in the JSON body request. An example of the functions’ input/output would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6ed28e9dbdfe7a38ab2de649a59e565/href">https://medium.com/media/e6ed28e9dbdfe7a38ab2de649a59e565/href</a></iframe><p>For more information about the Fibonacci series please go here <a href="https://en.wikipedia.org/wiki/Fibonacci_number">https://en.wikipedia.org/wiki/Fibonacci_number</a></p><p>The exciting bit of this is that when increasing the input, more processing time is needed for the calculation. That’s because this program needs to calculate all the previous series numbers before returning the result. So, for this case, we’ll need to pay special attention to CPU and memory and not that much to networking.</p><p>This is the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef8ea866994ab73c59ade4a4888ee199/href">https://medium.com/media/ef8ea866994ab73c59ade4a4888ee199/href</a></iframe><p><strong>Quick note:</strong> I tried first a recursive function, but my design was not tail recursive friendly so when I tried high values for num the function always timed out, that’s why I decided to use for-loop to keep things simple and focus on the lambda optimizations.</p><p>There are two function definitions, one for the handler, lambba_handler(), which extracts from the expected JSON the num property and delegates the actual computation to fibonacci(), which uses recursion.</p><p>Testing locally, these results are obtained:</p><p>~/tmp » serverless invoke local — function fibonacci -d ‘{ “num”: 1 }’ <br>{<br> “statusCode”: 200,<br> “body”: “{\”result\”: 0}”<br>}<br> — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — <br>~/tmp » serverless invoke local — function fibonacci -d ‘{ “num”: 2 }’ <br>{<br> “statusCode”: 200,<br> “body”: “{\”result\”: 1}”<br>}<br> — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — <br>~/tmp » serverless invoke local — function fibonacci -d ‘{ “num”: 7 }’ <br>{<br> “statusCode”: 200,<br> “body”: “{\”result\”: 13}”<br>}<br> — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — <br>~/tmp » serverless invoke local — function fibonacci -d ‘{ “num”: 10 }’ <br>{<br> “statusCode”: 200,<br> “body”: “{\”result\”: 55}”<br>}</p><h3>Running on AWS</h3><p>Well, let’s give it a try on AWS and see how long it takes the function with the following values for num:</p><p>[2, 3, 4, 5, 6, 7, 50, 55, 100, 200, 250, 300, 500, 1500, 2500, 3000, 5000, 10000, 20000, 200000, 500000, 1000000]</p><p>Remember, the higher the number the most costly the computation.</p><p>For the demo proposal, I selected “Author from scratch” option, function fibonacci, and Python 3.8 as a runtime environment. The rest is set as default.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/0*CtZ9zFuErhmnQ8XT" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/0*RORYOT9LkchLkGvf" /></figure><p>And deploy it. After that, we can create a test event and modify it with the values we want to check for the function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/0*hmyaM41d0vFCk0Mx" /></figure><p>So, these are the results for the test set:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37dde8a55acfc9ad9d1e0cad6cd2a03e/href">https://medium.com/media/37dde8a55acfc9ad9d1e0cad6cd2a03e/href</a></iframe><p>As the results show, the RAM is basically the same through the different tests, however, we can see how the CPU is hit when the value of num increases. And this is an interesting scenario because we could think that because RAM is not affected we should use the default configuration. Still, we can have long-lasting processes that will impact the final billing.</p><h3>Lambda Power Tuning</h3><p>There are different ways to deploy the power tuning tool, as depicted in the official GitHub repo <a href="https://github.com/alexcasalboni/aws-lambda-power-tuning">https://github.com/alexcasalboni/aws-lambda-power-tuning</a>.</p><p>One of the easiest is to deploy it using the serverlessrepo <a href="https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:451282441545:applications~aws-lambda-power-tuning">https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:451282441545:applications~aws-lambda-power-tuning</a>.</p><p>This way we can use a web interface for deploying and configuring the settings we want to test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*f7AQ64Zh_q7CC85W" /></figure><p>Once you click deploy, you’ll be redirected to your AWS console where you can “Review, configure and deploy”. Cloudformation is used behind the scenes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/0*zMynvKMMClSOuLpN" /></figure><p>Tick the acknowledge and make sure you have all the required permissions to deploy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/0*LKxHpBfvJ1kaIIy5" /></figure><p>Once it is deployed, a screen like this will be shown:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/0*pS2OjCvv9csrN5Ar" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/0*9O_dAJEwKAwL3bxo" /></figure><p>If everything is completed successfully, we can go to the steps functions service and verify that the new state machine has been created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/0*OPOOv9y0uC1C6km0" /></figure><p>Furthermore, you can also verify that each step of the step function has been also created as a lambda function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*tPS5AzeohOimp8uN" /></figure><p>Now, let’s go and configure our state machine execution. Go to the State machines on the step functions page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*kVwLmiLQiptIZuHL" /></figure><p>Now click on start execution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*tEguyX5XRsRfYinr" /></figure><p>And add the input as JSON. Below is the description of what that input means:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e81ca2ac97fffbba0912083a7e350eba/href">https://medium.com/media/e81ca2ac97fffbba0912083a7e350eba/href</a></iframe><ul><li><strong>lambdaARN:</strong> The ARN identifier of the Lambda function we want to test and optimize</li><li><strong>powerValues:</strong> The RAM configurations we want to include in the test</li><li><strong>num:</strong> The number of invocations for each power value</li><li><strong>payload: </strong>The actual payload. An array can be used for using multiple distinct payloads</li><li><strong>parallelInvocation: </strong>Allows to execute everything in parallel</li><li><strong>strategy:</strong> Defines the optimization goal, it could be set to cost” or “speed” or “balanced”</li></ul><p>Click on start execution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/0*AiToTRB8iICfxNqs" /></figure><p>Now that the execution finished successfully, we can take a look at the results and explore which one is our best configuration option. To do so, go to “Execution Input and Output” and follow the visualization link in the output panel:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A5fMmif8UcTz30NS" /></figure><p>Here are the results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3uJrT8LRM8LhPqH0" /></figure><p>These are the results. In this case, 128MB is the worst invocation time, and 3008MB is the worst cost. The best cost/benefit configuration is 1024MB with a really good performance at basically the same cost that the 128MB configuration. So, let’ go for it.</p><h3>Conclusion</h3><p>AWS Lambda Power Tuning is an excellent tool for profiling your lambdas and understanding how they are performing under different configurations, so you can select the one is more convenient depending on what you want to achieve, performance, cost-savings, etc.</p><p>Just a ramiander, don’t forget that improving your code can have a tremendous impact in the lambda performace, so make sure you code performs well first.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ff660693193" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Java, Kotlin, and Scala compile in Bytecode]]></title>
            <link>https://medium.com/@gersua/how-java-kotlin-and-scala-compile-in-bytecode-b77aab069986?source=rss-37ffde982da8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b77aab069986</guid>
            <category><![CDATA[bytecode]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[German Suárez]]></dc:creator>
            <pubDate>Mon, 18 Jul 2022 14:12:10 GMT</pubDate>
            <atom:updated>2022-07-18T14:12:10.425Z</atom:updated>
            <content:encoded><![CDATA[<h4>How the same written program in the three languages compile in bytecode instructions</h4><p>This short article aims to show the differences and similarities at the bytecode level of the same simple program.</p><p>Java, Scala, and Kotlin are general-purpose languages with solid use cases, especially in server-side and Android development. They all have syntactic differences, but we can argue they all offer object-oriented and functional constructs to a different extent.</p><figure><img alt="Java mascot and Kotlin and Scala logos" src="https://cdn-images-1.medium.com/max/1024/1*lYFLWb-nL8vyyO47SlgC1w.png" /></figure><h3><strong>What is Bytecode anyways?</strong></h3><p>The JVM compiler does not produce machine code directly. It generates instead bytecode, a set of instructions that the JVM can then interpret and make the actual machine code from.</p><p>This allows compiled Java (and other JVM langs) programs to run on multiple platforms with minimal or no code changes. This is how the Java platform keeps its promise of “Write once and run everywhere.”</p><p>Of course, the JVM optimizes the interpretation of that code, ensuring that the most used code is always cached as native code. The hottest used blocks of code are “aggressively” compiled into native code, increasing the overall performance of JVM comparable to programs written in C/C++. This is the JIT compiler, but we can go into detail in another post.</p><p>The takeaway is that the JVM language compilers produce bytecode instead of native code, enabling platform interoperability. This is true for Java, Scala, and Kotlin (and others) and that’s why we’re going to see for the same program in those three languages how similar is their bytecode instructions.</p><h3>javap — The Java Class File Disassembler</h3><p>Per the official documentation, “disassemble one or more class files.” <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javap.html">https://docs.oracle.com/en/java/javase/17/docs/specs/man/javap.html</a></p><p>javap is a command line tool that ships with the JVM and allows us to see how a class is structured. With the -c option, we can “Disassemble the code,” which enables us to understand how the compilers translate code into bytecode instructions.</p><p>You can try javap -help and see all the command options:</p><pre>javap — help Usage: javap &lt;options&gt; &lt;classes&gt;</pre><pre>where possible options include:</pre><pre> — help -help -h -? Print this help message</pre><pre>-version Version information</pre><pre>-v -verbose Print additional information</pre><pre>-l Print line number and local variable tables</pre><pre>-public Show only public classes and members</pre><pre>-protected Show protected/public classes and members</pre><pre>-package Show package/protected/public classes</pre><pre>and members (default)</pre><pre>-p -private Show all classes and members</pre><pre>-c Disassemble the code</pre><pre>-s Print internal type signatures</pre><pre>-sysinfo Show system info (path, size, date, SHA-256 hash)</pre><pre>of class being processed</pre><pre>-constants Show final constants</pre><pre> — module &lt;module&gt;, -m &lt;module&gt; Specify module containing classes to be disassembled</pre><pre>-J&lt;vm-option&gt; Specify a VM option</pre><pre> — module-path &lt;path&gt; Specify where to find application modules</pre><pre> — system &lt;jdk&gt; Specify where to find system modules</pre><pre> — class-path &lt;path&gt; Specify where to find user class files</pre><pre>-classpath &lt;path&gt; Specify where to find user class files</pre><pre>-cp &lt;path&gt; Specify where to find user class files</pre><pre>-bootclasspath &lt;path&gt; Override location of bootstrap class files</pre><pre> — multi-release &lt;version&gt; Specify the version to use in multi-release JAR files</pre><pre>GNU-style options may use = instead of whitespace to separate the name of an option</pre><pre>from its value.</pre><pre>Each class to be shown may be specified by a filename, a URL, or by its fully</pre><pre>qualified class name. Examples:</pre><pre>path/to/MyClass.class</pre><pre>jar:file:///path/to/MyJar.jar!/mypkg/MyClass.class</pre><pre>java.lang.Object</pre><h3><strong>Let’s code!</strong></h3><p>Let’s start writing the code. The idea is to write a simple [Language]Calc class with just one add() method that adds two integers and returns the result. This is very simple but will allow us to have clarity on what’s happening underneath.</p><p>Moreover, a comprehensive list of bytecode instructions and their meanings can be found here: <a href="https://docs.oracle.com/javase/specs/jvms/se17/html/index.html">https://docs.oracle.com/javase/specs/jvms/se17/html/index.html</a> and <a href="https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions">https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions</a>. We’re just going to review a couple of them in this article.</p><p>Ok, this is the Java code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c28a816f4e038ad5c006dac5762f600/href">https://medium.com/media/8c28a816f4e038ad5c006dac5762f600/href</a></iframe><p>Here the Kotlin code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef58cabea75a499120002bff163c5be3/href">https://medium.com/media/ef58cabea75a499120002bff163c5be3/href</a></iframe><p>And finally the Scala code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b651ef780ed5876ebb1d04dc8236abc/href">https://medium.com/media/1b651ef780ed5876ebb1d04dc8236abc/href</a></iframe><p>Due to the simplicity of the code, all three blocks are pretty concise and self-explanatory.</p><p>After building the projects and producing the .class files which contain the bytecode, it’s time to run the javap command on each of the resulting classes.</p><pre>javap -c app/build/classes/java/main/co/gersua/demo/calc/JavaCalc.class</pre><figure><img alt="disassembling JavaCalc class" src="https://cdn-images-1.medium.com/max/674/0*3FUFM5qssDSH8l0r" /></figure><p>We now have disassembled the class, and we can see the bytecode instructions for the add() method and the constructor. A quick reminder, even if we do not provide an explicit constructor, a default one is always provided. That’s why we see those instructions too.</p><p>We’ll omit all the constructor’s instructions to focus only on the add() method, so let’s see what we got.</p><pre>public int add(int, int);</pre><pre>Code:</pre><pre>0: iload_1</pre><pre>1: iload_2</pre><pre>2: iadd</pre><pre>3: ireturn</pre><ul><li>The first instruction we have is iload_1 which <em>“loads an int value from local variable 1”</em>. It means it’s loading the value of x.</li><li>The second instruction is iload_2 which <em>“loads an int value from local variable 2”</em>. So this time, the y value has been loaded.</li><li>The third instruction is iadd which <em>“adds two ints”</em>. In this case, it is taking the values of x and y previously loaded, and then it adds them.</li><li>The last instruction is ireturn, which “returns an integer from a method”. Basically, it is returning the value of the addition of the two integers, which is on top of the stack.</li></ul><p>This looks pretty straightforward, and it is due to the simplicity of our method.</p><p>However, this is also an excellent way to see what’s happening behind the scenes at the JVM level, where the x and y values are loaded to the stack, then they are added, and finally, that value is returned, very cool ah!</p><p>Now, let’s see how different the instructions are between Java, Kotlin, and Scala, so let’s go with Kotlin first. Lest’s omit the constructor instructions.</p><pre>javap -c app/build/classes/kotlin/main/co/gersua/demo/calc/KotlinCalc.class</pre><figure><img alt="disassembling KotlinCalc class" src="https://cdn-images-1.medium.com/max/808/0*iNKyHqcT5NdvFvwO" /></figure><p>This is very interesting. The instruction set is the same! The only visible difference is that Kotlin enforces the method as final. Apart from that, the generated bytecode is the same.</p><p>Let’s take a look at the Scala bytecode instructions:</p><pre>javap -c target/scala-2.13/classes/ScalaCalc.class</pre><figure><img alt="disassembling ScalaCalc class" src="https://cdn-images-1.medium.com/max/682/0*cWdsbL0j18d61Kog" /></figure><p>Well, I guess there are no surprises here, we have the same outcome.</p><p>And this is great! That means that these programming languages are more similar than initially expected at the bytecode level and that the compilers do the needed optimizations to the source code.</p><p>That also means that the performance of the running applications will be the same. That’s cool because we can focus on the language’s preferred paradigm and best practices and let the compilers optimize the instructions.</p><h3>Conclusion</h3><p>Reviewing the bytecode gives a better insight into what’s happening behind the scenes and how the different compilers tend to optimize their instructions making the same program written in three different languages basically the same.</p><p>In addition, it’s good to understand how the JVM works, what its instructions look like, and what build-in tools are out there.</p><p>The code can be found here: <a href="https://github.com/germansua/blog/tree/main/001-how-java-kotlin-and-scala-compile-in-bytecode">https://github.com/germansua/blog/tree/main/001-how-java-kotlin-and-scala-compile-in-bytecode</a></p><p>Everything was tested on <strong>Amazon Corretto-17.0.3.6.1</strong></p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b77aab069986" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>