Last active
December 25, 2019 13:47
-
-
Save L-Soft/c2de3f1faf1c1f0888a76c244137fa20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.JSONObject; | |
import org.springframework.http.*; | |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
import org.springframework.util.StopWatch; | |
import java.util.Collections; | |
import java.util.concurrent.CyclicBarrier; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class RestTemplate { | |
static AtomicInteger counter = new AtomicInteger(); | |
public static void main(String[] args) throws Exception { | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setContentType(MediaType.APPLICATION_JSON); | |
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); | |
// ∂headers.setBasicAuth("userName", "password"); | |
JSONObject jsonObject = new JSONObject(); | |
jsonObject.put("title", "test"); | |
jsonObject.put("body", "test"); | |
HttpEntity<String> entity = new HttpEntity<String>(jsonObject.toString(), headers); | |
ExecutorService es = Executors.newFixedThreadPool(100); | |
org.springframework.web.client.RestTemplate rt = new org.springframework.web.client.RestTemplate(); | |
String url = "http://localhost:3000/memo"; | |
final CyclicBarrier barrier = new CyclicBarrier(101); | |
int iCnt = 100; | |
while (iCnt-- > 0) { | |
es.submit(() -> { | |
int idx = counter.addAndGet(1); | |
System.out.println("counter: " + idx); | |
barrier.await(); | |
StopWatch sw = new StopWatch(); | |
sw.start(); | |
//String res = rt.postForEntity(url, String.class, idx); | |
final ResponseEntity<String> response = rt.exchange(url, HttpMethod.POST, entity, String.class); | |
if (response.getStatusCode() == HttpStatus.OK || response.getStatusCode() == HttpStatus.CREATED) { | |
System.out.println(response); | |
} { | |
System.out.println("E: " + response); | |
} | |
sw.stop(); | |
return null; | |
}); | |
} | |
barrier.await(); | |
StopWatch main = new StopWatch(); | |
main.start(); | |
es.shutdown(); | |
es.awaitTermination(1000, TimeUnit.SECONDS); | |
main.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment