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

io.hyperfoil.clustering.util.PersistedBenchmarkData Maven / Gradle / Ivy

There is a newer version: 0.27
Show newest version
package io.hyperfoil.clustering.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Collectors;

import io.hyperfoil.api.config.BenchmarkData;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

public class PersistedBenchmarkData implements BenchmarkData {
   private static final Logger log = LoggerFactory.getLogger(PersistedBenchmarkData.class);
   private final Path dir;

   public PersistedBenchmarkData(Path dir) {
      this.dir = dir;
   }

   @Override
   public InputStream readFile(String file) {
      try {
         return new FileInputStream(dir.resolve(sanitize(file)).toFile());
      } catch (FileNotFoundException e) {
         log.error("Cannot load file {} from directory {}", e, file, dir);
         return null;
      }
   }

   @Override
   public Map files() {
      try {
         return Files.list(dir).collect(Collectors.toMap(path -> path.getFileName().toString(), (Path path) -> {
            try {
               return Files.readAllBytes(path);
            } catch (IOException e) {
               log.error("Cannot read file {}", e, path);
               throw new RuntimeException(e);
            }
         }));
      } catch (IOException e) {
         throw new RuntimeException("Cannot list directory " + dir, e);
      }
   }

   static String sanitize(String file) {
      return file.replace(File.separatorChar, '_').replace(File.pathSeparatorChar, '_');
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy