Skip to content

Instantly share code, notes, and snippets.

@skuro
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skuro/648cf1d871d203a73a0c to your computer and use it in GitHub Desktop.
Save skuro/648cf1d871d203a73a0c to your computer and use it in GitHub Desktop.
Environment micro benchmark
@PropertySource(["classpath:application.properties"])
@Configuration
@ComponentScan("org.sample")
public class AppConfig {
@Autowired
private Environment environment
}
users.carlo.greeting=Saludi e trigu # used for the per-request property lookup
users.greetings=carlo:Saludi e trigu # used for construct time preload
@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"
}
}
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