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

io.gatling.core.body.RawFileBodies.scala Maven / Gradle / Ivy

There is a newer version: 3.13.1
Show newest version
/**
 * Copyright 2011-2016 GatlingCorp (http://gatling.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  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 io.gatling.core.body

import java.io.File

import io.gatling.commons.util.Io._
import io.gatling.commons.validation._
import io.gatling.core.config.GatlingConfiguration
import io.gatling.core.session.Expression
import io.gatling.core.util.Resource
import io.gatling.core.util.cache.SelfLoadingThreadSafeCache

case class FileWithCachedBytes(file: File, cachedBytes: Option[Array[Byte]]) {
  def bytes: Array[Byte] = cachedBytes.getOrElse(file.toByteArray())
}

class RawFileBodies(implicit configuration: GatlingConfiguration) {

  private val pathToFile: String => Validation[File] = path => Resource.body(path).map(_.file)
  private val pathToFileBytes: String => Validation[Array[Byte]] = path => Resource.body(path).map(_.file.toByteArray())

  private val rawFileBodyCache =
    SelfLoadingThreadSafeCache[String, Validation[File]](configuration.core.rawFileBodiesCacheMaxCapacity, pathToFile)

  private val rawFileBodyBytesCache =
    SelfLoadingThreadSafeCache[String, Validation[Array[Byte]]](configuration.core.rawFileBodiesCacheMaxCapacity, pathToFileBytes)

  private def cachedBytes(file: File): Validation[Option[Array[Byte]]] =
    if (file.length > configuration.core.rawFileBodiesInMemoryMaxSize)
      Success(None)
    else
      rawFileBodyBytesCache.get(file.getPath).map(Some(_))

  def asFileWithCachedBytes(filePath: Expression[String]): Expression[FileWithCachedBytes] =
    session =>
      for {
        path <- filePath(session)
        file <- rawFileBodyCache.get(path)
        validatedFile <- file.validateExistingReadable
        cachedBytes <- cachedBytes(validatedFile)
      } yield FileWithCachedBytes(validatedFile, cachedBytes)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy