Aurélien Pupier created CAMEL-28075:
---------------------------------------

             Summary: Use singleton test-infra services in tests to reduce 
container start-ups
                 Key: CAMEL-28075
                 URL: https://issues.apache.org/jira/browse/CAMEL-28075
             Project: Camel
          Issue Type: Task
          Components: tests, test-infra
            Reporter: Aurélien Pupier


Many modules start a new container (or embedded server) for every test class by 
using {{XxxServiceFactory.createService()}}, while test-infra offers a 
singleton variant ({{createSingletonService()}} and friends, based on 
{{org.apache.camel.test.infra.common.services.SingletonService}}) that starts 
the service once per JVM / launcher execution and stops it with a shutdown 
hook. Using singleton services reduces the number of container starts, hence 
the duration and the load of the CI, which is the goal of CAMEL-24290.

h2. Analysis
Done by searching the test sources (outside {{test-infra}}) for the test-infra 
service factories and comparing with the usage of {{createSingleton*}} methods; 
container start times come from the Testcontainers log lines {{Container 
<image> started in PT...}} found in the {{target/*-test.log}} of 391 local runs 
(one machine, so values are indicative). The number of starts is the number of 
concrete test classes that declare the service, directly or through a base 
class: each one starts its own container. The estimated saving is (starts - 1) 
x average start time.

h3. Modules that start a container per test class and do not use a singleton 
(priority order)
||#||Module||Starts||Avg start||Est. saving||Remark||
|1|camel-kafka (only the KafkaHealthCheckTestSupport hierarchy)|5|18.6s (up to 
26s)|95-125s (see subtask: less if some classes need a fresh broker)|measured|
|2|camel-hivemq|10|n/a (about 8s estimated)|about 70s|ITs take 10-14s each, one 
46s|
|3|camel-zookeeper|13|about 4s (14.5s once)|about 50s|measured|
|4|camel-aws2-secrets-manager|11|3.7s|about 37s|measured|
|5|camel-aws2-lambda|9|4.1s|about 33s|measured|
|6|camel-apicurio-registry|4|n/a (about 10s estimated)|about 30s|no singleton 
factory yet|
|7|camel-aws2-sns|6|4.0s|about 20s|measured|
|8|camel-aws2-kms|6|3.8s|about 19s|measured|
|9|camel-aws2-iam|6|3.6s|about 18s|measured|
|10|camel-aws-parameter-store|6|3.1s|about 15s|measured|
|11|camel-activemq|3|8.7s|about 17s|custom builder, no singleton factory|
|12|camel-activemq6|3|6.8s|about 14s|custom builder, no singleton factory|
|13|camel-aws2-sts|3|5.1s|about 10s|measured|
|14|camel-pgevent|2|11.2s|about 11s|custom builder, no singleton factory|
|15|camel-azure-servicebus|3|about 4s|about 8s|a first start failed after about 
2 minutes and was retried in one local log: the real gain may be higher|
|16|camel-aws2-transcribe|2|about 4s|about 4s|singleton factory method already 
exists|

h3. Modules where a singleton brings nothing (or nothing measurable)
* only one test class starts the service: camel-aws2-cw, camel-aws2-ec2, 
camel-aws-config, camel-azure-eventgrid, camel-azure-functions, 
camel-azure-key-vault, camel-google-secret-manager, camel-google-storage, 
camel-langchain4j-ingest, camel-cxf-spring-soap, camel-mllp;
* embedded services that do not start a container: camel-ignite, camel-ftp, and 
the Artemis VM services of camel-sjms (14 classes), camel-sjms2 (12), 
camel-amqp, camel-mllp, camel-cxf-spring-soap (the gain is the broker/grid 
start time, which was not measured);
* modules that already use singletons for most tests but keep a non-singleton 
service on purpose or for a specific scenario: camel-opa 
({{OpaBearerTokenIT}}), camel-smb ({{SmbServerGoneIT}}), the non-singleton 
usages in camel-amqp/camel-sjms/camel-sjms2.

h2. Stability is more important than speed
h3. Stability first
Moving a test class to a singleton service must *not* make the build less 
stable. Before switching a class:
* check that the test does not require a fresh container: it must not stop, 
shut down, pause, restart or reconfigure the service (or its container) and 
must not depend on an empty initial state;
* make the data it creates unique per test (queue/topic/bucket/secret/parameter 
names, consumer groups, ...) so that tests sharing the same instance, in the 
same JVM, do not interfere, including when test classes run concurrently;
* leave tests that really need a fresh instance on {{createService()}} (and 
document why in a comment);
* run the module several times (for example with 
{{-Dcamel.failsafe.forkCount=N}} and, where supported, 
{{-Dcamel.surefire.parallel=true}}) and compare the duration and the failure 
rate with the current ones before opening the PR.

h2. Plan
One sub-task per module, in the priority order of the table above (high for the 
first 3, low for the others). When a module needs a singleton factory method 
that does not exist yet in {{test-infra}} (the AWS services other than 
S3/SQS/DynamoDB/EventBridge/Kinesis/Transcribe, {{MessagingServiceFactory}}, 
{{ApicurioRegistryServiceFactory}}, {{JDBCServiceFactory}}), add it in the 
sub-task of the module, following {{RedisServiceFactory}} / 
{{SingletonService}}.


_AI-generated by IBM Bob on behalf of apupier._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to