aboutsummaryrefslogtreecommitdiffstats
path: root/src/extra_tests/timing/timing-tests/TimingTest.cpp
blob: f33cbd34e0930a0a6782322326bfc9df646b2a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* 
 * File:   TimingTest.cpp
 * Author: Juraj Somorovsky - juraj.somorovsky@hackmanit.de
 * 
 */

#include "TimingTest.h"
#include <time.h>

TimingTest::TimingTest()
   {
   }

TimingTest::~TimingTest()
   {
   }

void TimingTest::execute_evaluation()
   {
   m_results = std::vector<ticks>(m_measurement_iterations * 2);

   for (int i = 0; i < m_inputs.size() - 1; i++) 
      {
      for (int j = i + 1; j < m_inputs.size(); j++) 
         {
         std::cout << "\nExecuting measurements for inputs " << i << " and " << j;
         std::vector<byte> input1 = prepare_input(m_inputs[i]);
         std::vector<byte> input2 = prepare_input(m_inputs[j]);

         for (int k = 0; k < m_warmup_iterations + m_measurement_iterations; k++) 
            {
            ticks t1 = measure_critical_function(input1);
            ticks t2 = measure_critical_function(input2);

            if (k >= m_warmup_iterations) 
               {
               m_results[ (k - m_warmup_iterations) * 2] = t1;
               m_results[ (k - m_warmup_iterations) * 2 + 1] = t2;
               }
            }
         clock_t t;
         t = clock();
         store_results_in_file(std::to_string(t) + "test" + std::to_string(i) + std::to_string(j));
         }
      }
   }

void TimingTest::store_results_in_file(std::string file)
   {
   std::ofstream output(m_result_folder + "/" + file);
   for (int i = 0; i < m_measurement_iterations; i++) 
      {
      output << 2 * i << ";1;" << m_results[2 * i] << "\n";
      output << 2 * i + 1 << ";2;" << m_results[2 * i + 1] << "\n";
      }
   }

/**
 * Taken from Mona Timing Lib
 * Thanks Sebastian ;)
 * 
 * @return Number of processor ticks read using the RDTSC assembler instruction.
 */
ticks TimingTest::get_ticks()
   {
   ticks ret = 0;
   unsigned long minor = 0;
   unsigned long mayor = 0;

   asm volatile(
                "cpuid \n"
                "rdtsc"
                : "=a"(minor),
                "=d"(mayor)
                : "a" (0)
                : "%ebx", "%ecx"
                );

   ret = ((((ticks) mayor) << 32) | ((ticks) minor));

   return ret;
   }