Skip to content

Commit d8409ed

Browse files
committed
Added /load/simple, /load/random and /load/fibonacci endpoints
Enabled virtual threads
1 parent d98be04 commit d8409ed

File tree

8 files changed

+125
-3
lines changed

8 files changed

+125
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ out/
3535

3636
### VS Code ###
3737
.vscode/
38+
39+
deploy/

build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ plugins {
33
id 'war'
44
id 'org.springframework.boot' version '3.4.0'
55
id 'io.spring.dependency-management' version '1.1.6'
6+
id 'eclipse'
67
}
78

89
group = 'com.tagit.commons'
@@ -14,18 +15,46 @@ java {
1415
}
1516
}
1617

18+
configurations {
19+
compileOnly {
20+
extendsFrom annotationProcessor
21+
}
22+
}
23+
1724
repositories {
1825
mavenCentral()
1926
}
2027

2128
dependencies {
29+
compileOnly 'org.projectlombok:lombok'
30+
annotationProcessor 'org.projectlombok:lombok'
31+
2232
implementation 'org.springframework.boot:spring-boot-starter-actuator'
2333
implementation 'org.springframework.boot:spring-boot-starter-web'
34+
2435
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
36+
2537
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2638
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
2739
}
2840

2941
tasks.named('test') {
3042
useJUnitPlatform()
3143
}
44+
45+
jar {
46+
enabled = false
47+
}
48+
49+
war {
50+
enabled = false
51+
}
52+
53+
54+
task copyWar(type: Copy) {
55+
dependsOn bootWar
56+
from war, bootJar, bootWar
57+
into "deploy"
58+
}
59+
60+
defaultTasks 'clean', 'eclipse', 'build', 'copyWar'

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'LoadSimulator'
1+
rootProject.name = 'load-simulator'

src/main/java/com/tagit/commons/LoadSimulator/LoadSimulatorApplication.java renamed to src/main/java/com/tagit/commons/loadsimulator/LoadSimulatorApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.tagit.commons.LoadSimulator;
1+
package com.tagit.commons.loadsimulator;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;

src/main/java/com/tagit/commons/LoadSimulator/ServletInitializer.java renamed to src/main/java/com/tagit/commons/loadsimulator/ServletInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.tagit.commons.LoadSimulator;
1+
package com.tagit.commons.loadsimulator;
22

33
import org.springframework.boot.builder.SpringApplicationBuilder;
44
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.tagit.commons.loadsimulator.web;
2+
3+
import java.security.SecureRandom;
4+
import java.util.Arrays;
5+
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import lombok.extern.apachecommons.CommonsLog;
10+
11+
@RestController
12+
@CommonsLog
13+
public class LoadTestController {
14+
15+
@GetMapping("/load/simple")
16+
public String load1() {
17+
long start = System.currentTimeMillis();
18+
// Simulate some CPU load
19+
// For example, compute a large Fibonacci number or do a CPU-intensive loop.
20+
// Here, we'll do a simple CPU-bound operation: sum of a large array.
21+
int size = 2000000;
22+
long [] numbers = new long[size];
23+
for (int i = 0; i < size; i++) {
24+
numbers[i] = i;
25+
}
26+
27+
long sum = 0;
28+
for (long n : numbers) {
29+
sum += n;
30+
}
31+
32+
// Return a string response after the computation
33+
long end = System.currentTimeMillis();
34+
long elapsed = end - start;
35+
log.info("Computation done. Sum = " + sum + ". Time elapsed = " + elapsed + " ms. on " + Thread.currentThread().toString());
36+
return "Computation done. Sum = " + sum;
37+
}
38+
39+
@GetMapping("/load/random")
40+
public String load2() {
41+
long start = System.currentTimeMillis();
42+
// Define a large size for the array
43+
int size = 400000;
44+
45+
// Create and populate a large array of random integers
46+
int[] numbers = new int[size];
47+
SecureRandom rand = new SecureRandom();
48+
for (int i = 0; i < size; i++) {
49+
numbers[i] = rand.nextInt(Integer.MAX_VALUE);
50+
}
51+
52+
// Sum the elements to generate CPU usage
53+
long sum = 0;
54+
for (int n : numbers) {
55+
sum += n;
56+
}
57+
58+
// Sort the array to increase CPU usage further
59+
Arrays.sort(numbers);
60+
long end = System.currentTimeMillis();
61+
long elapsed = end - start;
62+
log.info("Computation done. Sum of Random Numbers = " + sum + ". Time elapsed = " + elapsed + " ms. on " + Thread.currentThread().toString());
63+
return "Computation done. Sum of Random Numbers = " + sum;
64+
}
65+
66+
@GetMapping("/load/fibonacci")
67+
public String loadFibonacci() {
68+
long start = System.currentTimeMillis();
69+
// Perform another CPU-heavy operation (e.g., compute Fibonacci)
70+
// Let's do a small but CPU-intensive Fibonacci calculation
71+
// This is a naive recursive Fibonacci for demonstration (CPU heavy)
72+
int fibNum = 35; // This is chosen to be somewhat expensive but not infinite
73+
long fibResult = fibonacci(fibNum);
74+
75+
long end = System.currentTimeMillis();
76+
long elapsed = end - start;
77+
log.info("Computation done. Fib(" + fibNum + ") = " + fibResult + ". Time elapsed = " + elapsed + " ms. on " + Thread.currentThread().toString());
78+
return "Computation done. Fib(" + fibNum + ") = " + fibResult;
79+
}
80+
81+
// Naive Fibonacci function (O(2^n) complexity)
82+
private long fibonacci(int n) {
83+
if (n <= 1) return n;
84+
return fibonacci(n - 1) + fibonacci(n - 2);
85+
}
86+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.tagit.commons.loadsimulator.web;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
spring.application.name=LoadSimulator
2+
3+
server.port=8080
4+
5+
spring.threads.virtual.enabled=true

0 commit comments

Comments
 (0)