I have 2 classes: 1. DeviceData 2. MinDeviceData
DeviceData class contains 15 fields and methods, while MinDeviceData contains only one field - int count and some methods.
In my code, I am trying to do a performance test on 100k operations of fromString from string to this class. I expect the time result to be consistent, but every time I run the test - I get different result.
Why?
This is my code:
@Test
    public void checkClasses() throws IOException, InterruptedException {
        String pkt = "BIG JSON OBJECT HERE";
        for (int x = 0; x <= 5; x++) {
            TimeUnit.SECONDS.sleep(5);
            /**
             * Check NO FIF
             */
            ObjectMapper om = new ObjectMapper();
            HashMap hm = new HashMap<>();
            int cycle = 100000;
            long startTime = System.nanoTime();
            for (int i = 0; i <= cycle; i++) {
                DeviceData.fromString(pkt, om, hm);
            }
            long containsDuration = (System.nanoTime() - startTime);  //divide by 1000000 to get milliseconds.
            long object = containsDuration/cycle;
            System.out.println("--- Duration NIF: " + containsDuration +",per pkt in nano:" + object );
            TimeUnit.SECONDS.sleep(5);
            /**
             * Check FIF
             */
            Gson gson = new Gson();
            long fifStartTime = System.nanoTime();
            for (int i = 0; i <= cycle; i++) {
                DeviceDataMin.fromString(pkt, gson);
            }
            long regexDuration = (System.nanoTime() - fifStartTime);  //divide by 1000000 to get milliseconds.
            long fif = regexDuration/cycle;
            System.out.println("--- Duration FIF: " + regexDuration +",per pkt in nano:" + fif );
            System.out.println("DIFF : " + (100 - (double) ((double)fif * 100/object)));
        }
    }
Test Result (output):
--- Duration NIF: 676214146,per pkt in nano:6762
--- Duration FIF: 563609001,per pkt in nano:5636
DIFF : 16.651878142561372
--- Duration NIF: 415484807,per pkt in nano:4154
--- Duration FIF: 392932554,per pkt in nano:3929
DIFF : 5.416466056812709
--- Duration NIF: 377752426,per pkt in nano:3777
--- Duration FIF: 389876068,per pkt in nano:3898
DIFF : -3.2036007413290974
--- Duration NIF: 349583684,per pkt in nano:3495
--- Duration FIF: 392007947,per pkt in nano:3920
DIFF : -12.160228898426325
--- Duration NIF: 470443379,per pkt in nano:4704
--- Duration FIF: 368536421,per pkt in nano:3685
DIFF : 21.6624149659864
--- Duration NIF: 346816168,per pkt in nano:3468
--- Duration FIF: 354205262,per pkt in nano:3542
DIFF : -2.1337946943483246
