Excluding setup methods from profiler to measure memory more accurately

25 Views Asked by At

I'm currently trying to measure memory consumption in JMH using a custom profiler (the same problem arrise when using the gc profiler) like this:

class ViatraMemoryProfiler extends InternalProfiler{
  var memoryBefore: Long = 0
  override def getDescription = "Max memory heap profiler for Viatra"

  override def beforeIteration(benchmarkParams: BenchmarkParams, iterationParams: IterationParams): Unit = {
    memoryBefore = Runtime.getRuntime.totalMemory - Runtime.getRuntime.freeMemory
    println("profiler before iteration")
  }


  override def afterIteration(benchmarkParams: BenchmarkParams, iterationParams: IterationParams, result: IterationResult): util.Collection[ScalarResult] = {
    val memoryAfter = Runtime.getRuntime.totalMemory - Runtime.getRuntime.freeMemory
    val diff = memoryAfter - memoryBefore

    var results = List[ScalarResult]()
    results = results.appended(new ScalarResult("Max memory heap", diff.toDouble, "bytes", AggregationPolicy.MAX))
    import scala.jdk.CollectionConverters.*
    results.asJava
  }
}

(I'm trying to measure Viatras performance and memory: https://wiki.eclipse.org/VIATRA/Query/FAQ#Does_VIATRA_Query_need_to_load_all_the_models_into_memory.2C_or_only_the_necessary_ones_like_EMF_Model_Query_2.3F).

But since I'm allocating a lot of memory in my @setup methods and those also get measured, the results are'nt really usefull to me.

So my question is: how can I attach the profiler only after the setup method has run?

Here someone asked a similar question, but only got the answer to stop using jmh for this: Exclude @Setup methods from profiling in JMH

Is there any other way to measure the memory consumption without the setup methods?

I tried to run jmh with my custom profiler to see if afterIteration is called after @setup methods, but it sadly isn't and I could not find a solution to this problem.

0

There are 0 best solutions below