Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/mail-authentication' int…
Browse files Browse the repository at this point in the history
…o feature/mail-authentication
  • Loading branch information
LeeJeongGi committed Apr 13, 2024
2 parents 813dd59 + 31ba2c0 commit 317afb9
Show file tree
Hide file tree
Showing 47 changed files with 1,285 additions and 375 deletions.
20 changes: 19 additions & 1 deletion bm-common/src/main/java/org/benchmarker/bmagent/AgentInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,36 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.benchmarker.bmcommon.util.RandomUtils;
import org.benchmarker.util.Randomized;

@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class AgentInfo {
public class AgentInfo implements Randomized<AgentInfo> {
private AgentStatus status;
private Set<Long> templateId;
private double cpuUsage;
private double memoryUsage;
private String serverUrl;
private ZonedDateTime startedAt;

@Override
public AgentInfo random() {
return AgentInfo.builder()
.status(AgentStatus.READY)
.startedAt(ZonedDateTime.now())
.memoryUsage(RandomUtils.randDouble(1,10))
.cpuUsage(RandomUtils.randDouble(1,10))
.serverUrl(RandomUtils.randString(10))
.templateId(Set.of(
RandomUtils.randLong(1,5),
RandomUtils.randLong(6,10),
RandomUtils.randLong(11,15)
))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.benchmarker.bmcommon.dto;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@ToString
public class MTTFBInfo {
private LocalDateTime timestamp;
private String mttbfb;
}
20 changes: 20 additions & 0 deletions bm-common/src/main/java/org/benchmarker/bmcommon/dto/TPSInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.benchmarker.bmcommon.dto;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class TPSInfo {
private LocalDateTime timestamp;
private double tps;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.benchmarker.bmcommon.util;

import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.benchmarker.bmagent.AgentStatus;
import org.benchmarker.bmcommon.dto.CommonTestResult;
Expand Down Expand Up @@ -57,4 +58,30 @@ public static double randDouble(long min, long max) {
return ThreadLocalRandom.current().nextDouble(min, max + 1);
}

public static String randString(int length) {
// Define the ASCII range for alphabetic characters
int lowerBound = 65; // ASCII value for 'A'
int upperBound = 122; // ASCII value for 'z'

// Create a StringBuilder to store the random string
StringBuilder stringBuilder = new StringBuilder(length);

// Create a Random object
Random random = new Random();

// Generate random characters and append them to the StringBuilder
for (int i = 0; i < length; i++) {
// Generate a random ASCII value within the range
int asciiValue = lowerBound + random.nextInt(upperBound - lowerBound + 1);

// Convert the ASCII value to a character and append it to the StringBuilder
stringBuilder.append((char) asciiValue);
}

// Return the generated random string
return stringBuilder.toString();
}



}
16 changes: 16 additions & 0 deletions bm-common/src/main/java/org/benchmarker/util/Randomized.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.benchmarker.util;

/**
* For randomized object generation
*
* @param <T>
*/
public interface Randomized<T> {

/**
* Generate randomized object
*
* @return T
*/
T random();
}
9 changes: 8 additions & 1 deletion bm-controller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ java {
sourceCompatibility = '17'
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs << '-parameters'
}

configurations {
asciidoctorExt
compileOnly {
Expand Down Expand Up @@ -84,6 +88,8 @@ dependencies {
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

implementation "io.jsonwebtoken:jjwt-api:0.11.1"

testImplementation 'org.testng:testng:7.1.0'
Expand All @@ -100,8 +106,9 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

implementation 'org.springframework.cloud:spring-cloud-starter-kubernetes-client'

implementation 'org.springframework.boot:spring-boot-starter-mail' //mail
implementation 'org.webjars:webjars-locator-core'
implementation 'org.webjars:chartjs:2.9.3'

implementation 'org.springframework.boot:spring-boot-starter-validation'
asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class BmControllerApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,45 @@
@Getter
@Slf4j
public class AgentServerManager {

// store current status of all agents
private final ConcurrentHashMap<String, AgentInfo> agentsUrl = new ConcurrentHashMap<>();

// key: running templateId, value: relate agent
private final HashMap<Long, String> agentMapped = new HashMap<>();

public void add(String url, AgentInfo agentInfo) {
agentsUrl.put(url, agentInfo);
}
public void update(String url, AgentInfo agentInfo){
if (agentsUrl.get(agentInfo.getServerUrl())==null){

public void update(AgentInfo agentInfo) {
if (agentsUrl.get(agentInfo.getServerUrl()) == null) {
log.error("cannot find agent");
return;
}
agentsUrl.put(agentInfo.getServerUrl(), agentInfo);
}

public Optional<AgentInfo> getReadyAgent(){
for (AgentInfo agentInfo : agentsUrl.values()){
if (agentInfo.getStatus()== AgentStatus.READY){
public Optional<AgentInfo> getReadyAgent() {
for (AgentInfo agentInfo : agentsUrl.values()) {
if (agentInfo.getStatus() == AgentStatus.READY) {
return Optional.of(agentInfo);
}
}
return Optional.empty();
}

public void removeAgent(String url){
public void remove(String url) {
agentsUrl.remove(url);
}

public void addTemplateRunnerAgent(Long id, String url){
agentMapped.put(id,url);
public void addTemplateRunnerAgent(Long id, String url) {
agentMapped.put(id, url);
}

public void removeTemplateRunnerAgent(Long id){
public void removeTemplateRunnerAgent(Long id) {
String url = agentMapped.get(id);
if (url != null){
if (url != null) {
agentMapped.remove(id);
agentsUrl.remove(url);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.benchmarker.bmcontroller.common.beans;

import java.time.Duration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Slf4j
@Configuration
@EnableRedisRepositories
@Profile("!test")
public class RedisConfig {

@Value("${spring.data.redis.host}")
private String redisHost;

@Value("${spring.data.redis.port}")
private int redisPort;

/**
* RedisConnectionFactory를 통해 내장 혹은 외부의 Redis를 연결합니다.
* @return RedisConnectionFactory
*/
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(redisHost, redisPort);
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
return redisTemplate;
}

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1)) // 캐시 유효 기간 설정
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

return RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public enum ErrorCode {
GROUP_NOT_FOUND(404, "그룹이 존재하지 않습니다."),
TEMPLATE_NOT_FOUND(404, "탬플릿이 존재하지 않습니다."),
TEMPLATE_RESULT_NOT_FOUND(404, "탬플릿에 대한 결과가 존재하지 않습니다."),
TEST_NOT_FOUND(404, "테스트 결과가 존재하지 않습니다."),

/**
* 500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public static ResponseEntity<GlobalErrorResponse> toResponseEntity(ErrorCode err

public static ResponseEntity<GlobalErrorResponse> toResponseEntity(GlobalException e) {
ErrorCode errorCode = e.getErrorCode();
if (e.getMessage() != null){
return ResponseEntity
.status(errorCode.getHttpStatus())
.body(GlobalErrorResponse.builder()
.status(errorCode.getHttpStatus())
.code(errorCode.name())
.message(e.getMessage())
.build()
);
}
return ResponseEntity
.status(errorCode.getHttpStatus())
.body(GlobalErrorResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class GlobalException extends RuntimeException {

private final ErrorCode errorCode;
private String message = null;

public static ErrorCode toErrorCode(Throwable e) {
GlobalException customException = (GlobalException) e;
Expand All @@ -22,4 +23,10 @@ public GlobalException(GlobalErrorResponse e) {
public GlobalException(ErrorCode errorCode) {
this.errorCode = errorCode;
}

public GlobalException(ErrorCode errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.benchmarker.bmcontroller.preftest.common;

import java.time.LocalDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.benchmarker.bmagent.AgentStatus;
import org.benchmarker.bmcommon.util.RandomUtils;
import org.benchmarker.util.Randomized;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class TestInfo implements Randomized<TestInfo> {

private String groupId;
private Integer templateId;
private String testId;
private AgentStatus testStatus;
private LocalDateTime startedAt;

@Override
public TestInfo random() {
return TestInfo.builder()
.testId(UUID.randomUUID().toString())
.startedAt(LocalDateTime.now())
.testStatus(AgentStatus.TESTING)
.templateId(RandomUtils.randInt(0, 100))
.groupId("defaultGroupId")
.build();
}
}
Loading

0 comments on commit 317afb9

Please sign in to comment.