Created
July 22, 2015 14:40
-
-
Save agrison/b51465fee8d46f675fff to your computer and use it in GitHub Desktop.
Sample OpenHFT import using spring boot
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 me.grison.openhft.foo; | |
import net.openhft.collections.SharedHashMap; | |
import net.openhft.collections.SharedHashMapBuilder; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import java.io.File; | |
import java.util.UUID; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
@ComponentScan | |
@Configuration | |
@SpringBootApplication | |
@EnableAutoConfiguration | |
public class OpenHFTMassImport { | |
@Bean | |
CommandLineRunner init() { | |
return args -> { | |
SharedHashMap<String, String> map = new SharedHashMapBuilder() | |
.create(File.createTempFile("mass-import", "foo"), String.class, String.class); | |
int numKeys = 1_000_000; | |
int availableProcessors = Runtime.getRuntime().availableProcessors() * 4; | |
ExecutorService exec = Executors.newFixedThreadPool(availableProcessors); | |
// pre-compute UUIDs | |
String[] uuids = new String[numKeys]; | |
for (int i = 0; i < numKeys; ++i) | |
uuids[i] = UUID.randomUUID().toString(); | |
// split | |
final int portion = numKeys / availableProcessors; | |
long time = System.nanoTime(); | |
for (int i = 0; i < availableProcessors; ++i) { | |
final int threadId = i; | |
exec.submit(() -> { | |
// throw the data at OpenHFT | |
for (int j = threadId * portion; j < (threadId + 1) * portion; ++j) | |
map.put(uuids[j], uuids[j]); | |
}); | |
} | |
exec.shutdown(); | |
try { | |
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); | |
long endTime = System.nanoTime(); | |
System.out.println("\tTook " + (endTime - time) + "ns to execute."); | |
} catch (InterruptedException e) { | |
System.err.println("Execution interrupted: " + e.getMessage()); | |
} | |
}; | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(OpenHFTMassImport.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment