Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample OpenHFT import using spring boot
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.