Created
April 11, 2019 18:08
-
-
Save gokhanoner/6852eb7721b419bb679bf080ebd6daff to your computer and use it in GitHub Desktop.
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 com.example.cacheabletest; | |
import com.hazelcast.config.Config; | |
import com.hazelcast.core.HazelcastInstance; | |
import com.hazelcast.core.IMap; | |
import java.time.Duration; | |
import java.time.Instant; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.concurrent.locks.LockSupport; | |
import java.util.function.Supplier; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cache.annotation.Cacheable; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.stereotype.Service; | |
@SpringBootApplication | |
@EnableCaching | |
public class CacheableTestApplication { | |
private static final Logger log = LoggerFactory.getLogger(CacheableTestApplication.class); | |
public static void main(String[] args) { | |
SpringApplication.run(CacheableTestApplication.class, args); | |
} | |
@Bean | |
Config config() { | |
return new Config(); | |
} | |
@Bean | |
CommandLineRunner cli(TestService testService) { | |
return (args) -> { | |
List<Supplier<Integer>> oprs = new ArrayList<>(); | |
oprs.add(() -> testService.getValueViaAnnotation(1)); | |
oprs.add(() -> testService.getValueViaIMap(1)); | |
oprs.forEach(opr -> { | |
try { | |
List<Tuple2<Long, Integer>> results = IntStream.range(0, 100) | |
.mapToObj(i -> timeIt(opr)) | |
.sorted(Comparator.comparing(Tuple2::getF0)) | |
.collect(Collectors.toList()); | |
log.info("Min - {}", results.get(0).getF0()); | |
log.info("Max - {}", results.get(results.size() - 1).getF0()); | |
log.info("Avg - {}", results.stream().mapToLong(Tuple2::getF0).average()); | |
} catch (Exception e) { | |
log.error("{}", e); | |
} | |
}); | |
}; | |
} | |
private <T> Tuple2<Long, T> timeIt(Supplier<T> opr) { | |
T result = null; | |
Instant start = Instant.now(); | |
try { | |
result = opr.get(); | |
} finally { | |
return Tuple2.of(Duration.between(start, Instant.now()).toNanos(), result); | |
} | |
} | |
} | |
@Service | |
class TestService { | |
private final IMap<Integer, Integer> imap; | |
public TestService(HazelcastInstance hz) { | |
this.imap = hz.getMap("cache2"); | |
} | |
@Cacheable("cache1") | |
public int getValueViaAnnotation(int key) { | |
//Sleep a bit for the first run | |
LockSupport.parkNanos((long) 1e9); | |
return 1; | |
} | |
int getValueViaIMap(int key) { | |
//Sleep a bit for the first run | |
Integer val = imap.get(key); | |
if (val == null) { | |
imap.set(key, 1); | |
//Sleep a bit for the first run | |
LockSupport.parkNanos((long) 1e9); | |
} | |
return 1; | |
} | |
} | |
class Tuple2<K, V> { | |
private final K f0; | |
private final V f1; | |
public static <K, V> Tuple2<K, V> of(K f0, V f1) { | |
return new Tuple2<>(f0, f1); | |
} | |
private Tuple2(K f0, V f1) { | |
this.f0 = f0; | |
this.f1 = f1; | |
} | |
public K getF0() { | |
return f0; | |
} | |
public V getF1() { | |
return f1; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
Tuple2<?, ?> tuple2 = (Tuple2<?, ?>) o; | |
return Objects.equals(f0, tuple2.f0) && | |
Objects.equals(f1, tuple2.f1); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(f0, f1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment