Micro Benchmarking with JMH: Measure, don’t guess!

Antonio's Blog

I’m sure you’ve all heard that assigning a variable to null helps the Garbage Collector, or not declaring a method final improves in lining…. But what you also know is that JVMs have evolved drastically and what was true yesterday may not be true today. So, how do we know that our code performs? Well, we don’t, because we are not supposed to guess what the JVM does… we just measure!

Measure, don’t guess!

As my friend Kirk Pepperdine once said, “Measure, don’t guess“. We’ve all faced performance problems in our projects and were asked to tune random bits in our source code… hoping that performance will get improved. Instead, we should setup a stable performance environment (operating system, JVM, application server, database…), measure continuously, set some performance goals… then, take action when our goals are not achieved. Continuous delivery, continuous testing… is one thing, but continuous measuring is another step.


View original post 816 more words


Resize Fedora/Gnome 3 window title bar

Run the following command :

sudo sed -i “/title_vertical_pad/s/value=\”[0-9]\{1,2\}\”/value=\”0\”/g” /usr/share/themes/Adwaita/metacity-1/metacity-theme-3.xml

and then press

Alt + F2‘ which will open an run window and then type

r‘ and press enter.

It’ll restart the gnome shell. You’ll notice the resized title bar immediately.

Run shell script as root user (without sudo) using setuid

In the most Linux distributions, setuid doesn’t work on shell scripts because of the security issues associated with it.

But you can use the setuid on binary files and from there you can execute the shell script as root user. And the following steps will help you to do that.

1) Copy the following C program to a file named “runasroot.c” and this program simply runs a script which is passed as command-line argument.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {                                              
  if (argc != 2) {                                                              
    printf("Usage: %s /path/to/script\n", argv[0]);                             
    printf("Note: '/path/to/script' can be either relative or absolute path.\nE\
xamples:\n%s ./build.sh \n%s /tmp/test.sh\n", argv[0], argv[0]);                
  } else {                                                                      
    printf("Running the script '%s' as root user...\n", argv[1]);               
   return 0;                                                                    

2) Compile it.

gcc runasroot.c -o runasroot

3) Change the ownership of the compiled executable binary to root

sudo chown root:root runasroot

4) Change the permissions on the binary file with setuid flag

sudo chmod 4755 runasroot

5) Run the script using the executable binary file

./runasroot script.sh

Examples :

./runasroot ./build.sh 
./runasroot /tmp/test.sh

You can even copy this ‘runasroot’ executable file to your home bin directory (i.e., in ‘/home/user/bin‘) so that you can use it anywhere in your scripts. And you can use the ‘runasroot‘ command (without any password prompt) instead ofsudo‘ in your commands. If you want to pass parameters to the commands, you should use single/double quotes to surround it.

runasroot 'fdisk -l'

instead of

sudo fdisk -l

Warning: Make sure you don’t place this script in any common directory (like /usr/bin or /opt) in a multi-user environments.

How to fix the wireless network issues in Fedora?

Issue 1 : NetworkManager asks password each time (even password is saved) when you connect to enterprise wireless network.

Does you fedora (NetworkManager) asks the password each time even when you saved it? I’m also faced it with my ThinkPad Edge (Fedora 20) and found a workaround to fix it.

Continue reading “How to fix the wireless network issues in Fedora?”

Maven Multi-Module Structure for Enterprise Java Projects (Best Practices)

Many times, I am trying to resolve several basic or complex Maven /  Java EE  project structure issues, on my day work. In order to provide the solutions,  I often end up experimenting with the project structure and it’s effectiveness.

It’s worth investing some time on application structure for big projects (especially Java EE). If not, it’s cannot be changed at the end.

It’s important to understand the application structure and the underlying build tools when you start working on a project.

Maven might be wise choice because it’s helps you build a better project quickly and effectively.

Maven helps in the following areas :

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

