-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add query time measurements to sparql client (#1670)
* Record execution times for new sparql client
- Loading branch information
Showing
20 changed files
with
248 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
graph-commons/src/main/scala/io/renku/triplesstore/ProjectSparqlClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2023 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* 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 | ||
* | ||
* http://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 io.renku.triplesstore | ||
|
||
import cats.Monad | ||
import cats.effect._ | ||
import cats.syntax.all._ | ||
import eu.timepit.refined.api.Refined | ||
import eu.timepit.refined.collection.NonEmpty | ||
import eu.timepit.refined.auto._ | ||
import fs2.io.net.Network | ||
import io.renku.jsonld.JsonLD | ||
import io.renku.triplesstore.client.http.{Retry, SparqlClient, SparqlQuery, SparqlUpdate} | ||
import org.typelevel.log4cats.Logger | ||
|
||
/** SparQL client fixed to the `projects` dataset. */ | ||
trait ProjectSparqlClient[F[_]] extends SparqlClient[F] | ||
|
||
object ProjectSparqlClient { | ||
def apply[F[_]: Monad: Logger: SparqlQueryTimeRecorder](c: SparqlClient[F]) = | ||
new ProjectSparqlClient[F] { | ||
private[this] val rec = SparqlQueryTimeRecorder[F].instance | ||
override def update(request: SparqlUpdate) = { | ||
val label = histogramLabel(request) | ||
val work = c.update(request) | ||
rec | ||
.measureExecutionTime(work, label) | ||
.flatMap(rec.logExecutionTime(s"Execute sparql update '$label'")) | ||
} | ||
|
||
override def upload(data: JsonLD) = { | ||
val label: String Refined NonEmpty = "jsonld upload" | ||
val work = c.upload(data) | ||
rec | ||
.measureExecutionTime(work, label.some) | ||
.flatMap(rec.logExecutionTime("Execute JSONLD upload")) | ||
} | ||
|
||
override def query(request: SparqlQuery) = { | ||
val label = histogramLabel(request) | ||
val work = c.query(request) | ||
rec | ||
.measureExecutionTime(work, label) | ||
.flatMap(rec.logExecutionTime(s"Execute sparql query '$label'")) | ||
} | ||
} | ||
|
||
def apply[F[_]: Network: Async: Logger: SparqlQueryTimeRecorder]( | ||
cc: ProjectsConnectionConfig, | ||
retryCfg: Retry.RetryConfig = Retry.RetryConfig.default | ||
): Resource[F, ProjectSparqlClient[F]] = { | ||
val cfg = cc.toCC(Some(retryCfg)) | ||
SparqlClient[F](cfg).map(apply(_)) | ||
} | ||
|
||
private def histogramLabel(r: Any): Option[String Refined NonEmpty] = | ||
r match { | ||
case q: io.renku.triplesstore.SparqlQuery => q.name.some | ||
case _ => None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
graph-commons/src/test/scala/io/renku/triplesstore/ProjectSparqlClientSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2023 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* 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 | ||
* | ||
* http://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 io.renku.triplesstore | ||
|
||
import cats.effect.IO | ||
import cats.effect.testing.scalatest.AsyncIOSpec | ||
import eu.timepit.refined.auto._ | ||
import io.prometheus.client.Histogram | ||
import io.renku.cli.model.CliSoftwareAgent | ||
import io.renku.graph.model.agents | ||
import io.renku.interpreters.TestLogger | ||
import io.renku.jsonld.syntax._ | ||
import io.renku.logging.TestExecutionTimeRecorder | ||
import io.renku.triplesstore.client.syntax._ | ||
import io.renku.triplesstore.client.util.JenaContainerSupport | ||
import org.scalatest.flatspec.AsyncFlatSpec | ||
import org.scalatest.matchers.should | ||
import org.typelevel.log4cats.Logger | ||
|
||
class ProjectSparqlClientSpec extends AsyncFlatSpec with AsyncIOSpec with JenaContainerSupport with should.Matchers { | ||
implicit val logger: Logger[IO] = TestLogger() | ||
|
||
val makeHistogram = IO( | ||
new Histogram.Builder().name("test").help("test").labelNames("update").buckets(0.5, 0.8).create() | ||
) | ||
|
||
def makeSparqlQueryTimeRecorder(h: Histogram): SparqlQueryTimeRecorder[IO] = | ||
new SparqlQueryTimeRecorder[IO](TestExecutionTimeRecorder[IO](Some(h))) | ||
|
||
def withProjectClient(implicit sqr: SparqlQueryTimeRecorder[IO]) = | ||
withDataset("projects").map(ProjectSparqlClient.apply(_)) | ||
|
||
def assertSampled(histogram: Histogram) = | ||
histogram.collect().get(0).samples.size should be > 0 | ||
|
||
def assertNotSampled(histogram: Histogram) = | ||
histogram.collect().get(0).samples.size shouldBe 0 | ||
|
||
def resetHistogram(histogram: Histogram) = { | ||
histogram.clear() | ||
assertNotSampled(histogram) | ||
} | ||
|
||
it should "measure execution time for named queries" in { | ||
val histogram = makeHistogram.unsafeRunSync() | ||
implicit val sr: SparqlQueryTimeRecorder[IO] = makeSparqlQueryTimeRecorder(histogram) | ||
withProjectClient.use { c => | ||
for { | ||
_ <- IO(assertNotSampled(histogram)) | ||
up = SparqlQuery.apply( | ||
name = "test-update", | ||
prefixes = Set.empty, | ||
body = sparql""" | ||
|PREFIX p: <http://schema.org/> | ||
|INSERT DATA { | ||
| p:fred p:hasSpouse p:wilma . | ||
| p:fred p:hasChild p:pebbles . | ||
| p:wilma p:hasChild p:pebbles . | ||
| p:pebbles p:hasSpouse p:bamm-bamm ; | ||
| p:hasChild p:roxy, p:chip. | ||
|}""".stripMargin | ||
) | ||
_ <- c.update(up) | ||
_ = assertSampled(histogram) | ||
|
||
_ <- IO(resetHistogram(histogram)) | ||
q = SparqlQuery( | ||
name = "test-query", | ||
prefixes = Set.empty, | ||
body = sparql"SELECT * WHERE { ?s ?p ?o } LIMIT 1" | ||
) | ||
_ <- c.query(q) | ||
_ = assertSampled(histogram) | ||
|
||
_ <- IO(resetHistogram(histogram)) | ||
data = CliSoftwareAgent(agents.ResourceId("http://u.rl"), agents.Name("test")).asJsonLD | ||
_ <- c.upload(data) | ||
_ = assertSampled(histogram) | ||
} yield () | ||
} | ||
} | ||
|
||
it should "not measure execution time for un-named queries" in { | ||
val histogram = makeHistogram.unsafeRunSync() | ||
implicit val sr: SparqlQueryTimeRecorder[IO] = makeSparqlQueryTimeRecorder(histogram) | ||
|
||
withProjectClient.use { c => | ||
for { | ||
_ <- IO(assertNotSampled(histogram)) | ||
|
||
q = sparql""" | ||
|PREFIX p: <http://schema.org/> | ||
|INSERT DATA { | ||
| p:fred p:hasSpouse p:wilma . | ||
| p:fred p:hasChild p:pebbles . | ||
| p:wilma p:hasChild p:pebbles . | ||
| p:pebbles p:hasSpouse p:bamm-bamm ; | ||
| p:hasChild p:roxy, p:chip. | ||
|}""".stripMargin | ||
|
||
_ <- c.update(q) | ||
|
||
_ = assertNotSampled(histogram) | ||
} yield () | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.