Skip to content

Commit

Permalink
[DROOLS-7539] FireAndAlarmBenchmark (#256)
Browse files Browse the repository at this point in the history
* FireAndAlarmBenchmark

* FIRE_AND_ALARM rules added

* move useObjectStoreWithReferenceParam
  • Loading branch information
nprentza authored Sep 5, 2023
1 parent bbf43bc commit d10d533
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.drools.benchmarks.reliability;

import org.drools.benchmarks.common.DRLProvider;
import org.drools.benchmarks.common.providers.RulesWithJoinsProvider;
import org.drools.benchmarks.common.util.BuildtimeUtil;
import org.drools.benchmarks.common.util.RuntimeUtil;
import org.drools.benchmarks.reliability.fireandalarm.Alarm;
import org.drools.benchmarks.reliability.fireandalarm.Fire;
import org.drools.benchmarks.reliability.fireandalarm.Room;
import org.drools.benchmarks.reliability.fireandalarm.Sprinkler;
import org.drools.kiesession.session.StatefulKnowledgeSessionImpl;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.runtime.conf.PersistedSessionOption;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.conf.ParallelExecutionOption;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.Warmup;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.benchmarks.reliability.AbstractReliabilityBenchmark.Mode.NONE;

@Warmup(iterations = 2000)
@Measurement(iterations = 1000)
public class FireAndAlarmBenchmark extends AbstractReliabilityBenchmark{

private static final String FIRE_AND_ALARM =
"import " + Alarm.class.getCanonicalName() + ";" +
"import " + Fire.class.getCanonicalName() + ";" +
"import " + Sprinkler.class.getCanonicalName() + ";" +
"import " + Room.class.getCanonicalName() + ";" +
"rule 'When there is a fire turn on the sprinkler' when\n" +
" Fire($room : room) \n" +
" $sprinkler: Sprinkler( room == $room, on == false ) \n" +
"then\n" +
" modify($sprinkler) { setOn(true); } \n" +
"end\n" +
"rule 'Raise the alarm when we have one or more firs' when\n" +
" exists Fire() \n" +
"then\n" +
" insert( new Alarm() );\n" +
"end\n" +
"rule 'Cancel the alarm when all the fires have gone' when \n" +
" not Fire() \n" +
" $alarm : Alarm() \n" +
"then\n" +
" delete ( $alarm ); \n" +
"end\n" +
"rule 'Status output when things are ok' when\n" +
" not Alarm() \n" +
" not Sprinkler ( on == true ) \n" +
"then \n" +
" System.out.println(\"Everything is ok\"); \n" +
"end";

@Param({"100"})
private int factsNr;

@Param({"EMBEDDED"})
private Mode mode;

@Param({"true", "false"})
private boolean useObjectStoreWithReferences;

@Param({"true", "false"})
private boolean useSafepoints;

@Setup
public void setupKieBase() {
final DRLProvider drlProvider = new RulesWithJoinsProvider(1, false, true);

kieBase = BuildtimeUtil.createKieBaseFromDrl(true, FIRE_AND_ALARM,
ParallelExecutionOption.SEQUENTIAL,
EventProcessingOption.CLOUD);
}

@Setup(Level.Iteration)
@Override
public void setup() {
if (mode != NONE) {
PersistedSessionOption option = PersistedSessionOption.newSession().withPersistenceStrategy(PersistedSessionOption.PersistenceStrategy.STORES_ONLY);
if (useSafepoints) {
option = option.withSafepointStrategy(PersistedSessionOption.SafepointStrategy.AFTER_FIRE);
}
if (useObjectStoreWithReferences){
option = option.withPersistenceObjectsStrategy(PersistedSessionOption.PersistenceObjectsStrategy.OBJECT_REFERENCES);
}
kieSession = RuntimeUtil.createKieSession(kieBase, option);
} else {
kieSession = RuntimeUtil.createKieSession(kieBase);
}

populateKieSessionPerIteration();
}
@Override
protected void populateKieSessionPerIteration() {
// no-op
}

@Benchmark
public int test() {

// phase 1
List<Room> rooms = new ArrayList<Room>();
List<FactHandle> fireFactHandles = new ArrayList<FactHandle>();
for (int i = 0; i < factsNr; i++) {
rooms.add(new Room("room_" + i));
kieSession.insert(rooms.get(i));
fireFactHandles.add(kieSession.insert(new Fire(rooms.get(i))));
}
kieSession.fireAllRules();

// phase 2
Sprinkler sprinkler;
for (int i = 0; i < factsNr; i++) {
sprinkler = new Sprinkler(rooms.get(i));
kieSession.insert(sprinkler);
}
kieSession.fireAllRules();

// phase 3
for (int i = 0; i < factsNr; i++) {
kieSession.delete(fireFactHandles.get(i));
}
return kieSession.fireAllRules();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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 org.drools.benchmarks.reliability.fireandalarm;

import java.io.Serializable;

public class Alarm implements Serializable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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 org.drools.benchmarks.reliability.fireandalarm;

import java.io.Serializable;

public class Fire implements Serializable {
private Room room;

public Fire() { }

public Fire(Room room) {
this.room = room;
}

public Room getRoom() {
return room;
}

public void setRoom(Room room) {
this.room = room;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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 org.drools.benchmarks.reliability.fireandalarm;

import java.io.Serializable;

public class Room implements Serializable {

private String name;

public Room() { }

public Room(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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 org.drools.benchmarks.reliability.fireandalarm;

import java.io.Serializable;

public class Sprinkler implements Serializable {
private Room room;
private boolean on = false;

public Sprinkler() { }

public Sprinkler(Room room) {
this.room = room;
}

public Room getRoom() {
return room;
}

public void setRoom(Room room) {
this.room = room;
}

public boolean isOn() {
return on;
}

public void setOn(boolean on) {
this.on = on;
}

@Override
public String toString() {
return "Sprinkler for " + room;
}
}

0 comments on commit d10d533

Please sign in to comment.