December 18, 2012

My Pen and I


I stare at the nib and it stares right back at me. Where are the words of wisdom when I need them to come out of me and pour on to the paper? As if my pen has ability to think for itself, I expect my writing to tell me what is that I seek in life. Of course, I momentarily forget that what I write is what I think but I do so in the faith that perhaps I will discover something new, something astonishing and more importantly something with healing powers. What depth of knowledge can give me salvation from the destitute, desperate, depressive, and demeaning thoughts? The answer to such rhetorical questions are hard to find amidst the infinitude of truths, lies and everything in between. An insurmountable mountain of difficulties lie in front of me. With my head resting on my arm, I wait for a thought of significant value to come. Yet none comes. I fill my lungs with as much as I can intake and then release it. A big sigh, indeed.

Sounds of multiple instruments of Jazz and a cup of fair-trade organic coffee attempt to lift my spirits. Tempted to fall into the magical experience of music and the exotic taste of my beverage, I ponder about the poor souls that may never appreciate Jazz music with coffee like I do. Saxophone interludes the piano. Then suddenly, classical guitar bursts itself in to ascertain its place. Feeling quite insecure, the trumpet reminds me that it too is present. Then the presenter interrupts to tell me that the piece was by Kenny Wheeler and it is called the "Fortune's Child". A soul-stirring song, I must say.

I turn around to look for something to inspire. I look at my pen again. I say, "Go on pen, write something for me. You are free." But the pen writes exactly what I ask. I ask "Don't you have any free will? Of course, you are an inanimate object but don't you worry for most of us rather feel like we are inanimate, stripped out of freedom, reason and life". Breaking these thoughts, I burst into laughter as I see Fred trying to get to the leftover crusts from the pizza box. One being's struggle becomes another one's laughter. Meanwhile, the radio continues to spit out one gem after another. Is there a more blissful place than This? Is there a more satisfying time than Now? As I realize this, a anticlimactic but a joyful feeling sets in. I think good and subsequently feel good. Thank you, my dear pen.

November 21, 2012



Use of ThreadLocal in JMeter

Suppose:
- We have a class called Person and a member variable called name
- We have two Java samplers in JMeter. First sampler will set the name. The second sampler will read the name.

We can make the name variable to be a static variable. However if there are multiple threads, then racing conditions will occur and the data will be unreliable. What we need is a ThreadLocal. Unlike global static variables, ThreadLocal variables are static only within a thread's life and thus, multiple threads will have its own copy of the Person.name variable.

package jmeter.threadlocal;

public class Person {
    String name;
  
    private Person() {
    }

    public static ThreadLocal<Person> threadLocal = new ThreadLocal<Person>() {
        @Override
        protected Person initialValue() {
            return new Person();
        }
    };
   
    public static void setName(String newName){
        threadLocal.get().name = newName;
    }
   
    public static String getName(){
        return threadLocal.get().name;
    }

}


First Java sampler that will set the name:

package jmeter.threadlocal;

import java.util.Random;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class Test1 extends AbstractJavaSamplerClient {

    @Override
    public SampleResult runTest(JavaSamplerContext context) {
        SampleResult res = new SampleResult();
        res.sampleStart();

        String name = context.getParameter("name")
                + (new Random()).nextInt(Integer.MAX_VALUE)
                + Integer
                        .toHexString(((new Random().nextInt(Integer.MAX_VALUE))));
        super.getLogger().info(
                Thread.currentThread().getId() + " Setting name: " + name);
        Person.setName(name);
        super.getLogger().info(
                Thread.currentThread().getId() + " Set name: " + name);
        res.sampleEnd();
        return res;
    }

    public Arguments getDefaultParameters() {
        Arguments args = new Arguments();
        args.addArgument("name", "");
        return args;
    }
}


Second Java sampler that will read the name:

package jmeter.threadlocal;

import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class Test2 extends AbstractJavaSamplerClient {

    @Override
    public SampleResult runTest(JavaSamplerContext context) {
        SampleResult res = new SampleResult();
        res.sampleStart();

        super.getLogger().info(
                Thread.currentThread().getId() + " Getting bytes: "
                        + Person.getName());

        res.sampleEnd();
        return res;
    }

}


Compile and package the .class files into a jar. Place it in $JMETER_INSTALLDIR\lib\ext. Create a JMeter test plan as follows:

   




JMeter logs:

2012/11/21 11:11:47 INFO  - jmeter.protocol.java.sampler.AbstractJavaSamplerClient: 29 Setting name: parthy5192850885cfaf9a9
2012/11/21 11:11:47 INFO  - jmeter.protocol.java.sampler.AbstractJavaSamplerClient: 29 Set name: parthy5192850885cfaf9a9
2012/11/21 11:11:47 INFO  - jmeter.protocol.java.sampler.AbstractJavaSamplerClient: 28 Setting name: parthy148372871654161351
2012/11/21 11:11:47 INFO  - jmeter.protocol.java.sampler.AbstractJavaSamplerClient: 28 Set name: parthy148372871654161351
2012/11/21 11:11:47 INFO  - jmeter.protocol.java.sampler.AbstractJavaSamplerClient: 29 Getting name: parthy5192850885cfaf9a9
2012/11/21 11:11:47 INFO  - jmeter.protocol.java.sampler.AbstractJavaSamplerClient: 28 Getting name: parthy148372871654161351