SlideShare a Scribd company logo
1 of 18
On performance of random
and thread contention
Dmytro_Aleksandrov@epam.com
Link to source 1
Monte-carlo simulation
2
Monte-carlo simulation
3
java.util.Random – 1 per thread:
1 thread ~ 80 sec
2 threads~ 45 sec
4 threads~ 26 sec
ThreadLocalRandom:
1 thread ~ 8 sec
2 threads~ 4 sec
4 threads~ 2 sec
Summary
4
java.util.Random:
• thread-safe aka synchronized
• AtomicLong as seed
java.util.concurrent.ThreadLocalRandom:
• UNSAFE long as seed
Link to source: http://java-performance.info/java-util-random-java-util-concurrent-threadlocalrandom-multithreaded-environments/
Not yet reactive but
asynchronous MVC
Dmytro_Aleksandrov@epam.com
Link to source 5
Signature of ‘typical’ @RequestMapping
6
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getUser()
Link to source: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
How does it processed
7
• Threaded model
Link to source: http://www.oracle.com/technetwork/articles/grid/asynchronous-servlets-088344.html
Now imagine a service
8
@RestController
class EventsController {
}
def fastEvent(): Event =
{…}
def blockingOp(): Int = {…}
Now imagine a service
9
def blockingOp(): Int = {
val processingTime = ThreadLocalRandom.current.nextInt(1000, 2000)
// IO bounded operation intended here
Thread.sleep(processingTime)
processingTime
}
def fakeEvent(): Event = {
Event("fakeId", System.currentTimeMillis, Array.empty)
}
With one “heavy” operation
And a light one
Load test sample
10
Link to source: http://goo.gl/abYAO8
Side note
11
Link to source: http://gatling.io/
Load test are written in Gatling
Try to simulate C10K with JMeter
threaded model, again
Why not Jmeter?
Link to source: https://github.com/alkersan/timebench/blob/master/modules/loadtest/src/test/scala/SpringBootSimulation.scala
Asynchronous completion
12
@RequestMapping(value = "/user", method = RequestMethod.GET)
public Callable<String> getUser()
• Servlet 3.0 and 3.1
• Spring 3.2
Simply change signature to utilize this:
or Future<T>
or DeferredResult<T>
Link to source: http://docs.spring.io p.17.3.4
What’s changed under load
13
Link to source: http://goo.gl/hYl0Ie
SLA
14
def fakeEvent(): Event = {
Event("fakeId", System.currentTimeMillis, Array.empty)
}
Light operations:
Can be served under load
Why this still is not “reactive”?
15
Because in reality it doesn’t solve the reason of blocking
}
def blockingOp(): Int = {
// JDBC call
// or Remote WS call
// or File IO
// or remote RPC call
For example: SpringData-Elastic call
16
• Intuitive interface
• Reduces boilerplate
public interface UserRepository extends
CrudRepository<User, Long> {
Long deleteByLastname(String lastname);
List<User> removeByLastname(String lastname);
}
Link to source: http://docs.spring.io/spring-data/elasticsearch/docs/1.1.0.M1/reference/htmlsingle/
“Doubt everything we know”
- Descartes
17
Basic construct is:
ElasticsearchTemplate
public <T> Page<T> queryForPage (StringQuery query, …args…, ..args…) {
SearchResponse response = repareSearch(query,clazz)
.setQuery(query.getSource())
.execute()
.actionGet();
return mapper.mapResults(response, clazz, query.getPageable());
}
ListenableFuture
Synchronously Awaits
(i.e. Blocks!)
Link to source: https://github.com/spring-projects/spring-data-elasticsearch
Conclusion
18
Always look under the hood

More Related Content

What's hot

Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8
Martin Toshev
 

What's hot (20)

Quick Introduction to Kotlin Coroutine for Android Dev
Quick Introduction to Kotlin Coroutine for Android DevQuick Introduction to Kotlin Coroutine for Android Dev
Quick Introduction to Kotlin Coroutine for Android Dev
 
OB1K - New, Better, Faster, Devops Friendly Java container by Outbrain
OB1K - New, Better, Faster, Devops Friendly Java container by OutbrainOB1K - New, Better, Faster, Devops Friendly Java container by Outbrain
OB1K - New, Better, Faster, Devops Friendly Java container by Outbrain
 
Ob1k presentation at Java.IL
Ob1k presentation at Java.ILOb1k presentation at Java.IL
Ob1k presentation at Java.IL
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Fork Join
Fork JoinFork Join
Fork Join
 
Introduction to Reactive Java
Introduction to Reactive JavaIntroduction to Reactive Java
Introduction to Reactive Java
 
Lecture5
Lecture5Lecture5
Lecture5
 
Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8
 
Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)
 
OHHttpStubs
OHHttpStubsOHHttpStubs
OHHttpStubs
 
Structured concurrency with Kotlin Coroutines
Structured concurrency with Kotlin CoroutinesStructured concurrency with Kotlin Coroutines
Structured concurrency with Kotlin Coroutines
 
Real world functional reactive programming
Real world functional reactive programmingReal world functional reactive programming
Real world functional reactive programming
 
Service discovery like a pro (presented at reversimX)
Service discovery like a pro (presented at reversimX)Service discovery like a pro (presented at reversimX)
Service discovery like a pro (presented at reversimX)
 
RxJava Applied
RxJava AppliedRxJava Applied
RxJava Applied
 
Reactive Java (33rd Degree)
Reactive Java (33rd Degree)Reactive Java (33rd Degree)
Reactive Java (33rd Degree)
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 
Basic example using quartz component in anypoint studio
Basic example using quartz component in anypoint studioBasic example using quartz component in anypoint studio
Basic example using quartz component in anypoint studio
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
 

