Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Last active August 29, 2015 14:10
Show Gist options
  • Save thomasdarimont/9e75961de13046f24b92 to your computer and use it in GitHub Desktop.
Save thomasdarimont/9e75961de13046f24b92 to your computer and use it in GitHub Desktop.
<?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>
<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>
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;
}
}
package test.test;
import org.springframework.data.mongodb.repository.MongoRepository;
interface ShorelineRepository extends MongoRepository<Shoreline, String> {}
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;
}
}
@thomasdarimont
Copy link
Author

Example run:

Nov 28, 2014 4:31:34 PM org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
Nov 28, 2014 4:31:35 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [application-context.xml]
Nov 28, 2014 4:31:35 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@21a06946: startup date [Fri Nov 28 16:31:35 CET 2014]; root of context hierarchy
Nov 28, 2014 4:31:53 PM test.test.ShorelineRepositoryTest testTemplateInsertSingle
INFO: testTemplateInsertSingle took 17536ms
Nov 28, 2014 4:32:10 PM test.test.ShorelineRepositoryTest testRepositorySaveList
INFO: testRepositorySaveList took 17612ms
Nov 28, 2014 4:32:11 PM test.test.ShorelineRepositoryTest testRepositoryInsertList
INFO: testRepositoryInsertList took 39ms
Nov 28, 2014 4:32:28 PM test.test.ShorelineRepositoryTest testRepositorySaveSingle
INFO: testRepositorySaveSingle took 17629ms
Nov 28, 2014 4:32:46 PM test.test.ShorelineRepositoryTest testTemplateInsertList
INFO: testTemplateInsertList took 17651ms
Nov 28, 2014 4:33:03 PM test.test.ShorelineRepositoryTest testRepositoryInsertSingle
INFO: testRepositoryInsertSingle took 17536ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment