Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spring Data Redis Repository Benchmark
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.stereotype.Component;
@Component
@ReadingConverter
public class ByteToEmpId implements Converter<byte[], Employee.EmpId> {
@Override
public Employee.EmpId convert(byte[] bytes) {
String[] empId = new String(bytes).split("-");
return new Employee.EmpId(empId[0], Long.valueOf(empId[1]));
}
}
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.stereotype.Component;
@Component
@WritingConverter
public class EmpIdToByte implements Converter<Employee.EmpId, byte[]> {
@Override
public byte[] convert(Employee.EmpId empId) {
return (empId.getBatch() + "-" + empId.getId()).getBytes();
}
}
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.stereotype.Component;
@Component
@WritingConverter
public class EmpIdToString implements Converter<Employee.EmpId, String> {
@Override
public String convert(Employee.EmpId empId) {
return empId.getBatch() + "-" + empId.getId();
}
}
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import java.io.Serializable;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
@RedisHash(value = "employee")
public class Employee {
@Id
private EmpId id;
private String name;
private Address address;
public Employee() {
}
public Employee(EmpId id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
public EmpId getId() {
return id;
}
public void setId(EmpId id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public static class Address {
private String city;
public Address() {
}
public Address(String city) {
this.city = city;
}
}
public static class EmpId implements Serializable {
private String batch;
private long id;
public EmpId() {
}
public EmpId(String batch, long id) {
this.batch = batch;
this.id = id;
}
public String getBatch() {
return batch;
}
public long getId() {
return id;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>redis-perf</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.9.RELEASE</version>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.23</version>
</dependency>
</dependencies>
</project>
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.convert.CustomConversions;
import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.data.redis.repository.support.RedisRepositoryFactory;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.repository.CrudRepository;
/**
* @author Mark Paluch
*/
@BenchmarkMode(Mode.Throughput)
@Fork(value = 1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.SECONDS)
@Timeout(time = 10)
public class RedisRepositoryBenchmark {
@Benchmark
public void save(Resources resources) {
Employee person = new Employee();
person.setId(new Employee.EmpId(UUID.randomUUID().toString(), 1L));
person.setName("White");
person.setAddress(new Employee.Address("city"));
resources.repository.save(person);
}
@State(Scope.Benchmark)
public static class Resources {
private final PersonRepository repository;
private final LettuceConnectionFactory lcf;
public Resources() {
this.lcf = new LettuceConnectionFactory();
lcf.afterPropertiesSet();
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setDefaultSerializer(new StringRedisSerializer());
template.setConnectionFactory(lcf);
template.afterPropertiesSet();
RedisMappingContext mappingContext = new RedisMappingContext();
CustomConversions conversions = new CustomConversions(Arrays
.asList(new EmpIdToByte(), new EmpIdToString()));
RedisKeyValueAdapter keyValueAdapter = new RedisKeyValueAdapter(template, mappingContext, conversions);
keyValueAdapter.afterPropertiesSet();
KeyValueTemplate keyValueTemplate = new KeyValueTemplate(keyValueAdapter, mappingContext);
keyValueAdapter.afterPropertiesSet();
RedisRepositoryFactory factory = new RedisRepositoryFactory(keyValueTemplate);
this.repository = factory.getRepository(PersonRepository.class);
repository.deleteAll();
}
@TearDown
public void cleanup() {
lcf.destroy();
}
}
interface PersonRepository extends CrudRepository<Employee, String> {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment