File indexing completed on 2025-10-14 09:15:12
0001
0002 #include "JANA/JApplicationFwd.h"
0003 #include "JANA/JEventSource.h"
0004 #include "JANA/JLogger.h"
0005 #include <catch.hpp>
0006 #include <JANA/JApplication.h>
0007 #include <JANA/JFactory.h>
0008 #include <JANA/JEventProcessor.h>
0009 #include <chrono>
0010 #include <thread>
0011
0012
0013 namespace jana::engine::ordering_tests {
0014
0015 struct MyData {int x;};
0016
0017 class MyFac : public JFactory {
0018
0019 Output<MyData> m_data_out {this};
0020 int m_delays_s[4] = {2, 0, 5, 1};
0021
0022 public:
0023 MyFac() {
0024 SetPrefix("myfac");
0025 SetTypeName("MyFac");
0026 }
0027 void Process(const JEvent& event) override {
0028 auto event_nr = event.GetEventNumber();
0029 auto delay = m_delays_s[event_nr % 4];
0030
0031 LOG_DEBUG(GetLogger()) << "Delaying event " << event_nr << " with index " << event.GetEventIndex() << " for " << delay << " seconds";
0032 std::this_thread::sleep_for(std::chrono::seconds(delay));
0033 LOG_DEBUG(GetLogger()) << "Finished Delaying event " << event_nr;
0034 m_data_out().push_back(new MyData{.x=delay});
0035 }
0036 };
0037
0038 class MyProc : public JEventProcessor {
0039
0040 Input<MyData> m_data_in {this};
0041 int last_event_nr = -1;
0042
0043 public:
0044 MyProc(bool enable_ordering=true) {
0045 SetPrefix("myproc");
0046 SetTypeName("MyProc");
0047 SetCallbackStyle(CallbackStyle::ExpertMode);
0048 EnableOrdering(enable_ordering);
0049 }
0050
0051 void ProcessSequential(const JEvent& event) override {
0052 auto evt_nr = event.GetEventNumber();
0053 LOG_DEBUG(GetLogger()) << "Processing event nr " << evt_nr << " with index " << event.GetEventIndex();
0054 if (IsOrderingEnabled()) {
0055 REQUIRE(evt_nr == (size_t) last_event_nr+1);
0056 last_event_nr = evt_nr;
0057 }
0058 }
0059 };
0060
0061 TEST_CASE("OrderingTests") {
0062 JApplication app;
0063 app.Add(new JEventSource);
0064 app.Add(new MyProc);
0065 app.Add(new JFactoryGeneratorT<MyFac>);
0066 app.SetParameterValue("jana:nevents", 20);
0067 app.SetParameterValue("myfac:loglevel", "debug");
0068 app.SetParameterValue("myproc:loglevel", "debug");
0069 app.SetParameterValue("nthreads", "4");
0070 app.Run();
0071 }
0072
0073 TEST_CASE("OrderingTests_SequentialBaseline") {
0074 JApplication app;
0075 app.Add(new JEventSource);
0076 app.Add(new MyProc);
0077 app.Add(new JFactoryGeneratorT<MyFac>);
0078 app.SetParameterValue("jana:nevents", 20);
0079 app.SetParameterValue("myfac:loglevel", "debug");
0080 app.SetParameterValue("myproc:loglevel", "debug");
0081 app.SetParameterValue("nthreads", "1");
0082 app.Run();
0083 }
0084
0085 TEST_CASE("OrderingTests_FullyParallel") {
0086 JApplication app;
0087 app.Add(new JEventSource);
0088 app.Add(new MyProc{false});
0089 app.Add(new JFactoryGeneratorT<MyFac>);
0090 app.SetParameterValue("jana:nevents", 20);
0091 app.SetParameterValue("myfac:loglevel", "debug");
0092 app.SetParameterValue("myproc:loglevel", "debug");
0093 app.SetParameterValue("nthreads", "4");
0094 app.Run();
0095 }
0096
0097 }