Similar to Not yet reactive but asyncronous mvc

Similar to Not yet reactive but asyncronous mvc (20)

"Highlights from Java 10&11 and Future of Java" at Java User Group Bonn 2018 ...
"Highlights from Java 10&11 and Future of Java" at Java User Group Bonn 2018 ..."Highlights from Java 10&11 and Future of Java" at Java User Group Bonn 2018 ...
"Highlights from Java 10&11 and Future of Java" at Java User Group Bonn 2018 ...
 
Highlights from Java 10-13 and Future of Java at JCON 2019 by Alukhanov and K...
Highlights from Java 10-13 and Future of Java at JCON 2019 by Alukhanov and K...Highlights from Java 10-13 and Future of Java at JCON 2019 by Alukhanov and K...
Highlights from Java 10-13 and Future of Java at JCON 2019 by Alukhanov and K...
 
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
 
Ch10.애플리케이션 서버의 병목_발견_방법
Ch10.애플리케이션 서버의 병목_발견_방법Ch10.애플리케이션 서버의 병목_발견_방법
Ch10.애플리케이션 서버의 병목_발견_방법
 
Highlights from Java 10, 11 and 12 and Future of Java at JUG Koblenz
Highlights from Java 10, 11 and 12 and Future of Java at JUG KoblenzHighlights from Java 10, 11 and 12 and Future of Java at JUG Koblenz
Highlights from Java 10, 11 and 12 and Future of Java at JUG Koblenz
 
Scalable io in java
Scalable io in javaScalable io in java
Scalable io in java
 
Data access
Data accessData access
Data access
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 
J2EE-assignment
 J2EE-assignment J2EE-assignment
J2EE-assignment
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx java
 
Database connect
Database connectDatabase connect
Database connect
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
Container Orchestration from Theory to Practice
Container Orchestration from Theory to PracticeContainer Orchestration from Theory to Practice
Container Orchestration from Theory to Practice
 
Java 8
Java 8Java 8
Java 8
 
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
No callbacks, No Threads - Cooperative web servers in Ruby 1.9No callbacks, No Threads - Cooperative web servers in Ruby 1.9
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 

Not yet reactive but asyncronous mvc

  • 1. On performance of random and thread contention Dmytro_Aleksandrov@epam.com Link to source 1
  • 3. Monte-carlo simulation 3 java.util.Random – 1 per thread: 1 thread ~ 80 sec 2 threads~ 45 sec 4 threads~ 26 sec ThreadLocalRandom: 1 thread ~ 8 sec 2 threads~ 4 sec 4 threads~ 2 sec
  • 4. Summary 4 java.util.Random: • thread-safe aka synchronized • AtomicLong as seed java.util.concurrent.ThreadLocalRandom: • UNSAFE long as seed Link to source: http://java-performance.info/java-util-random-java-util-concurrent-threadlocalrandom-multithreaded-environments/
  • 5. Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com Link to source 5
  • 6. Signature of ‘typical’ @RequestMapping 6 @RequestMapping(value = "/user", method = RequestMethod.GET) public String getUser() Link to source: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
  • 7. How does it processed 7 • Threaded model Link to source: http://www.oracle.com/technetwork/articles/grid/asynchronous-servlets-088344.html
  • 8. Now imagine a service 8 @RestController class EventsController { } def fastEvent(): Event = {…} def blockingOp(): Int = {…}
  • 9. Now imagine a service 9 def blockingOp(): Int = { val processingTime = ThreadLocalRandom.current.nextInt(1000, 2000) // IO bounded operation intended here Thread.sleep(processingTime) processingTime } def fakeEvent(): Event = { Event("fakeId", System.currentTimeMillis, Array.empty) } With one “heavy” operation And a light one
  • 10. Load test sample 10 Link to source: http://goo.gl/abYAO8
  • 11. Side note 11 Link to source: http://gatling.io/ Load test are written in Gatling Try to simulate C10K with JMeter threaded model, again Why not Jmeter? Link to source: https://github.com/alkersan/timebench/blob/master/modules/loadtest/src/test/scala/SpringBootSimulation.scala
  • 12. Asynchronous completion 12 @RequestMapping(value = "/user", method = RequestMethod.GET) public Callable<String> getUser() • Servlet 3.0 and 3.1 • Spring 3.2 Simply change signature to utilize this: or Future<T> or DeferredResult<T> Link to source: http://docs.spring.io p.17.3.4
  • 13. What’s changed under load 13 Link to source: http://goo.gl/hYl0Ie
  • 14. SLA 14 def fakeEvent(): Event = { Event("fakeId", System.currentTimeMillis, Array.empty) } Light operations: Can be served under load
  • 15. Why this still is not “reactive”? 15 Because in reality it doesn’t solve the reason of blocking } def blockingOp(): Int = { // JDBC call // or Remote WS call // or File IO // or remote RPC call
  • 16. For example: SpringData-Elastic call 16 • Intuitive interface • Reduces boilerplate public interface UserRepository extends CrudRepository<User, Long> { Long deleteByLastname(String lastname); List<User> removeByLastname(String lastname); } Link to source: http://docs.spring.io/spring-data/elasticsearch/docs/1.1.0.M1/reference/htmlsingle/
  • 17. “Doubt everything we know” - Descartes 17 Basic construct is: ElasticsearchTemplate public <T> Page<T> queryForPage (StringQuery query, …args…, ..args…) { SearchResponse response = repareSearch(query,clazz) .setQuery(query.getSource()) .execute() .actionGet(); return mapper.mapResults(response, clazz, query.getPageable()); } ListenableFuture Synchronously Awaits (i.e. Blocks!) Link to source: https://github.com/spring-projects/spring-data-elasticsearch