Skip to content

Commit

Permalink
Merge pull request #19 from IvanKondrashkov/homework-11
Browse files Browse the repository at this point in the history
feat: Добавлены нагрузочные тесты, модуль hw09-back
  • Loading branch information
IvanKondrashkov authored Dec 15, 2024
2 parents 04b4bbb + 034b999 commit f2ded4f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ allprojects {
dependencies {
imports {
//mavenBom("io.micronaut:micronaut-bom:3.10.4")
mavenBom("org.apache.jmeter:ApacheJMeter_bom:5.6.3")
}
dependency("com.google.guava:guava:${Versions.guava}")
dependency("org.junit.jupiter:junit-jupiter:${Versions.junit_jupiter}")
Expand All @@ -38,6 +39,8 @@ allprojects {
dependency("com.zaxxer:HikariCP:${Versions.hikari}")
dependency("org.postgresql:postgresql:${Versions.postgres}")
dependency("org.flywaydb:flyway-core:${Versions.flyway}")
dependency("com.google.code.gson:gson:${Versions.gson}")
dependency("us.abstracta.jmeter:jmeter-java-dsl:${Versions.jmeter_java_dsl}")
}
}

Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/groovy/Versions.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ interface Versions {
String hikari = '5.0.1'
String postgres = '42.5.1'
String flyway = '9.12.0'
String gson = '2.11.0'
String jmeter_java_dsl = '1.29.1'
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62748
57245
2 changes: 2 additions & 0 deletions hw09-back/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation 'io.micronaut.serde:micronaut-serde-jackson'
implementation 'io.micronaut.sql:micronaut-hibernate-jpa'
implementation 'io.micronaut.sql:micronaut-jdbc-hikari'
implementation 'com.google.code.gson:gson:2.11.0'

compileOnly 'io.micronaut.data:micronaut-data-processor'
compileOnly 'io.micronaut:micronaut-http-client'
Expand All @@ -34,6 +35,7 @@ dependencies {
testImplementation 'org.testcontainers:postgresql'
testImplementation 'org.testcontainers:testcontainers'
testImplementation 'io.micronaut:micronaut-http-client'
testImplementation 'us.abstracta.jmeter:jmeter-java-dsl'

testRuntimeOnly 'org.postgresql:postgresql'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.otus.homework.util

import java.time.LocalDateTime
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import java.time.format.DateTimeFormatter

class LocalDateTimeAdapter extends TypeAdapter<LocalDateTime> {
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME

@Override
void write(JsonWriter jsonWriter, LocalDateTime date) throws IOException {
if (date == null) {
jsonWriter.nullValue()
return
}
jsonWriter.value(date.format(formatter))
}

@Override
LocalDateTime read(JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull()
return null
}
return LocalDateTime.parse(jsonReader.nextString(), formatter)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package ru.otus.homework.perfomans

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.micronaut.context.annotation.Property
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import java.time.Duration
import java.time.LocalDateTime
import spock.lang.Specification
import ru.otus.homework.model.Task
import ru.otus.homework.model.Action
import org.apache.http.entity.ContentType
import ru.otus.homework.util.LocalDateTimeAdapter
import us.abstracta.jmeter.javadsl.core.TestPlanStats
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;

@MicronautTest
@Property(name = "datasources.default.driver-class-name", value = "org.testcontainers.jdbc.ContainerDatabaseDriver")
@Property(name = "datasources.default.url", value = "jdbc:tc:postgresql:///db")
class PerfomansTest extends Specification {
Task task
Action action
Gson gson

String taskId
String actionId

static final String BASE_URL = "http://localhost:9090"

def setup() {
task = new Task(
name: "learning",
description: "read book",
startDate: LocalDateTime.of(2024, 11, 1, 0, 0),
endDate: LocalDateTime.of(2024, 11, 10, 0, 0),
)
action = new Action(
name: "read",
description: "read math",
startDate: LocalDateTime.of(2024, 11, 2, 0, 0),
endDate: LocalDateTime.of(2024, 11, 3, 0, 0),
task: task
)
gson = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.serializeNulls()
.create()
}

void cleanup() {
task = null
action = null
gson = null
}

def "saveTask"() {
when:
TestPlanStats stats = testPlan(
threadGroup(10, 20,
httpSampler(BASE_URL + "/tasks")
.post(gson.toJson(task), ContentType.APPLICATION_JSON)
.children(
jsr223PostProcessor(
"if (prev.responseCode == '400') { prev.successful = true }")
)
),
jtlWriter("build/jtls"),
htmlReporter("build/reports/jmeter")
).run()

then:
stats.overall().sampleTimePercentile99() <= Duration.ofSeconds(1)
}

def "saveAction"() {
when:
TestPlanStats stats = testPlan(
threadGroup(10, 20,
httpSampler(BASE_URL + "/actions")
.post(gson.toJson(action), ContentType.APPLICATION_JSON)
.children(
jsr223PostProcessor(
"if (prev.responseCode == '400') { prev.successful = true }")
)
),
jtlWriter("build/jtls"),
htmlReporter("build/reports/jmeter")
).run()

then:
stats.overall().sampleTimePercentile99() <= Duration.ofSeconds(1)
}

def "findByIdTask"() {
when:
TestPlanStats stats = testPlan(
threadGroup(10, 20,
httpSampler(BASE_URL + "/tasks")
.post(gson.toJson(task), ContentType.APPLICATION_JSON)
.children(
jsonExtractor(taskId, "[].id"),
debugPostProcessor()
),
httpSampler(BASE_URL + "/tasks/${taskId}")
),
jtlWriter("build/jtls"),
htmlReporter("build/reports/jmeter")
).run()

then:
stats.overall().sampleTimePercentile99() <= Duration.ofSeconds(1)
}

def "findByIdAction"() {
when:
TestPlanStats stats = testPlan(
threadGroup(10, 20,
httpSampler(BASE_URL + "/actions")
.post(gson.toJson(action), ContentType.APPLICATION_JSON)
.children(
jsonExtractor(actionId, "[].id"),
debugPostProcessor()
),
httpSampler(BASE_URL + "/actions/${actionId}")
),
jtlWriter("build/jtls"),
htmlReporter("build/reports/jmeter")
).run()

then:
stats.overall().sampleTimePercentile99() <= Duration.ofSeconds(1)
}
}
Binary file added image/jmeter-dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f2ded4f

Please sign in to comment.