packages feed

squeeze-1.0.3.0: src/Squeeze/Test/Performance.hs

{-
	Copyright (C) 2011 Dr. Alistair Ward

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Defines a performance-test, based on randomly generated /virtual/ files.
-}

module Squeeze.Test.Performance(
-- * Functions
	run
) where

import			Control.Arrow((&&&))
import qualified	Control.Monad
import qualified	Control.Monad.Writer
import qualified	Data.List
import qualified	Data.Maybe
import qualified	Factory.Math.Probability
import qualified	Squeeze.Data.CommandOptions	as Data.CommandOptions
import qualified	Squeeze.Data.File		as Data.File
import qualified	Squeeze.Data.FileCombination	as Data.FileCombination
import qualified	Squeeze.Squeeze			as Squeeze
import qualified	System.IO
import qualified	System.Random

{- |
	* Find the best-fit, into the available space, using a set composed from the specified number of /virtual/ files.

	* The files have a random size, conforming to the requested distribution.

	* The generated file-name, arbitrarily reflects its size.
-}
run :: (RealFrac ratio, RealFloat f, Show f, System.Random.Random f)
	=> Data.CommandOptions.CommandOptions ratio
	-> Int	-- ^ The number of /virtual/ files to randomly generate.
	-> Factory.Math.Probability.DiscreteDistribution f
	-> IO [Data.FileCombination.FileCombination]
run commandOptions fileCount probabilityDistribution	= do
	randomGen	<- Data.Maybe.maybe System.Random.getStdGen {-use the global random-number generator-} (
		return {-to IO-monad-} . System.Random.mkStdGen	--Seed the random-number generator as specified.
	 ) $ Data.CommandOptions.getMaybeRandomSeed commandOptions	--Select a random-number generator.

	let (acceptedFileSizeAndPathList, logFile)	= Control.Monad.Writer.runWriter . Data.File.selectSuitableFileSizes (Data.CommandOptions.isWithinSizeBounds commandOptions) . Data.List.nub . map (id &&& show) $ Factory.Math.Probability.generateDiscretePopulation fileCount probabilityDistribution randomGen

	Control.Monad.when (Data.CommandOptions.getVerbosity commandOptions > minBound) . System.IO.hPutStrLn System.IO.stderr $ Data.List.intercalate "\n" logFile

	return {-to IO-monad-} $ Squeeze.findBestFit (Data.CommandOptions.solutionSizeBounds commandOptions) acceptedFileSizeAndPathList