示例#1
0
        /// <summary>
        /// Creates the instance file.
        /// </summary>
        /// <param name="instanceSeed">The instance seed.</param>
        /// <returns>The instance file.</returns>
        private static InstanceFile CreateInstanceFile(int instanceSeed)
        {
            File.WriteAllText(BbobRunnerTests.SeedFilePath, $"{instanceSeed}");
            var instance = new InstanceFile(BbobRunnerTests.SeedFilePath);

            return(instance);
        }
示例#2
0
        public void EqualsReturnsFalseForOneObjectNotInstanceFile()
        {
            var instance  = new InstanceFile("1");
            var wrongType = new TestInstance("1");

            Assert.False(
                object.Equals(wrongType, instance),
                $"Instance {instance} was identified to be equal to {wrongType}.");
        }
示例#3
0
        public void EqualsReturnsFalseForDifferentValue()
        {
            var firstInstance  = new InstanceFile("1");
            var secondInstance = new InstanceFile("2");

            Assert.False(
                object.Equals(firstInstance, secondInstance),
                $"Instance {firstInstance} is supposedly the same as instance {secondInstance}.");
        }
示例#4
0
        public void EqualsReturnsTrueEqualFileNames()
        {
            string path      = "1";
            var    instance1 = new InstanceFile(path);
            var    instance2 = new InstanceFile(path);

            Assert.True(
                object.Equals(instance1, instance2),
                $"{instance1} and {instance2} are supposedly different, but both encode instance {path}.");
        }
示例#5
0
        public void TryToGetInstanceFromInstanceIdWorksForInstanceFiles()
        {
            var instanceFile           = new InstanceFile("dummy");
            var targetAlgorithmFactory =
                new DummyTargetAlgorithmFactory <DummyTargetAlgorithm <InstanceFile, TestResult>, InstanceFile, TestResult>() as
                ITargetAlgorithmFactory <DummyTargetAlgorithm <InstanceFile, TestResult>, InstanceFile, TestResult>;

            targetAlgorithmFactory.TryToGetInstanceFromInstanceId(instanceFile.ToId(), out var instance).ShouldBeTrue();
            instance.Equals(instanceFile).ShouldBeTrue();
        }
示例#6
0
        /// <summary>
        /// Creates a cancellable task that runs the <see cref="CommandExecutorBase{TResult}.Command"/> on the given instance.
        /// </summary>
        /// <param name="instance">Instance to run on.</param>
        /// <param name="cancellationToken">Token that is regurlarly checked for cancellation.
        /// If cancellation is detected, the task will be stopped.</param>
        /// <returns>A task that returns the run's result on completion.</returns>
        public override Task <ContinuousResult> Run(InstanceFile instance, CancellationToken cancellationToken)
        {
            // Define process and redirect standard output to read value to optimize from output.
            var processInfo = this.BuildProcessStartInfo(instance);

            processInfo.RedirectStandardOutput = true;

            return(Task.Run(
                       function: () =>
            {
                // Start process.
                var timer = new Stopwatch();
                timer.Start();
                using (var process = Process.Start(processInfo))
                    using (var processRegistration =
                               cancellationToken.Register(() => ProcessUtils.CancelProcess(process)))
                    {
                        // Wait until end of process.
                        process.WaitForExit();

                        // If the process was cancelled, clean up resources and escalate it up.
                        if (cancellationToken.IsCancellationRequested)
                        {
                            this.CleanUp(process);
                            cancellationToken.ThrowIfCancellationRequested();
                        }

                        // If the process has inappropriate exit code, clean up resources and return cancelled result.
                        if (process.ExitCode != 0)
                        {
                            this.CleanUp(process);
                            return ContinuousResult.CreateCancelledResult(this._timeout);
                        }

                        // If the process was not cancelled, find the last value written to console.
                        string output = process.StandardOutput.ReadToEnd();
                        timer.Stop();

                        // If the output does not match to regex, clean up resources and return cancelled result.
                        if (!ValueReadingExecutor.NumberMatcher.IsMatch(output))
                        {
                            this.CleanUp(process);
                            return ContinuousResult.CreateCancelledResult(this._timeout);
                        }

                        // If the output matches to regex, clean up resources and return the founded output as result.
                        double value = double.Parse(ValueReadingExecutor.NumberMatcher.Match(output).Value, CultureInfo.InvariantCulture);
                        this.CleanUp(process);
                        return new ContinuousResult(value, timer.Elapsed);
                    }
            },
                       cancellationToken: cancellationToken));
        }
示例#7
0
        public void GetHashCodeReturnsDifferentHashCodesForDifferentPaths()
        {
            var instance1 = new InstanceFile("1");
            var instance2 = new InstanceFile("2");

            Assert.False(object.Equals(instance1, instance2));

            var firstInstanceHash  = instance1.GetHashCode();
            var secondInstanceHash = instance2.GetHashCode();

            Assert.True(
                firstInstanceHash != secondInstanceHash,
                $"Instances {instance1} and {instance2} are not equal, but have equal hashes {firstInstanceHash} and {secondInstanceHash}.");
        }
