Skip to content

Commit

Permalink
added benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Oct 10, 2024
1 parent 68737c4 commit fc10bf4
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 19 deletions.
54 changes: 35 additions & 19 deletions dorm/graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
<!-- dependencies -->

<dependencies>
<!-- kotlin -->

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>

<!-- local -->

<dependency>
Expand All @@ -28,51 +40,55 @@

<!-- spring -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- TODO cleanup -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- graphql -->

<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>13.0.5</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>22.3</version>
</dependency>

<!-- kotlin -->
<!-- test -->

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<!--dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency-->
<artifactId>kotlin-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<artifactId>kotlin-test-junit5</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
103 changes: 103 additions & 0 deletions dorm/graphql/src/test/kotlin/org/sirius/dorm/graphql/AbstractTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.sirius.dorm.graphql
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

import org.sirius.common.tracer.TraceLevel
import org.sirius.common.tracer.Tracer
import org.sirius.common.tracer.trace.ConsoleTrace

import org.sirius.dorm.DORMConfiguration
import org.sirius.dorm.ObjectManager

import org.sirius.dorm.model.ObjectDescriptor
import org.sirius.dorm.session.DummySessionContextProvider
import org.sirius.dorm.session.SessionContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.stereotype.Component

@Component
class DummySessionContext : SessionContext(DummySessionContextProvider("me")) {}

@Configuration()
@Import(DORMConfiguration::class)
class GraphQLTestConfiguration {
}

@SpringBootTest(classes =[GraphQLTestConfiguration::class])
abstract class AbstractTest {
@Autowired
lateinit var objectManager : ObjectManager

protected var personDescriptor : ObjectDescriptor? = null

init {
Tracer(ConsoleTrace(), "%t{yyyy-MM-dd HH:mm:ss,SSS} %l{-10s} [%p] %m")
.setTraceLevel("", TraceLevel.OFF)
.setTraceLevel("com", TraceLevel.LOW)
.setTraceLevel("com.sirius.dorm", TraceLevel.HIGH)
}

protected fun createPerson(name: String, age: Int) : Long {
objectManager.begin()
try {
val person = objectManager.create(personDescriptor!!)

person["name"] = name
person["age"] = age

return person.id
}
finally {
objectManager.commit()
}
}

protected fun withTransaction(doIt: () -> Unit) {
objectManager.begin()
var committed = false
try {
doIt()

committed = true
objectManager.commit()
}
catch (throwable: Throwable) {
if ( !committed )
objectManager.rollback()

throw throwable
}
}

protected fun measure(test: String, n: Int, doIt: () -> Unit) {
println("> $test")
val start = System.currentTimeMillis()

var committed = false
//objectManager.begin()
try {
doIt()

committed = true
//objectManager.commit()
}
catch (throwable: Throwable) {
//if ( !committed )
// objectManager.rollback()

throw throwable
}
finally {
val ms = System.currentTimeMillis() - start
val avg = ms.toFloat() / n

println("< ${ms}ms, avg: ${avg}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.sirius.dorm.graphql
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

import graphql.GraphQL
import jakarta.annotation.PostConstruct
import org.junit.jupiter.api.RepeatedTest
import org.sirius.dorm.DORMConfiguration
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.junit.jupiter.api.Test
import org.sirius.dorm.ObjectManager
import org.sirius.dorm.graphql.test.TestData

@Configuration()
@Import(DORMConfiguration::class)
class TestConfiguration {
@Autowired
lateinit var objectManager : ObjectManager
@PostConstruct
fun createData() {
TestData(objectManager)
}
}

@SpringBootTest(classes =[TestConfiguration::class])
class GraphQLTest : AbstractTest() {
@Autowired
lateinit var graphQL : GraphQL

@RepeatedTest(3)
fun test() {
measure("simple read", 2000) {
for ( i in 0..2000) {
val result = graphQL.execute(
"query sampleQuery {\n" +
" Human(filter: { id: { eq: 1 } }) {\n" +
" versionCounter\n" +
" name\n" +
" father {\n" +
" name\n" +
" }\n" +
" id\n" +
" name\n" +
" }\n" +
"}"
)
}
}
}
}
91 changes: 91 additions & 0 deletions dorm/graphql/src/test/kotlin/org/sirius/dorm/graphql/TestData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.sirius.dorm.graphql.test
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

import jakarta.annotation.PostConstruct
import org.sirius.common.type.base.*
import org.sirius.dorm.ObjectManager
import org.sirius.dorm.model.Multiplicity
import org.sirius.dorm.model.attribute
import org.sirius.dorm.model.relation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component


class TestData(val objectManager: ObjectManager) {
init {
setupData()
}

// private

private fun <T> withTransaction(doIt: () -> T) : T {
objectManager.begin()
var committed = false

try {
val result = doIt()

committed = true
objectManager.commit()

return result
}
catch (throwable: Throwable) {
if ( !committed )
objectManager.rollback()

throw throwable
}
}

fun setupData() {
withTransaction {
// create type

objectManager.type("Human")
.add(attribute("firstName").type(string()))
.add(attribute("name").type(string()))
.add(attribute("age").type(int()))

.add(attribute("short").type(short()))
.add(attribute("long").type(long()))
.add(attribute("float").type(float()))
.add(attribute("double").type(double()))

// relations

.add(relation("father").target("Human").multiplicity(Multiplicity.ZERO_OR_ONE).inverse("children"))
.add(relation("children").target("Human").multiplicity(Multiplicity.ZERO_OR_MANY).inverse("father").owner())

// done

.register()

// create data

val personDescriptor = objectManager.getDescriptor("Human")

val andi = objectManager.create(personDescriptor)

andi["firstName"] = "Andi"
andi["name"] = "Ernst"
andi["age"] = 58

// child

val child = objectManager.create(personDescriptor)

child["firstName"] = "Nika"
child["name"] = "Martinez"
child["age"] = 14

// link

child["father"] = andi
}
}
}
38 changes: 38 additions & 0 deletions dorm/graphql/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
spring:
application:
name: dorm
h2:
console:
enabled: true
sql:
init:
mode: always
datasource:
url: jdbc:h2:mem:mydb
username: sa
password: password
driverClassName: org.h2.Driver
jpa:
properties:
hibernate:
format_sql: false
generate_statistics: false
show-sql: false
defer-datasource-initialization: true
hibernate:
ddl-auto: update
naming:
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
strategy: org.hibernate.cfg.ImprovedNamingStrategy
logging:
level:
org:
xhibernate:
sql: debug
orm:
jdbc:
bind: TRACE
type:
descriptor:
sql: trace

0 comments on commit fc10bf4

Please sign in to comment.