Skip to content

Add example stress query #583

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

Merged
merged 5 commits into from
Feb 3, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.examples.directives

import com.expediagroup.graphql.annotations.GraphQLDirective

/**
* Used to verify the performance overhead of instrumentation on fields.
* Marker directive only, does not have DirectiveWiring.
*/
@GraphQLDirective(
name = TRACK_TIMES_INVOKED_DIRECTIVE_NAME,
description = "If the field is marked with this directive, " +
"we keep track of how many times this field was invoked per exection " +
"and log the result server side through graphql-java Instrumentation"
)
annotation class TrackTimesInvoked

const val TRACK_TIMES_INVOKED_DIRECTIVE_NAME = "trackTimesInvoked"
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2020 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.examples.instrumentation

import com.expediagroup.graphql.examples.directives.TRACK_TIMES_INVOKED_DIRECTIVE_NAME
import graphql.ExecutionResult
import graphql.execution.instrumentation.InstrumentationContext
import graphql.execution.instrumentation.InstrumentationState
import graphql.execution.instrumentation.SimpleInstrumentation
import graphql.execution.instrumentation.SimpleInstrumentationContext
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap

/**
* Adds field count tracking in the instrumentation layer if [com.expediagroup.graphql.examples.directives.TrackTimesInvoked] is present.
*/
@Component
class TrackTimesInvokedInstrumentation : SimpleInstrumentation() {

private val logger = LoggerFactory.getLogger(TrackTimesInvokedInstrumentation::class.java)

override fun createState(): InstrumentationState = TrackTimesInvokedInstrumenationState()

override fun beginFieldFetch(parameters: InstrumentationFieldFetchParameters): InstrumentationContext<Any> {
if (parameters.field.getDirective(TRACK_TIMES_INVOKED_DIRECTIVE_NAME) != null) {
(parameters.getInstrumentationState() as? TrackTimesInvokedInstrumenationState)?.incrementCount(parameters.field.name)
}

return SimpleInstrumentationContext<Any>()
}

override fun instrumentExecutionResult(executionResult: ExecutionResult, parameters: InstrumentationExecutionParameters): CompletableFuture<ExecutionResult> {
val count = (parameters.getInstrumentationState() as? TrackTimesInvokedInstrumenationState)?.getCount()
logger.info("TrackTimesInvokedInstrumentation fields invoked: $count")
return super.instrumentExecutionResult(executionResult, parameters)
}

/**
* The state per execution for this Instrumentation
*/
private class TrackTimesInvokedInstrumenationState : InstrumentationState {

private val fieldCount = ConcurrentHashMap<String, Int>()

fun incrementCount(fieldName: String) {
val currentCount = fieldCount.getOrDefault(fieldName, 0)
fieldCount[fieldName] = currentCount.plus(1)
}

fun getCount() = fieldCount.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("unused")

package com.expediagroup.graphql.examples.query

import com.expediagroup.graphql.examples.directives.TrackTimesInvoked
import com.expediagroup.graphql.spring.operations.Query
import io.netty.util.internal.ThreadLocalRandom
import org.springframework.stereotype.Component
import java.util.UUID

/**
* Used to stress test the performance of running many data fetchers.
* Tests properties vs functions vs suspend functions.
*/
@Component
class StressQuery : Query {

fun stressNode(traceId: String?, count: Int?): List<StressNode> {
val id = traceId ?: getRandomStringFromThread()
return (1..(count ?: 1)).map { StressNode(id) }
}
}

@Suppress("MemberVisibilityCanBePrivate", "RedundantSuspendModifier")
class StressNode(val traceId: String) {

val valueId: String = getRandomStringFromThread()

fun functionId(): String = getRandomStringFromThread()

suspend fun suspendId(): String = getRandomStringFromThread()

@TrackTimesInvoked
fun loggingFunctionId(): String = getRandomStringFromThread()

@TrackTimesInvoked
suspend fun suspendLoggingFunctionId(): String = getRandomStringFromThread()
}

private fun getRandomStringFromThread(): String {
val random = ThreadLocalRandom.current()
return UUID(random.nextLong(), random.nextLong()).toString()
}