Best practices (maven’s) for development

  • Keeping your test source code in a separate, but parallel source tree
  • Using test case naming conventions to locate and execute tests
  • Have test cases setup their environment and don’t rely on customizing the build for test preparation.

Maven is designed to be flexible, but only to an extent. So, using maven in your project means, you need to reorganize your project (if not in good shape) and adhere to the maven’s conventions.

Random Quote :

“It is good to break conventions in your life and in your coding, but never with Maven.”

 Organizing Style

There are number of ways you can organize your project’s sub-modules.

  • Single Bulk Module
  • Module by Class Type
  • Module by Functionality Area

1) All the modules can be grouped together as a single bulk project. But it’s not recommended anyway.

2) Module by class type : Project can be splitted into sub-projects/modules. And each module are used to group together all classes that comprise, but are not limited to, a layer in the project’s architecture. For example, a module could contain all classes that make up the project’s service or persistence layers or a module could contain all the model class (i.e., jpa or entity beans).

3) Modules by functionality area : Project’s sub-modules are organized by functional area (component based) i.e, vertical slice of the application including model beans, service layer, repositories etc..

Example for ‘Module by class type’:

  • app-model
  • app-utils
  • app-repository
  • app-services
  • app-web
  • app-ear

Example for ‘Module by functionality’:

  • app-utils
  • app-user
  • app-audit
  • app-auth
  • app-integration
  • app-web
  • app-ear

It’s always necessary for each module to have it’s own unit tests.

Pros and Cons of Class Type :

+ Pretty maintainable in that you always known where to find stuff
+ Build order is very straight forward
Circular dependencies
Classes with totally different responsibilities are all mixed up together making it difficult to re-use functionality in other projects

Pros and Cons of Functionality Type :

+ Modules are highly decoupled
+ It’s far simpler to re-use a functional area of code in other projects
Build order might be complex because of its dependencies

You may have noticed that both of them are not 100% perfect.

Also there are few modules which are repeated in both these types in the project. Because it’s always wise to keep the necessary things together i.e., creating a separate database module allows you to change your project’s database implementation fairly easily, which may make testing easier in some circumstances and likewise, having a separate web module allows you to re-skin your code easily.

At the end of the day, it really depends on the need of the project and it’s existing structure. And you can also mix-match both these types, if helps to solve your problem effectively.

GNOME 3.6: GNOME Online Accounts and Google two-factor authentication

Kparal's Fedora Blog

goa-panel In GNOME 3.4 (Fedora 17), GNOME Online Accounts (GOA) worked great with Google two-factor authentication (you really should enable that, if you value your data). In GNOME 3.6 (Fedora 18) it works no more, and it might be fixed in GNOME 3.8. When developers break some existing functionality for the sake of “progress”, but don’t bother fixing it or providing an alternative way before an official release, I always feel a bit… disenchanted.

Fortunately you can work around the broken code.

  1. Open Seahorse, filter your passwords for “GOA”, you should see one or two items of “Gnome Online Accounts password” type. Delete them.
  2. Re-login to Gnome session.
  3. Open Online Accounts and log in to your Google account. It will fail.
  4. Create an application-specific password for your Google account in the web browser.
  5. Open Seahorse, filter your password for “GOA”, you should see a single item. Open it and display…

View original post 84 more words

Generating and Compiling Java Classes using Java Compiler API

Steps :

  • Construct JavaFileObject from any source (i.e, here, from sourceCode String) and make it to iterable object
  • Get the Java System Compiler
  • Create a compiler task using Compiler.getTask(), (if required create the task with diagnostic collection to get compilation errors)
  • Execute the Compiler Task (compile it).

Source Code :

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class CompileDynamicClasses {

static String sourceCode = "class DynamicClass{" + "public static void main (String args[]){"
+ "System.out.println (\"Hello, Dynamic Class!\");" + "}" + "}";

public static void main(String[] args) {

// Construct a JavaFileObject from source code
SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject("DynamicClass",

Continue reading "Generating and Compiling Java Classes using Java Compiler API"