How to unit test for relative performance?

Given that during deployment, I don’t know which system my code will work on, how to write a performance benchmark that uses the potential of the system as a criterion.

I mean, if the system is able to run a piece of code 1000 times per second, I would like the test to provide the maximum possible approximation to 1000. If it can only make 500, then I would like to compare this figure with it.

If this helps make the answer more specific, I use JUnit4.

Thanks.

+3
source share
4 answers

, /. , , . , .

, , .

, " ", "". . Whetstone, Dhrystone .. . , , , , TPC. , , netperf. , GUI, - .

- " ". . . . .

, .

- - . , , , , ..

, : " , , , , [X] ". "".

, . , , 1000 .

+5

.

-, /. /, , , .

-, . , , / .. .

+6

Brian, , . , /.
, , , , , .

import static org.junit.Assert.*;
import org.junit.Test;

package com.stackoverflow.samples.tests {

    @Test
    public void doStuffRuns500TimesPerSecond() {
        long maximumRunningTime = 1000;
        long currentRunningTime = 0;
        int iterations = 0;

        do {
            long startTime = System.getTimeMillis();

            // do stuff

            currentRunningTime += System.getTimeMillis() - startTime;
            iterations++;
        }
        while (currentRunningTime <= maximumRunningTime);

        assertEquals(500, iterations);
    }
}
+2

, , , , .

, , - -, . , . - , .

, , , . , , - - . , .

, , - , , , .. .

, , , , , .

DSP, , , !

- jeffk ++

0

Source: https://habr.com/ru/post/1712141/


All Articles