Marcin Grzejszczak avatar Marcin Grzejszczak committed 10a40b0

Initial commit of the Guava Cache usage example with Patterns

Comments (0)

Files changed (4)

Guava/Cache/pom.xml

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>pl.grzejszczak.marcin</groupId>
+	<artifactId>guava-cache</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<maven.compiler.source>1.6</maven.compiler.source>
+		<maven.compiler.target>1.6</maven.compiler.target>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.10</version>
+		</dependency>
+		<dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <version>13.0.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-log4j12</artifactId>
+            <version>1.5.2</version>
+        </dependency>
+	</dependencies>
+</project>

Guava/Cache/src/main/pl/grzejszczak/marcin/guava/cache/GuavaCache.java

+package pl.grzejszczak.marcin.guava.cache;
+
+
+import com.google.common.base.Stopwatch;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import pl.grzejszczak.marcin.guava.cache.utils.RegexpUtils;
+
+import java.util.regex.Pattern;
+
+import static java.lang.String.format;
+
+public class GuavaCache {
+    private static final Logger LOGGER = LoggerFactory.getLogger(GuavaCache.class);
+    public static final String STRING_TO_MATCH = "something";
+
+    public static void main(String[] args) {
+        runTestForManualCompilationAndOneUsingCache(1);
+        runTestForManualCompilationAndOneUsingCache(10);
+        runTestForManualCompilationAndOneUsingCache(100);
+        runTestForManualCompilationAndOneUsingCache(1000);
+        runTestForManualCompilationAndOneUsingCache(10000);
+        runTestForManualCompilationAndOneUsingCache(100000);
+        runTestForManualCompilationAndOneUsingCache(1000000);
+    }
+
+    private static void runTestForManualCompilationAndOneUsingCache(int firstNoOfRepetitions) {
+        repeatManualCompilation(firstNoOfRepetitions);
+        repeatCompilationWithCache(firstNoOfRepetitions);
+    }
+
+    private static void repeatManualCompilation(int noOfRepetitions) {
+        Stopwatch stopwatch = new Stopwatch().start();
+        compileAndMatchPatternManually(noOfRepetitions);
+        LOGGER.debug(format("Time needed to compile and check regexp expression [%d] ms, no of iterations [%d]", stopwatch.elapsedMillis(), noOfRepetitions));
+    }
+
+    private static void repeatCompilationWithCache(int noOfRepetitions) {
+        Stopwatch stopwatch = new Stopwatch().start();
+        compileAndMatchPatternUsingCache(noOfRepetitions);
+        LOGGER.debug(format("Time needed to compile and check regexp expression using Cache [%d] ms, no of iterations [%d]", stopwatch.elapsedMillis(), noOfRepetitions));
+    }
+
+    private static void compileAndMatchPatternManually(int limit) {
+        for (int i = 0; i < limit; i++) {
+            Pattern.compile("something").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something1").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something2").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something3").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something4").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something5").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something6").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something7").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something8").matcher(STRING_TO_MATCH).matches();
+            Pattern.compile("something9").matcher(STRING_TO_MATCH).matches();
+        }
+    }
+
+
+    private static void compileAndMatchPatternUsingCache(int limit) {
+        for (int i = 0; i < limit; i++) {
+            RegexpUtils.matches(STRING_TO_MATCH, "something");
+            RegexpUtils.matches(STRING_TO_MATCH, "something1");
+            RegexpUtils.matches(STRING_TO_MATCH, "something2");
+            RegexpUtils.matches(STRING_TO_MATCH, "something3");
+            RegexpUtils.matches(STRING_TO_MATCH, "something4");
+            RegexpUtils.matches(STRING_TO_MATCH, "something5");
+            RegexpUtils.matches(STRING_TO_MATCH, "something6");
+            RegexpUtils.matches(STRING_TO_MATCH, "something7");
+            RegexpUtils.matches(STRING_TO_MATCH, "something8");
+            RegexpUtils.matches(STRING_TO_MATCH, "something9");
+        }
+    }
+
+}

Guava/Cache/src/main/pl/grzejszczak/marcin/guava/cache/utils/RegexpUtils.java

+package pl.grzejszczak.marcin.guava.cache.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+
+import java.util.concurrent.ExecutionException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static java.lang.String.format;
+
+public final class RegexpUtils {
+
+    private RegexpUtils() {
+        throw new UnsupportedOperationException("RegexpUtils is a utility class - don't instantiate it!");
+    }
+
+    private static final LoadingCache<String, Pattern> COMPILED_PATTERNS =
+            CacheBuilder.newBuilder().build(new CacheLoader<String, Pattern>() {
+                @Override
+                public Pattern load(String regexp) throws Exception {
+                    return Pattern.compile(regexp);
+                }
+            });
+
+    public static Pattern getPattern(String regexp) {
+        try {
+            return COMPILED_PATTERNS.get(regexp);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(format("Error when getting a pattern [%s] from cache", regexp), e);
+        }
+    }
+
+    public static boolean matches(String stringToCheck, String regexp) {
+        return doGetMatcher(stringToCheck, regexp).matches();
+    }
+
+    public static Matcher getMatcher(String stringToCheck, String regexp) {
+        return doGetMatcher(stringToCheck, regexp);
+    }
+
+    private static Matcher doGetMatcher(String stringToCheck, String regexp) {
+        Pattern pattern = getPattern(regexp);
+        return pattern.matcher(stringToCheck);
+    }
+
+}

Guava/Cache/src/resources/log4j.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+	<!-- Appenders -->
+	<appender name="console" class="org.apache.log4j.ConsoleAppender">
+		<param name="Target" value="System.out" />
+		<layout class="org.apache.log4j.PatternLayout">
+			<param name="ConversionPattern" value="%d %-5p [%t] %c:%L %m%n" />
+		</layout>
+	</appender>
+	
+	<!-- Application Loggers -->
+	<logger name="pl.grzejszczak.marcin">
+		<level value="DEBUG" />
+	</logger>
+	
+	<!-- 3rdparty Loggers -->
+	<logger name="org.springframework.core">
+		<level value="INFO" />
+	</logger>
+	
+	<logger name="org.springframework.beans">
+		<level value="INFO" />
+	</logger>
+	
+	<logger name="org.springframework.context">
+		<level value="INFO" />
+	</logger>
+
+	<logger name="org.springframework.web">
+		<level value="INFO" />
+	</logger>
+	
+	<logger name="org.springframework.security">
+		<level value="INFO" />
+	</logger>
+
+	<!-- Root Logger -->
+	<root>
+		<priority value="DEBUG" />
+		<appender-ref ref="console" />
+	</root>
+	
+</log4j:configuration>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.