示例#8
0
        public void GetHashCodeReturnsSameHashCodesForEqualPaths()
        {
            string path      = "test";
            var    instance1 = new InstanceFile(path);
            var    instance2 = new InstanceFile(path);

            Assert.True(object.Equals(instance1, instance2));

            var firstInstanceHash  = instance1.GetHashCode();
            var secondInstanceHash = instance2.GetHashCode();

            Assert.True(
                firstInstanceHash == secondInstanceHash,
                $"Instances {instance1} and {instance2} are equal, but have different hashes {firstInstanceHash} and {secondInstanceHash}.");
        }
示例#9
0
        /// <summary>
        /// Builds the <see cref="ProcessStartInfo"/> for starting the algorithm on the given instance.
        /// </summary>
        /// <param name="instance">The instance to start the algorithm on.</param>
        /// <returns>The built <see cref="ProcessStartInfo"/>.</returns>
        protected ProcessStartInfo BuildProcessStartInfo(InstanceFile instance)
        {
            // Create the process information using the correct program and parameters.
            var command = this.Command.Replace(InstanceReplacement, $"\"{instance.Path}\"");
            ProcessStartInfo processInfo = new ProcessStartInfo(
                fileName: command.Split(' ').First(),
                arguments: command.Substring(command.IndexOf(' ') + 1))
            {
                // Make sure no additional window will be opened on process start.
                CreateNoWindow  = true,
                UseShellExecute = false,
            };

            return(processInfo);
        }
示例#10
0
        public void TimeMeasuringExecutorCreatesNotCancelledResult()
        {
            var timeout      = TimeSpan.FromSeconds(10);
            var exitCode     = 0;
            var basicCommand = $"{TestUtils.ReturnExitCodeApplicationCall} {exitCode}";
            var instance     = new InstanceFile("");

            var timer = new Stopwatch();

            timer.Start();

            var commandExecutor = new TimeMeasuringExecutor(new Dictionary <string, IAllele>(), basicCommand, timeout);
            var runner          = commandExecutor.Run(instance, this._cancellationTokenSource.Token);

            runner.Wait();
            timer.Stop();
            runner.Result.ShouldNotBeNull();
            runner.Result.IsCancelled.ShouldBeFalse();
            runner.Result.Runtime.ShouldBeLessThan(timeout);
            timer.Elapsed.ShouldBeLessThan(timeout);
        }
示例#11
0
        /// <summary>
        /// Creates a cancellable task that runs the <see cref="CommandExecutorBase{TResult}.Command"/> on the given instance.
        /// </summary>
        /// <param name="instance">Instance to run on.</param>
        /// <param name="cancellationToken">Token that is regurlarly checked for cancellation.
        /// If cancellation is detected, the task will be stopped.</param>
        /// <returns>A task that returns the run's runtime on completion.</returns>
        public override Task <RuntimeResult> Run(InstanceFile instance, CancellationToken cancellationToken)
        {
            // Define process to target algorithm from command line.
            var processInfo = this.BuildProcessStartInfo(instance);

            return(Task.Run(
                       function: () =>
            {
                var timer = new Stopwatch();
                timer.Start();
                // Start process and make sure it's cancelled if the cancellationToken is cancelled.
                using (var process = Process.Start(processInfo))
                    using (var processRegistration =
                               cancellationToken.Register(() => ProcessUtils.CancelProcess(process)))
                    {
                        // Wait until end of process.
                        process.WaitForExit();

                        // If the process was cancelled, escalate it up.
                        if (cancellationToken.IsCancellationRequested)
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                        }

                        // If the process has inappropriate exit code, clean up resources and return cancelled result.
                        if (process.ExitCode != 0)
                        {
                            return RuntimeResult.CreateCancelledResult(this._timeout);
                        }

                        // If the process was not cancelled, return CPU time as result.
                        timer.Stop();
                        return new RuntimeResult(timer.Elapsed);
                    }
            },
                       cancellationToken: cancellationToken));
        }
示例#12
0
        public void ToStringReturnsPath()
        {
            var instance = new InstanceFile("bar/foo");

            Assert.Equal("bar/foo", instance.ToString());
        }
示例#13
0
        public void PathIsSetCorrectly()
        {
            var instance = new InstanceFile("bar/foo");

            Assert.Equal("bar/foo", instance.Path);
        }
示例#14
0
 /// <summary>
 /// Creates a cancellable task that runs the <see cref="Command"/> on the given instance.
 /// </summary>
 /// <param name="instance">Instance to run on.</param>
 /// <param name="cancellationToken">Token that is regurlarly checked for cancellation.
 /// If cancellation is detected, the task will be stopped.</param>
 /// <returns>A task that returns the run's result on completion.</returns>
 public abstract Task <TResult> Run(InstanceFile instance, CancellationToken cancellationToken);