Last active
August 29, 2015 14:10
-
-
Save thomasdarimont/9e75961de13046f24b92 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
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" | |
xmlns:util="http://www.springframework.org/schema/util" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd | |
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> | |
<mongo:mongo id="mongo"> | |
<mongo:options connections-per-host="200" | |
threads-allowed-to-block-for-connection-multiplier="10" /> | |
</mongo:mongo> | |
<mongo:db-factory id="mongoDb" mongo-ref="mongo" | |
write-concern="JOURNAL_SAFE" dbname="test" /> | |
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> | |
<constructor-arg ref="mongoDb" /> | |
</bean> | |
<mongo:repositories base-package="test.test" /> | |
</beans> |
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>test</groupId> | |
<artifactId>spring-data-mongodb-repository-insert</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>spring-data-mongodb-repository-insert</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-mongodb</artifactId> | |
<version>1.7.0.DATAMONGO-1054-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<version>4.0.7.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-jdk14</artifactId> | |
<version>1.7.7</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-lang3</artifactId> | |
<version>3.3.2</version> | |
</dependency> | |
</dependencies> | |
</project> |
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 test.test; | |
import org.springframework.data.annotation.Id; | |
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; | |
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
@Document | |
public class Shoreline { | |
@Id | |
private String id; | |
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) | |
private double[] point; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public double[] getPoint() { | |
return point; | |
} | |
public void setPoint(double[] point) { | |
this.point = point; | |
} | |
} |
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 test.test; | |
import org.springframework.data.mongodb.repository.MongoRepository; | |
interface ShorelineRepository extends MongoRepository<Shoreline, String> {} |
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 test.test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.commons.lang3.time.StopWatch; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.mongodb.core.MongoTemplate; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; | |
@ContextConfiguration("classpath:application-context.xml") | |
public class ShorelineRepositoryTest extends AbstractJUnit4SpringContextTests { | |
@Autowired private ShorelineRepository shorelineRepository; | |
@Autowired private MongoTemplate mongoTemplate; | |
private Logger logger = LoggerFactory.getLogger(getClass()); | |
private List<Shoreline> list; | |
@Before | |
public void init() { | |
shorelineRepository.deleteAll(); | |
list = create(500); | |
} | |
@Test | |
public void testTemplateInsertSingle() { | |
StopWatch stopWatch = new StopWatch(); | |
stopWatch.start(); | |
for (Shoreline s : list) { | |
mongoTemplate.insert(s); | |
} | |
stopWatch.stop(); | |
logger.info("testTemplateInsertSingle took {}ms", stopWatch.getTime()); | |
} | |
@Test | |
public void testTemplateInsertList() { | |
StopWatch stopWatch = new StopWatch(); | |
stopWatch.start(); | |
for (Shoreline s : list) { | |
mongoTemplate.insert(s); | |
} | |
stopWatch.stop(); | |
logger.info("testTemplateInsertList took {}ms", stopWatch.getTime()); | |
} | |
@Test | |
public void testRepositoryInsertSingle() { | |
StopWatch stopWatch = new StopWatch(); | |
stopWatch.start(); | |
for (Shoreline s : list) { | |
shorelineRepository.insert(s); | |
} | |
stopWatch.stop(); | |
logger.info("testRepositoryInsertSingle took {}ms", stopWatch.getTime()); | |
} | |
@Test | |
public void testRepositoryInsertList() { | |
StopWatch stopWatch = new StopWatch(); | |
stopWatch.start(); | |
shorelineRepository.insert(list); | |
stopWatch.stop(); | |
logger.info("testRepositoryInsertList took {}ms", stopWatch.getTime()); | |
} | |
@Test | |
public void testRepositorySaveSingle() { | |
StopWatch stopWatch = new StopWatch(); | |
stopWatch.start(); | |
for (Shoreline s : list) { | |
shorelineRepository.save(s); | |
} | |
stopWatch.stop(); | |
logger.info("testRepositorySaveSingle took {}ms", stopWatch.getTime()); | |
} | |
@Test | |
public void testRepositorySaveList() { | |
StopWatch stopWatch = new StopWatch(); | |
stopWatch.start(); | |
shorelineRepository.save(list); | |
stopWatch.stop(); | |
logger.info("testRepositorySaveList took {}ms", stopWatch.getTime()); | |
} | |
private List<Shoreline> create(int n) { | |
List<Shoreline> list = new ArrayList<Shoreline>(); | |
for (int i = 0; i < n; i++) { | |
Shoreline shoreline = new Shoreline(); | |
shoreline.setPoint(new double[] { 101 + (0.001 * i), 3 + (0.001 * i) }); | |
list.add(shoreline); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example run: