- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个项目,在该项目中我尝试使用 RestTemplate
对我的一台服务器进行 URL 调用它给我返回一个 JSON 字符串作为响应,并且工作正常......
现在我决定对此进行一些性能测试......
下面是我的代码,它使用 ExecutorService
和Callables
-
public class URLTest {
private ExecutorService executor = Executors.newFixedThreadPool(10);
public String getData() {
Future<String> future = executor.submit(new Task());
String response = null;
try {
System.out.println("Started..");
response = future.get(200, TimeUnit.MILLISECONDS);
System.out.println("Finished!");
} catch (TimeoutException e) {
System.out.println("Terminated!");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return response;
}
}
下面是我的任务类,它实现了 Callable 接口(interface) -
class Task implements Callable<String> {
private RestTemplate restTemplate = new RestTemplate();
public String call() throws Exception {
String url = "some_url";
// TimerTest timer = TimerTest.getInstance(); // line 3
String response = restTemplate.getForObject(url, String.class);
// timer.getDuration(); // line 4
System.out.println(response);
return response;
}
}
下面是我在另一个类中的代码 DemoTest
它调用 getData
URLTest
中的方法类500 times
并测量它的第 95 个百分位数,因为这就是我需要做的 -
public class DemoTest {
public static void main(String[] args) {
URLTest bc = new URLTest();
for (int i = 0; i <= 500; i++) {
TimerTest timer = TimerTest.getInstance(); // line 1
bc.getData();
timer.getDuration(); // line 2
}
// this method prints out the 95th percentile
logPercentileInfo();
}
}
按照上面的代码,我总是将第 95 个百分位数视为 14-15 ms
(这对我的用例不利,因为它是端到端流程,这就是我需要测量的)但是如果我注释掉 line 1
和line 2
在DemoTest
类并取消注释 line 3
和line 4
在Task
类,然后再次运行该程序,它将开始给我第 95 个百分位,如 3 ms
.
我很惊讶为什么?是ExectuorFramework
在这里添加所有延迟?为什么如果我只测量 RestTemplate
的性能调用,那么与端到端性能相比,它总是能给我更好的性能?
我的主要目标是尽可能减少这里的延迟。我的用例很简单,对我的一台启用了超时功能的服务器进行 URL 调用,这意味着服务器是否需要花费大量时间来响应,然后使整个调用超时。
我的客户端程序和服务器都在同一数据中心的生产环境中运行,因此 ping 时间约为 0.5 毫秒。
我运行了几次来进行此测试,但结果仍然相同..
有什么我遗漏的东西或者ExecutorService
的其他风格吗?我需要使用吗?我怎样才能提高我在这里的表现?任何建议都会有很大帮助..
更新:-
添加了一些预热时间 -
public static void main(String[] args) {
URLTest bc = new URLTest();
// little bit warmup
for (int i = 0; i <= 500; i++) {
bc.getData();
}
for (int i = 0; i <= 500; i++) {
TimerTest timer = TimerTest.getInstance();
bc.getData();
timer.getDuration();
}
logPercentileInfo();
}
最佳答案
ExecutorService 正在增加延迟,因为它必须管理工作线程和工作队列。
但这不应该是 12ms 差异的原因。
据我所知,这里存在几个问题:
System.out.println(response)
的时间,而在另一种情况下,您则没有测量编辑:根据评论中的要求,ExecutorCompletionService 的示例:
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorCompletionExample
{
public static void main(String[] args)
{
ExecutorService executor = Executors.newFixedThreadPool(10);
CompletionService<String> completionService =
new ExecutorCompletionService<String>(executor);
int n = 500;
for (int i = 0; i < n; i++)
{
Task task = new Task("Task"+i);
completionService.submit(task);
}
for (int i = 0; i < n; ++i)
{
try
{
Future<String> future = completionService.take();
String response = future.get();
System.out.println("Response: "+response);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
return;
}
catch (ExecutionException e)
{
e.printStackTrace();
}
}
}
}
class Task implements Callable<String>
{
private static Random random = new Random(0);
private String name;
private long delay;
Task(String name)
{
this.name = name;
this.delay = 10 + random.nextInt(10)*10;
}
public String call() throws Exception
{
//String response = restTemplate.getForObject(url, String.class);
Thread.sleep(delay);
String response = "Response of "+name+" with delay "+delay;
return response;
}
}
用法相当简单,据我了解,最初的问题涉及到与 CompletionService
接口(interface)的描述相对应的任务:
"A service that decouples the production of new asynchronous tasks from the consumption of the results of completed tasks. .. A
CompletionService
can for example be used to manage asynchronous IO, in which tasks that perform reads are submitted in one part of a program or system, and then acted upon in a different part of the program when the reads complete, possibly in a different order than they were requested."
无法根据给定的示例来测试这是否真正符合预期的用例(当然,它是否有助于实现所需的总体延迟减少)。
关于java - 执行器框架在性能测试中增加了大量时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21231135/
我需要开发一个简单的网站,我通常使用 bootstrap CSS 框架,但是我想使用 Gumbyn,它允许我使用 16 列而不是 12 列。 我想知道是否: 我可以轻松地改变绿色吗? 如何使用固定布局
这个问题在这里已经有了答案: 关闭 13 年前。 与直接编写 PHP 代码相比,使用 PHP 框架有哪些优点/缺点?
我开发了一个 Spring/JPA 应用程序:服务、存储库和域层即将完成。 唯一缺少的层是网络层。我正在考虑将 Playframework 2.0 用于 Web 层,但我不确定是否可以在我的 Play
我现有的 struts Web 应用程序具有单点登录功能。然后我将使用 spring 框架创建一个不同的 Web 应用程序。然后想要使用从 struts 应用程序登录的用户来链接新的 spring 应
我首先使用Spark框架和ORMLite处理网页上表单提交的数据,在提交中文字符时看到了unicode问题。我首先想到问题可能是由于ORMLite,因为我的MySQL数据库的字符集已设置为使用utf8
我有一个使用 .Net 4.5 功能的模块,我们的应用程序也适用于 XP 用户。所以我正在考虑将这个 .net 4.5 依赖模块移动到单独的项目中。我怎样才能有一个解决方案,其中有两个项目针对不同的版
我知道这是一个非常笼统的问题,但我想我并不是真的在寻找明确的答案。作为 PHP 框架的新手,我很难理解它。 Javascript 框架,尤其是带有 UI 扩展的框架,似乎通过将 JS 代码与设计分开来
我需要收集一些关于现有 ORM 解决方案的信息。 请随意编写任何编程语言。 你能谈谈你用过的最好的 ORM 框架吗?为什么它比其他的更好? 最佳答案 我使用了 NHibernate 和 Entity
除了 Apple 的 SDK 之外,还有什么强大的 iPhone 框架可供开始开发?有没有可以加快开发时间的方法? 最佳答案 此类框架最大的是Three20 。 Facebook 和许多其他公司都使用
有人可以启发我使用 NodeJS 的 Web 框架吗?我最近开始从免费代码营学习express js,虽然一切进展顺利,但我对express到底是什么感到困惑。是全栈框架吗?纯粹是为了后端吗?我发现您
您可以推荐哪种 Ajax 框架/工具包来构建使用 struts 的 Web 应用程序的 GUI? 最佳答案 我会说你的 AJAX/javascript 库选择应该较少取决于你的后端是如何实现的,而更多
我有生成以下错误的 python 代码: objc[36554]: Class TKApplication is implemented in both /Library/Frameworks/Tk.
首先,很抱歉,如果我问的问题很明显,因为我没有编程背景,那我去吧: 我想运行一系列测试场景并在背景部分声明了几个变量(我打印它们以仔细检查它们是否已正确声明),第一个是整数,另外两个字符串为你可以看到
在我们承担的一个项目中,我们正在寻找一个视频捕获和录制库。我们的基础工作(基于 google 搜索)表明 vlc (libvlc)、ffmpeg (libavcodec) 和 gstreamer 是三
我试过没有运气的情况下寻找某种功能来杀死/中断Play中的正常工作!框架。 我想念什么吗?还是玩了!实际没有添加此功能? 最佳答案 Java stop类中没有像Thread方法那样的东西,由于种种原因
我们希望在我们的系统中保留所有重大事件的记录。例如,在数据库可能存储当前用户状态的地方,事件日志应记录对该状态的所有更改以及更改发生的时间。 事件记录工具应该尽可能接近于事件引发器的零开销,应该容纳结
那里有 ActionScript 2.0/3.0 的测试框架列表吗? 最佳答案 2010-05-18 更新 由于这篇文章有点旧,而且我刚刚收到了赞成票,因此可能值得提供一些更新的信息,这样人们就不会追
我有一个巨大的 numpy 数组列表(一维),它们是不同事件的时间序列。每个点都有一个标签,我想根据其标签对 numpy 数组进行窗口化。我的标签是 0、1 和 2。每个窗口都有一个固定的大小 M。
我是 Play 的新手!并编写了我的第一个应用程序。这个应用程序有一组它依赖的 URL,从 XML 响应中提取数据并返回有效的 URL。 此应用程序需要在不同的环境(Dev、Staging 和 Pro
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我是一名优秀的程序员,十分优秀!