Skip to content

Commit

Permalink
v0.11.6 release
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerbrandl committed Oct 19, 2023
1 parent 8d6dcf8 commit 173d962
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {

group = "com.github.holgerbrandl"
//version = "0.8.101"
version = "0.11.5"
version = "0.11.6"
//version = "0.12-SNAPSHOT"


Expand Down
2 changes: 1 addition & 1 deletion docs/userguide/docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
To get started simply add it as a dependency:
```
dependencies {
implementation "com.github.holgerbrandl:kalasim:0.11.5"
implementation "com.github.holgerbrandl:kalasim:0.11.6"
}
```

Expand Down
27 changes: 16 additions & 11 deletions src/main/kotlin/org/kalasim/monitors/MetricTimeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ operator fun <V : Number> MetricTimeline<V>.div(other: MetricTimeline<V>) =
combineInternal(this, other, ArithmeticOp.Div)

operator fun <V : Number> MetricTimeline<V>.div(factor: Double): MetricTimeline<Double> {
val constMT = MetricTimeline("Constant $factor", initialValue = factor.toDouble()).apply {
timestamps.apply { clear(); add(timestamps.first()) }
val constMT = MetricTimeline("Constant $factor", initialValue = factor)
.apply {
timestamps.apply { clear(); add(this@div.timestamps.first()) }
}
return combineInternal(this.asDoubleTimeline(), constMT, ArithmeticOp.Div)
}
Expand All @@ -218,7 +219,7 @@ private fun <V : Number> combineInternal(

val minTime = maxOf(mt.timestamps.first(), other.timestamps.first())
// val maxTime = Math.min(mt.timestamps.last(), other.timestamps.last())
val maxTime = mt.now.value
val maxTime = maxOf(mt.timestamps.last(), other.timestamps.last())
// val timeRange = minTime..maxTime

val merged = MetricTimeline(
Expand All @@ -230,14 +231,18 @@ private fun <V : Number> combineInternal(
values.clear()
}

joinTime.dropWhile { it < minTime }.takeWhile { it <= maxTime }.forEach { time ->
merged.timestamps.add(time)
val value = when(mode) {
ArithmeticOp.Plus -> mt[time].toDouble() + other[time].toDouble()
ArithmeticOp.Minus -> mt[time].toDouble() - other[time].toDouble()
ArithmeticOp.Times -> mt[time].toDouble() * other[time].toDouble()
ArithmeticOp.Div -> mt[time].toDouble() / other[time].toDouble()
}
joinTime
.dropWhile { it < minTime }
.takeWhile { it <= maxTime }
.forEach { time ->
merged.timestamps.add(time)
val value = when(mode) {
ArithmeticOp.Plus -> mt[time].toDouble() + other[time].toDouble()
ArithmeticOp.Minus -> mt[time].toDouble() - other[time].toDouble()
ArithmeticOp.Times -> mt[time].toDouble() * other[time].toDouble()
ArithmeticOp.Div -> mt[time].toDouble() / other[time].toDouble()
}

merged.values.add(value)
}

Expand Down
30 changes: 27 additions & 3 deletions src/test/kotlin/org/kalasim/test/MonitorTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import io.kotest.matchers.shouldBe
import org.apache.commons.math3.distribution.EnumeratedDistribution
import org.apache.commons.math3.stat.descriptive.StatisticalSummaryValues
import org.junit.Test
import org.kalasim.misc.*
import org.kalasim.misc.merge
import org.kalasim.misc.mergeStats
import org.kalasim.monitors.*
import org.kalasim.test.MonitorTests.Car.*
import org.kalasim.tt

@OptIn(AmbiguousDuration::class)
class MonitorTests {

private enum class Car {
Expand Down Expand Up @@ -204,10 +206,11 @@ class MonitorTests {


/* Test the monitors can be merged. */
@OptIn(org.kalasim.misc.AmbiguousDuration::class)
class MergeTimelineTests {

@Test
fun `it should metric timelines`() = createTestSimulation {
fun `it should support algebra of metric timelines`() = createTestSimulation {
val mtA = IntTimeline()
val mtB = IntTimeline()

Expand All @@ -220,11 +223,21 @@ class MergeTimelineTests {
run(until = 12.tt)
mtB.addValue(5)

val mtC = IntTimeline(initialValue = 5)


run(until = 14.tt)
mtA.addValue(10)
mtC.addValue(12)

//merge statistics
// test scalar operations
(mtA / 2.0).apply {
get(0) shouldBe 0.0
get(6) shouldBe 11.5
get(16) shouldBe 5
}

//merge statistics
val addedTL = mtA + mtB

println(addedTL.timestamps)
Expand All @@ -233,11 +246,22 @@ class MergeTimelineTests {
addedTL.values shouldBe listOf(0.0, 23.0, 26.0, 28.0, 15.0)
addedTL.timestamps shouldBe listOf(0.0, 5.0, 10.0, 12.0, 14.0).map { it.tt }

// just make sure that the other ops do not error do not error
// make sure that the other ops do not error do not error
println(mtA + mtB)
println(mtA - mtB)
println(mtA * mtB)
(mtA * mtB)[11] shouldBe 69
println(mtA / mtB)

listOf(mtA, mtB).sum()[11] shouldBe 26.0
listOf(mtA, mtB).mean()[11] shouldBe 13.0


// also validate that when merging with c the later init is accounted for
val mtAC = (mtA+mtC)
mtAC[13] shouldBe (mtA[13]+mtC[13])
mtAC[100] shouldBe 22
shouldThrow<IllegalArgumentException>{mtAC[10]}
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/test/kotlin/org/kalasim/test/ResourceTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import org.kalasim.Priority.Companion.LOWEST
import org.kalasim.Priority.Companion.NORMAL
import org.kalasim.ResourceSelectionPolicy.*
import kotlinx.datetime.Instant
import org.kalasim.misc.TestUtil
import org.kalasim.misc.asCMPairList
import org.kalasim.misc.*
import kotlin.repeat
import kotlin.time.Duration.Companion.minutes

class ResourceTests {
Expand Down Expand Up @@ -409,6 +409,7 @@ class ResourceTests {
run(1)
}

@OptIn(AmbiguousDuration::class)
@Test
fun `it should track request scoped activities`() = createTestSimulation {
val r1 = Resource(capacity = 4)
Expand Down Expand Up @@ -455,7 +456,7 @@ class ResourceTests {

// We should make sure that only actual changes are tracked (e.g. not same capacity value twice
timeline.filter { it.metric == ResourceMetric.Capacity }.size shouldBe 2
timeline.size shouldBe 28
timeline.size shouldBe 26

// now set the tick-transform and check if the timeline includes walltime
startDate =Instant.parse("2021-01-01T00:00:00.00Z")
Expand Down

0 comments on commit 173d962

Please sign in to comment.