/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */

package net.ranides.assira.text;

import com.google.caliper.SimpleBenchmark;
import net.ranides.assira.BenchmarkRunner;
import net.ranides.assira.math.Randomizer;

/**
 *
 * @author ranides
 */
public class StringUtilsBenchmark extends SimpleBenchmark {

    private final String[] randomTexts;

    public StringUtilsBenchmark() {
        randomTexts = new String[1000];
        for(int i=0; i<randomTexts.length; i++) {
            randomTexts[i] = Randomizer.text(128, "qwertyuiopasdfghjklzxcvbnm");
        }
    }
    
    public int timeReplaceAssira(int reps) {
        int c = 0;
        for(int r=0; r<reps; r++) {
            for (String text : randomTexts) {
                c += StringUtils.replace(text, 'a', "abc").charAt(0);
            }
        }
        return c;
    }

    public int timeReplaceJava(int reps) {
        int c = 0;
        for(int r=0; r<reps; r++) {
            for (String text : randomTexts) {
                c += text.replace(String.valueOf('a'), "abc").charAt(0);
            }
        }
        return c;
    }

    public int timeReplaceApache(int reps) {
        int c = 0;
        for(int r=0; r<reps; r++) {
            for (String text : randomTexts) {
                c += org.apache.commons.lang3.StringUtils.replace(text, String.valueOf('a'), "abc").charAt(0);
            }
        }
        return c;
    }

    public static void main(String[] args) throws Exception {
        BenchmarkRunner.run();
    }

}
