All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hyperfoil.core.parser.StartAfterParser Maven / Gradle / Ivy

There is a newer version: 0.27.1
Show newest version
package io.hyperfoil.core.parser;

import java.util.function.BiConsumer;

import org.yaml.snakeyaml.events.Event;
import org.yaml.snakeyaml.events.MappingStartEvent;
import org.yaml.snakeyaml.events.ScalarEvent;
import org.yaml.snakeyaml.events.SequenceStartEvent;

import io.hyperfoil.api.config.BenchmarkDefinitionException;
import io.hyperfoil.api.config.PhaseBuilder;
import io.hyperfoil.api.config.PhaseReference;
import io.hyperfoil.api.config.RelativeIteration;

class StartAfterParser implements Parser {
   private final BiConsumer consumer;

   StartAfterParser(BiConsumer consumer) {
      this.consumer = consumer;
   }

   @Override
   public void parse(Context ctx, PhaseBuilder target) throws ParserException {
      if (!ctx.hasNext()) {
         throw ctx.noMoreEvents(ScalarEvent.class, SequenceStartEvent.class, MappingStartEvent.class);
      }
      Event event = ctx.peek();
      if (event instanceof SequenceStartEvent) {
         ctx.parseList(target, this::parseItem);
         return;
      }
      parseItem(ctx, target);
   }

   private void parseItem(Context ctx, PhaseBuilder target) throws ParserException {
      Event event = ctx.peek();
      if (event instanceof ScalarEvent) {
         consumer.accept(target, new PhaseReference(((ScalarEvent) event).getValue(), RelativeIteration.NONE, null));
         ctx.consumePeeked(event);
      } else if (event instanceof MappingStartEvent) {
         PhaseReferenceBuilder ms = new PhaseReferenceBuilder();
         MappingParser.INSTANCE.parse(ctx, ms);
         if (ms.phase == null || ms.phase.isEmpty()) {
            throw new BenchmarkDefinitionException("Missing name in phase reference.");
         }
         consumer.accept(target, new PhaseReference(ms.phase, ms.iteration, null));
      } else {
         throw ctx.unexpectedEvent(event);
      }
   }

   private static class PhaseReferenceBuilder {
      String phase;
      RelativeIteration iteration = RelativeIteration.NONE;
      String fork;
   }

   private static class MappingParser extends AbstractMappingParser {
      static MappingParser INSTANCE = new MappingParser();

      MappingParser() {
         register("phase", new PropertyParser.String<>((ms, value) -> ms.phase = value));
         register("iteration", new PropertyParser.String<>((ms, value) -> ms.iteration = RelativeIteration.valueOf(value.toUpperCase())));
         register("fork", new PropertyParser.String<>((ms, value) -> ms.fork = value));
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy