3

I'm trying to measure and increase the execution time of a test case. What I'm doing is the following:

  1. Let's assume I'm testing the method abc() using testAbc().
  2. I'm using android studio and junit for my software development.
  3. At the very beginning I'm recording the timestamp in nano seconds in start variable, and then when the method finishes, it returns the difference between the current nano seconds and the start.
  4. testAbc() is divided into 3 parts: initialization, testing abc() and assertion (checking the test results).
  5. I keep track the test time inside testAbc() same way as I do in abc() method.
  6. after executing the test I found that abc() method takes about 45-50% of the test time.
  7. I modified the testAbc() as follows:
void testAbc(){
startTime = System.nanoTime();
//no modification to the initialization part
//testing abc part is modified by placing it in a for loop to increase its 
//execution time.
for(int i=0 ; i < 100; i++)
{
    //test abc code goes here ...
    abcTime += abc();
}

//assertion part wasn't modified
testEndTime = System.nanoTime - startTime;
}

By repeating the test part I thought the ratio between abcTime and testEndTime will increase (by dramatically increasing abcTime), however, it didn't at all I mean it's 45-50%.

My questions:

  • Why the ratio didn't increase? I mean in principle the execution time for the initialization and assertion parts should not be affected by the for loop and therefore the time for abc should get closer to the test time after 100 repetitions.
  • How can I increase the ratio between abc() and testAbc()?

thank you for your time.

  • 1
    Why do you need to slow down your test? How about using some `Thread.sleep()` inside the test? –  Nov 19 '17 at 09:04
  • Hi RC, thank you for your reply. I need to increase the computation of my tests to measure its energy consumption using the internal fuel gauge. I tried Thread.sleep() and using for loop (1 to 1000000) inside abc(). They are very effective to increase the time, but they don't increase the computation. – Mahmoud_Bokhari Nov 20 '17 at 01:09
  • I don't know what computation is, but maybe what you want is to simulate cpu load, if so: https://caffinc.github.io/2016/03/cpu-load-generator/ (NB: not my site). See also https://stackoverflow.com/questions/25101370/simulate-high-cpu-load-for-java-development and https://stackoverflow.com/questions/382113/generate-cpu-load-in-java –  Nov 20 '17 at 06:00
  • @Mahmoud_Bokhari i think the ratio didnt increase because as abcTime increases , testEndTime also increases. or am I missing something? – user1207289 Nov 30 '17 at 00:57

0 Answers0