Last active
August 29, 2015 14:14
-
-
Save skuro/648cf1d871d203a73a0c to your computer and use it in GitHub Desktop.
Environment micro benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PropertySource(["classpath:application.properties"]) | |
@Configuration | |
@ComponentScan("org.sample") | |
public class AppConfig { | |
@Autowired | |
private Environment environment | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users.carlo.greeting=Saludi e trigu # used for the per-request property lookup | |
users.greetings=carlo:Saludi e trigu # used for construct time preload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
class Greeter { | |
private final Environment environment | |
private final Map<String, String> greetings | |
@Autowired | |
public Greeter(Environment environment) { | |
this.environment = environment; | |
this.greetings = loadGreetings() | |
} | |
def loadGreetings() { | |
def greetings = [:] | |
def rawGreetings = environment.getProperty("users.greetings") | |
rawGreetings.split(";").collect { | |
def (name, greeting) = it.split(":") | |
greetings << [(name): greeting] | |
} | |
return greetings | |
} | |
def slow(String user) { | |
def greeting = environment.getProperty("users.${user}.greeting") | |
if(greeting != null) "hit" | |
else "miss" | |
} | |
def fast(String user) { | |
def greeting = greetings.get(user) | |
if(greeting != null) "hit" | |
else "miss" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sample | |
import org.openjdk.jmh.annotations.Benchmark | |
import org.openjdk.jmh.annotations.Scope | |
import org.openjdk.jmh.annotations.State | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext | |
import java.util.concurrent.Callable | |
import java.util.function.Function | |
@State(Scope.Benchmark) | |
class MyBenchmark { | |
final Greeter greeter | |
final String[] fixture | |
public MyBenchmark(){ | |
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class) | |
greeter = ctx.getBean(Greeter.class) | |
// 20 elements are missing in the props file, one is found | |
fixture = (0..20).collect {"missing${it}" as String} << "carlo" | |
} | |
def run(method) { | |
fixture.collect { method(it) } | |
} | |
// @Benchmark | |
def testSlow() { | |
run { str -> greeter.slow(str) + "<- DONE" } | |
} | |
@Benchmark | |
def testFast() { | |
run { str -> greeter.fast(str) + "<- DONE" } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment