Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new tests #7737

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions database
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
create table category (id blob not null, name varchar(255) not null, primary key (id))
create table transaction (amount float not null, transaction_date_time timestamp not null, category_id blob not null, currency varchar(255) not null, id blob not null, primary key (id))

16 changes: 16 additions & 0 deletions database_categories.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
33fcf03d-b8cb-489b-a2a3-ac15828b2c3e,BANK_FEES_AND_CHARGES
6275d856-5c30-448f-8c0f-1d93a3952c5a,CHARITY
916e5f76-110c-44e5-8f6b-eaeb6e508360,EDUCATION
97a9712d-9176-4f25-9e32-f835d6d65d5e,ENTERTAINMENT
4850b299-113f-41fd-ae0c-06025c316f39,GOVERNMENT
04342c26-d140-4c31-b57a-ddb4aac5af54,GROCERIES
16303f68-3dd7-4d56-b6c9-2812cda6fbb3,HEALTH_AND_WELLBEING
d671a7b3-6191-4f27-b880-4fc2ca26e396,LOANS_AND_INVESTMENT
932e72bd-282b-43c7-8b47-962354728bb0,RENT_AND_SERVICES
6bfce29e-c587-4ac7-94d9-4fe3c8a52703,RESTAURANTS_DINING
526d2857-a913-4bd7-a193-7d67fc104bc9,RETAIL
ab11e67c-04f3-4961-aef2-a6aca1095434,SALARY_AND_REVENUE
9b74a4c1-e0ea-4759-9770-c4162715714e,TRANSFER
cb42592a-557e-430a-a937-a6ee69b42a75,TRANSPORT
bd12b4ae-563a-4d33-b129-41539904c9b1,TRAVEL
bc1c0e04-143d-4a68-afde-35db53c065cb,OTHER
1,298 changes: 1,298 additions & 0 deletions database_transactions.csv

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.9"

services:
db:
container_name: db
image: nouchka/sqlite3
volumes:
- ./db/database.db:/root/db/database.db

#volumes:
# db:
# driver: local
81 changes: 49 additions & 32 deletions java/spring/pom.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[3.3,3.4)</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>benchmark.spring-boot</groupId>
<artifactId>benchmark</artifactId>
<version>1.0.0</version>
<name>benchmark</name>
<description>Benchmark for Spring Web Mvc (thread per request model)</description>
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[3.3,3.4)</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>benchmark.spring-boot</groupId>
<artifactId>benchmark</artifactId>
<version>1.0.0</version>
<name>benchmark</name>
<description>Benchmark for Spring Web Mvc (thread per request model)</description>

<properties>
<maven.compiler.release>21</maven.compiler.release>
</properties>
<properties>
<maven.compiler.release>21</maven.compiler.release>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-community-dialects</artifactId>
</dependency>
</dependencies>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,29 +1,178 @@
package benchmark.springboot;

import benchmark.springboot.serialization.SerializationRequest;
import benchmark.springboot.serialization.SerializationResponse;
import benchmark.springboot.web.Category;
import benchmark.springboot.web.CategoryRepository;
import benchmark.springboot.web.Transaction;
import benchmark.springboot.web.TransactionDto;
import benchmark.springboot.web.TransactionRepository;
import benchmark.springboot.web.WebRequest;
import benchmark.springboot.web.WebResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClient;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
public class BenchmarkApplication {
public static void main(String[] args) {
SpringApplication.run(BenchmarkApplication.class, args);
}

@RestController
public class BenchmarkController {
@GetMapping("/")
public void root() {}

@GetMapping("/user/{id}")
public Integer userId(@PathVariable Integer id) {
return id;

public static void main(String[] args) {
SpringApplication.run(BenchmarkApplication.class, args);
}

@PostMapping("/user")
public void user() {}
}
@RestController
@RequiredArgsConstructor
public class BenchmarkController {

@GetMapping("/")
public void root() {
}

// --

@GetMapping("/external-service")
@SneakyThrows
public void externalService() {
Thread.sleep(500);
}

// --

@PostMapping("/serialization")
public SerializationResponse serialization(
@RequestBody List<SerializationRequest> request,
@RequestParam String name,
@RequestParam Double count,
@RequestParam Boolean required,
@RequestParam LocalDateTime startDate
) {
return new SerializationResponse(
request.stream().map(it ->
new SerializationRequest(
it.getName() + "_2",
it.getCount() + 2,
!it.getRequired(),
it.getStartDate().plusDays(2)
)
).toList(),
name + "_2",
count + 2,
!required,
startDate.plusDays(1)
);
}

// --

@Value("${server.port}")
String serverPort;
RestClient webClient = RestClient.builder().build();
String file;
{
try {
file = new String(
Files.readAllBytes(
Path.of(
new File("./../../../database_transactions.csv")
.toURI()
)
)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@SneakyThrows
@GetMapping("/file")
public String getFile() {
return file;
}

@GetMapping("/http-client")
public String httpClient() {
return webClient.get()
.uri("http://localhost:" + serverPort + "/file")
.retrieve()
.body(String.class);
}

// --

private final TransactionRepository transactionRepository;
private final CategoryRepository categoryRepository;

TransactionDto mapToTransactionDto(Transaction tx, List<Category> categories) {
return TransactionDto.builder()
.id(tx.getId())
.categoryName(categories.stream().filter(c -> c.getId().equals(tx.getCategoryId())).findFirst().get().getName())
.amountEUR(tx.getAmount())
.amountUSD(convertToUsd(tx.getAmount()))
.transactionDateTime(tx.getTransactionDateTime())
.build();
}

@SneakyThrows
double convertToUsd(double euro) {
Thread.sleep(10);
return euro * 1.09d;
}

@PostMapping("/web")
public WebResponse web(
@RequestBody WebRequest body
) {

val transactions = transactionRepository
.findAllByTransactionDateTimeBetween(
body.getStartDate(),
body.getEndDate(),
PageRequest.of(body.getPage(), body.getLimit(), Sort.by("id")));

val categoryIds = transactions.stream()
.map(Transaction::getCategoryId)
.collect(Collectors.toMap(p -> p, p -> p, (p, q) -> p))
.values();

val categories = categoryRepository.findAllById(categoryIds);

val transactionsDTO = transactions.stream()
.map(tx -> mapToTransactionDto(tx, categories))
.toList();

val inflows = transactions.stream().mapToDouble(Transaction::getAmount).filter(amount -> amount > 0).sum();
val outflows = transactions.stream().mapToDouble(Transaction::getAmount).filter(amount -> amount < 0).sum();

val response = WebResponse.builder()
.inflows(inflows)
.outflows(outflows)
.totalResponses(transactions.size())
.transactions(transactionsDTO)
.build();

return response;

}

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package benchmark.springboot.serialization;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SerializationRequest {
String name;
Double count;
Boolean required;
LocalDateTime startDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package benchmark.springboot.serialization;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SerializationResponse {
List<SerializationRequest> response;
String name;
Double count;
Boolean required;
LocalDateTime startDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package benchmark.springboot.web;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

import java.util.UUID;

@Data
@ToString(callSuper = true)
@SuperBuilder(toBuilder = true)
@NoArgsConstructor
@Entity
@Table(name = "category")
public class Category {
@Id
@Column(name = "id", updatable = false, nullable = false)
private UUID id;
@Column(name = "name", nullable = false)
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package benchmark.springboot.web;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface CategoryRepository extends JpaRepository<Category, UUID> {
}
Loading
Loading