Exemple #1
0
        private static void ExecuteSunSpider()
        {
            const string fileName = @"..\..\..\Jint.Tests\SunSpider\Tests\access-fannkuch.js";

            var jint = new JintEngine();

            #if false
            jint.ExecuteFile(fileName);
            Console.WriteLine("Attach");
            Console.ReadLine();
            jint.ExecuteFile(fileName);
            #else

            jint.ExecuteFile(fileName);

            var times = new TimeSpan[20];
            int timeOffset = 0;
            var lowest = new TimeSpan();

            // Perform the iterations.

            for (int i = 0; ; i++)
            {
                long memoryBefore = GC.GetTotalMemory(true);

                var stopwatch = Stopwatch.StartNew();

                jint.ExecuteFile(fileName);

                var elapsed = stopwatch.Elapsed;

                long memoryAfter = GC.GetTotalMemory(false);

                times[timeOffset++] = elapsed;
                if (timeOffset == times.Length)
                    timeOffset = 0;

                if (times[times.Length - 1].Ticks != 0)
                {
                    var average = new TimeSpan(times.Sum(p => p.Ticks) / times.Length);

                    if (lowest.Ticks == 0 || average.Ticks < lowest.Ticks)
                        lowest = average;

                    Console.WriteLine(
                        "This run: {0}, average: {1}, lowest: {2}, memory usage: {3}",
                        elapsed.ToString("s\\.fffff"),
                        average.ToString("s\\.fffff"),
                        lowest.ToString("s\\.fffff"),
                        NiceMemory(memoryAfter - memoryBefore)
                    );
                }
            }
            #endif
        }
        public void TestSecp256k1ElGamalBinaryDoubleAndAdd()
        {
            TimeSpan[] setup_times = new TimeSpan[num_runs];
            TimeSpan[] key_generation_times = new TimeSpan[num_runs];
            TimeSpan[] encryption_times = new TimeSpan[num_runs];
            TimeSpan[] decryption_times = new TimeSpan[num_runs];

            for (int i = 0; i < num_runs; i++)
            {
                var stopwatch = Stopwatch.StartNew();

                var curve = CurveFactory.secp256k1;
                WeierstrassCurvePoint.Multiplier = new BinaryDoubleAndAddPointMultiplier();
                var encoder = new ProbabilisticWeierstrassMessageEncoder(curve, new BigInteger(7));
                var encryptor = new ElGamalEncryptor(curve, encoder);

                stopwatch.Stop();
                var t_setup = stopwatch.Elapsed;
                stopwatch.Restart();

                var keys = encryptor.GenerateKeyPair();

                stopwatch.Stop();
                var t_key_generation = stopwatch.Elapsed;
                stopwatch.Restart();

                var c = encryptor.Encrypt(keys.PublicKey, _m);

                stopwatch.Stop();
                var t_encryption = stopwatch.Elapsed;
                stopwatch.Restart();

                var m2 = encryptor.Decrypt(keys.PrivateKey, c);

                stopwatch.Stop();
                var t_decryption = stopwatch.Elapsed;
                var t_total = t_setup + t_key_generation + t_encryption + t_decryption;

                Assert.AreEqual(_m, m2);

                setup_times[i] = t_setup;
                key_generation_times[i] = t_key_generation;
                encryption_times[i] = t_encryption;
                decryption_times[i] = t_decryption;
            }

            var setup_avg = setup_times.Sum(x => x.Milliseconds) / (double)num_runs;
            var key_generation_avg = key_generation_times.Sum(x => x.Milliseconds) / (double)num_runs;
            var encryption_avg = encryption_times.Sum(x => x.Milliseconds) / (double)num_runs;
            var decryption_avg = decryption_times.Sum(x => x.Milliseconds) / (double)num_runs;

            var total_avg = setup_avg + key_generation_avg + encryption_avg + decryption_avg;

            throw new AssertFailedException("Runtime " + total_avg + "(" + key_generation_avg + "," + encryption_avg + "," + decryption_avg + ")");
        }
 static TimeSpan CalculateAverageTimeSpan(TimeSpan[] timeSpans)
 {
     double miliseconds = timeSpans.Sum(t => t.TotalMilliseconds) / timeSpans.Length;
     return TimeSpan.FromMilliseconds(miliseconds);
 }