-2

A program I'm developing has two threads running similar but different task:

thread1:

timer1.start()
writeToExternalDB1(consumedData)
timer1.end()

thread2:

timer2.start()
writeToExternalDB2(consumedData)
timer2.end()

I want to compare the writing performance of the two external database. I have timers around the write operation so that I can collect some metrics. Those two threads are running concurrently.

My question is will this design give correct measurement result regarding to the time spent by each write operation? My understanding is probably not because the processing time given by CPU to each thread may be different. Let's say for thread1, after timer1 started, cpu will not move forward to the followed write operation, but switch to processing thread2 instead. This may introduce some gaps between timer1.start() and writeToExternalDB1(consumedData). However, in that case, should we regard this gap as negligible if we are processing a lot of data? What should I do to give the correct measurement result?

zonyang
  • 828
  • 3
  • 10
  • 24
  • 1
    For a good performance test, it's probably safest to not run anything else at the same time to be sure. But, you likely have more than one CPU core, so your threads will run on independent CPU's if nothing else is running on the machine. – Erwin Bolwidt Nov 21 '20 at 22:04
  • Similar: [*Is `System.currentTimeMillis()` correct across multiple processes?*](https://stackoverflow.com/q/64961179/642706) – Basil Bourque Nov 23 '20 at 06:40

1 Answers1

2

You asked:

Will this time duration measurement in two threads give correct result?

Yes, you will accurately capture start and stop moments if your timer is properly coded, such as calling Instant.now().

You asked:

I want to compare the writing performance of the two external database. … Those two threads are running concurrently.

Your comparison is unlikely to be precise, or even valid.

If your app is running on a machine with a single core, then while one thread is running the other is sleeping.

On a multi-core machine, your two threads may be running simultaneously. Or they may not be, You cannot control the scheduling of processes by the host OS. Your JVM may be paused at any moment by the host OS.

If your machine has multiple cores that are likely to be unused while your test runs, then you might presume your test results are somewhere in the ball park of valid, but you’ll not know for sure, and you certainly cannot call them accurate.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154