You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Owner <o8...@gmail.com> on 2025/01/21 09:39:59 UTC

Tomcat Virtual Threads Performance

Hey Tomcat,

Andy here. I'm not sure if this is the place to ask this question, but I
wanted to check regarding performances of virtual threads vs using a
conventional worker thread pool.

I set up an endpoint with a CPU task of calculating factorials like the
below:

@GetMapping("/fullCPU")
    public void allCPU() throws InterruptedException {
        factorialCalculator();
    }

public void factorialCalculator() {
        int numberOfOperations = 35;
        BigInteger factorial = BigInteger.ONE;
        int n = 4000;
        for (int i = 1; i <= numberOfOperations; i++) {
            for (int j = 1; j <= n; j++) {
                factorial = factorial.multiply(BigInteger.valueOf(j));
            }
            factorial = BigInteger.ONE;
        }

When enabling virtual threads (using spring.virtual.threads.enabled with
Tomcat 10.1.33), the results I get are as follows. I utilise JMeter to do
stress testing -
[image: image.png]
Is there a reason why for CPU tasks, the performance for virtual threads is
so much worse? Theoretically both virtual threads and platform threads
should take the same amount of CPU time right?

Thank you!

Regards,
Andy

Re: Tomcat Virtual Threads Performance

Posted by Christopher Schultz <ch...@christopherschultz.net>.
All,

On 1/21/25 7:12 AM, Mark Thomas wrote:
> On 21/01/2025 11:17, joan.balaguero@ventusproxy.com wrote:
>> Hi,
>>
>> Virtual threads only shine on I/O bound tasks, in terms of throughput 
>> (not latency). They were created for that, don't expect any 
>> improvement on CPU bound tasks.
> 
> +1.
> 
> If I had to guess, I'd guess something to do with concurrency levels and 
> context switching.
> 
> If you want to know the real answer then you'll need to use a profiler. 
> I use YourKit because they give free copies to open source developers. 
> Other profilers are available.

+1

Also, your code doesn't show how you use threads at all. If you are 
creating a zillion virtual threads and discarding them, that takes time. 
If you are only creating a small number of platform threads and re-using 
them, that takes ... less time.

-chris

>> -----Original Message-----
>> From: Owner <o8...@gmail.com>
>> Sent: Tuesday, January 21, 2025 11:32 AM
>> To: Tomcat Users List <us...@tomcat.apache.org>
>> Subject: Re: Tomcat Virtual Threads Performance
>>
>> Hi Maxim,
>>
>> https://postimg.cc/N9n8CQW7
>>
>> there you go! Do let me know if it's not up :)
>>
>> Regards
>> Andy
>>
>> On Tue, Jan 21, 2025 at 6:30 PM Maxim Solodovnik <so...@gmail.com>
>> wrote:
>>
>>> your image was dropped :((
>>> please use some image image service and provide the URL here :))
>>>
>>> On Tue, 21 Jan 2025 at 16:40, Owner <o8...@gmail.com> wrote:
>>>
>>>> Hey Tomcat,
>>>>
>>>> Andy here. I'm not sure if this is the place to ask this question,
>>>> but I wanted to check regarding performances of virtual threads vs
>>>> using a conventional worker thread pool.
>>>>
>>>> I set up an endpoint with a CPU task of calculating factorials like
>>>> the
>>>> below:
>>>>
>>>> @GetMapping("/fullCPU")
>>>>      public void allCPU() throws InterruptedException {
>>>>          factorialCalculator();
>>>>      }
>>>>
>>>> public void factorialCalculator() {
>>>>          int numberOfOperations = 35;
>>>>          BigInteger factorial = BigInteger.ONE;
>>>>          int n = 4000;
>>>>          for (int i = 1; i <= numberOfOperations; i++) {
>>>>              for (int j = 1; j <= n; j++) {
>>>>                  factorial = factorial.multiply(BigInteger.valueOf(j));
>>>>              }
>>>>              factorial = BigInteger.ONE;
>>>>          }
>>>>
>>>> When enabling virtual threads (using spring.virtual.threads.enabled
>>>> with Tomcat 10.1.33), the results I get are as follows. I utilise
>>>> JMeter to do stress testing -
>>>> [image: image.png]
>>>> Is there a reason why for CPU tasks, the performance for virtual
>>>> threads is so much worse? Theoretically both virtual threads and
>>>> platform threads should take the same amount of CPU time right?
>>>>
>>>> Thank you!
>>>>
>>>> Regards,
>>>> Andy
>>>>
>>>
>>>
>>> -- 
>>> Best regards,
>>> Maxim
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat Virtual Threads Performance

Posted by Mark Thomas <ma...@apache.org>.
On 21/01/2025 11:17, joan.balaguero@ventusproxy.com wrote:
> Hi,
> 
> Virtual threads only shine on I/O bound tasks, in terms of throughput (not latency). They were created for that, don't expect any improvement on CPU bound tasks.

+1.

If I had to guess, I'd guess something to do with concurrency levels and 
context switching.

If you want to know the real answer then you'll need to use a profiler. 
I use YourKit because they give free copies to open source developers. 
Other profilers are available.

Mark


> 
> Regards,
> 
> Joan.
> 
> -----Original Message-----
> From: Owner <o8...@gmail.com>
> Sent: Tuesday, January 21, 2025 11:32 AM
> To: Tomcat Users List <us...@tomcat.apache.org>
> Subject: Re: Tomcat Virtual Threads Performance
> 
> Hi Maxim,
> 
> https://postimg.cc/N9n8CQW7
> 
> there you go! Do let me know if it's not up :)
> 
> Regards
> Andy
> 
> On Tue, Jan 21, 2025 at 6:30 PM Maxim Solodovnik <so...@gmail.com>
> wrote:
> 
>> your image was dropped :((
>> please use some image image service and provide the URL here :))
>>
>> On Tue, 21 Jan 2025 at 16:40, Owner <o8...@gmail.com> wrote:
>>
>>> Hey Tomcat,
>>>
>>> Andy here. I'm not sure if this is the place to ask this question,
>>> but I wanted to check regarding performances of virtual threads vs
>>> using a conventional worker thread pool.
>>>
>>> I set up an endpoint with a CPU task of calculating factorials like
>>> the
>>> below:
>>>
>>> @GetMapping("/fullCPU")
>>>      public void allCPU() throws InterruptedException {
>>>          factorialCalculator();
>>>      }
>>>
>>> public void factorialCalculator() {
>>>          int numberOfOperations = 35;
>>>          BigInteger factorial = BigInteger.ONE;
>>>          int n = 4000;
>>>          for (int i = 1; i <= numberOfOperations; i++) {
>>>              for (int j = 1; j <= n; j++) {
>>>                  factorial = factorial.multiply(BigInteger.valueOf(j));
>>>              }
>>>              factorial = BigInteger.ONE;
>>>          }
>>>
>>> When enabling virtual threads (using spring.virtual.threads.enabled
>>> with Tomcat 10.1.33), the results I get are as follows. I utilise
>>> JMeter to do stress testing -
>>> [image: image.png]
>>> Is there a reason why for CPU tasks, the performance for virtual
>>> threads is so much worse? Theoretically both virtual threads and
>>> platform threads should take the same amount of CPU time right?
>>>
>>> Thank you!
>>>
>>> Regards,
>>> Andy
>>>
>>
>>
>> --
>> Best regards,
>> Maxim
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Tomcat Virtual Threads Performance

Posted by jo...@ventusproxy.com.
Hi,

Virtual threads only shine on I/O bound tasks, in terms of throughput (not latency). They were created for that, don't expect any improvement on CPU bound tasks.

Regards,

Joan.

-----Original Message-----
From: Owner <o8...@gmail.com> 
Sent: Tuesday, January 21, 2025 11:32 AM
To: Tomcat Users List <us...@tomcat.apache.org>
Subject: Re: Tomcat Virtual Threads Performance

Hi Maxim,

https://postimg.cc/N9n8CQW7

there you go! Do let me know if it's not up :)

Regards
Andy

On Tue, Jan 21, 2025 at 6:30 PM Maxim Solodovnik <so...@gmail.com>
wrote:

> your image was dropped :((
> please use some image image service and provide the URL here :))
>
> On Tue, 21 Jan 2025 at 16:40, Owner <o8...@gmail.com> wrote:
>
> > Hey Tomcat,
> >
> > Andy here. I'm not sure if this is the place to ask this question, 
> > but I wanted to check regarding performances of virtual threads vs 
> > using a conventional worker thread pool.
> >
> > I set up an endpoint with a CPU task of calculating factorials like 
> > the
> > below:
> >
> > @GetMapping("/fullCPU")
> >     public void allCPU() throws InterruptedException {
> >         factorialCalculator();
> >     }
> >
> > public void factorialCalculator() {
> >         int numberOfOperations = 35;
> >         BigInteger factorial = BigInteger.ONE;
> >         int n = 4000;
> >         for (int i = 1; i <= numberOfOperations; i++) {
> >             for (int j = 1; j <= n; j++) {
> >                 factorial = factorial.multiply(BigInteger.valueOf(j));
> >             }
> >             factorial = BigInteger.ONE;
> >         }
> >
> > When enabling virtual threads (using spring.virtual.threads.enabled 
> > with Tomcat 10.1.33), the results I get are as follows. I utilise 
> > JMeter to do stress testing -
> > [image: image.png]
> > Is there a reason why for CPU tasks, the performance for virtual 
> > threads is so much worse? Theoretically both virtual threads and 
> > platform threads should take the same amount of CPU time right?
> >
> > Thank you!
> >
> > Regards,
> > Andy
> >
>
>
> --
> Best regards,
> Maxim
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat Virtual Threads Performance

Posted by Owner <o8...@gmail.com>.
Hi Maxim,

https://postimg.cc/N9n8CQW7

there you go! Do let me know if it's not up :)

Regards
Andy

On Tue, Jan 21, 2025 at 6:30 PM Maxim Solodovnik <so...@gmail.com>
wrote:

> your image was dropped :((
> please use some image image service and provide the URL here :))
>
> On Tue, 21 Jan 2025 at 16:40, Owner <o8...@gmail.com> wrote:
>
> > Hey Tomcat,
> >
> > Andy here. I'm not sure if this is the place to ask this question, but I
> > wanted to check regarding performances of virtual threads vs using a
> > conventional worker thread pool.
> >
> > I set up an endpoint with a CPU task of calculating factorials like the
> > below:
> >
> > @GetMapping("/fullCPU")
> >     public void allCPU() throws InterruptedException {
> >         factorialCalculator();
> >     }
> >
> > public void factorialCalculator() {
> >         int numberOfOperations = 35;
> >         BigInteger factorial = BigInteger.ONE;
> >         int n = 4000;
> >         for (int i = 1; i <= numberOfOperations; i++) {
> >             for (int j = 1; j <= n; j++) {
> >                 factorial = factorial.multiply(BigInteger.valueOf(j));
> >             }
> >             factorial = BigInteger.ONE;
> >         }
> >
> > When enabling virtual threads (using spring.virtual.threads.enabled with
> > Tomcat 10.1.33), the results I get are as follows. I utilise JMeter to do
> > stress testing -
> > [image: image.png]
> > Is there a reason why for CPU tasks, the performance for virtual threads
> > is so much worse? Theoretically both virtual threads and platform threads
> > should take the same amount of CPU time right?
> >
> > Thank you!
> >
> > Regards,
> > Andy
> >
>
>
> --
> Best regards,
> Maxim
>

Re: Tomcat Virtual Threads Performance

Posted by Maxim Solodovnik <so...@gmail.com>.
your image was dropped :((
please use some image image service and provide the URL here :))

On Tue, 21 Jan 2025 at 16:40, Owner <o8...@gmail.com> wrote:

> Hey Tomcat,
>
> Andy here. I'm not sure if this is the place to ask this question, but I
> wanted to check regarding performances of virtual threads vs using a
> conventional worker thread pool.
>
> I set up an endpoint with a CPU task of calculating factorials like the
> below:
>
> @GetMapping("/fullCPU")
>     public void allCPU() throws InterruptedException {
>         factorialCalculator();
>     }
>
> public void factorialCalculator() {
>         int numberOfOperations = 35;
>         BigInteger factorial = BigInteger.ONE;
>         int n = 4000;
>         for (int i = 1; i <= numberOfOperations; i++) {
>             for (int j = 1; j <= n; j++) {
>                 factorial = factorial.multiply(BigInteger.valueOf(j));
>             }
>             factorial = BigInteger.ONE;
>         }
>
> When enabling virtual threads (using spring.virtual.threads.enabled with
> Tomcat 10.1.33), the results I get are as follows. I utilise JMeter to do
> stress testing -
> [image: image.png]
> Is there a reason why for CPU tasks, the performance for virtual threads
> is so much worse? Theoretically both virtual threads and platform threads
> should take the same amount of CPU time right?
>
> Thank you!
>
> Regards,
> Andy
>


-- 
Best regards,
Maxim