我最近的任务是对我们 API 的一些特性进行基准测试,我一直在使用 Caliper 来做这件事。它看起来相当简单,实际上非常强大,我一直在关注这里的教程:
我在我们当前的应用程序中使用 Guice,所以当我尝试运行基准测试时,我确保注入我需要的服务
下面提供的是我的代码。我尝试使用自己的@injected
注释设置变量,我尝试直接启动它们(尽管有太多的依赖关系需要处理,我也必须启动它们)。@Parms
注释不起作用,因为我需要一个字符串类型来迭代(参数只接受字符串,有文档说明如果它是另一种类型该怎么办,但它需要一个.toString
类型方法)
package com.~~~.~~~.api.benchmark;
import com.~~~.ds.mongo.orm.user.User;
import com.~~~.~~~.api.exceptions.auth.AccessDeniedException;
import com.~~~.~~~.api.service.authorization.UserService;
import com.~~~.~~~.api.service.campaign.CampaignMetadataService;
import com.google.caliper.BeforeExperiment;
import com.google.caliper.Benchmark;
import com.google.caliper.Param;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
public class CampaignBenchmark {
// === SERVICE INJECTIONS
@Inject
private CampaignMetadataService campaignMetadataService;
@Inject
private UserService userService;
// =============================================
// === BENCHMARK PARMS
@Param({
"7ca8c319",
"49191829"
})
String userId;
@Param({
"485",
"500"
})
String hashAdvertiserId;
// =============================================
// === TESTING PARMS
private User user;
// =============================================
// === SETUP
@Inject
public CampaignBenchmark(){
Injector injector = Guice.createInjector();
this.userService = injector.getInstance(UserService.class);
this.campaignMetadataService = injector.getInstance(CampaignMetadataService.class);
}
@BeforeExperiment
void setUp(){
this.user = userService.getUserByHashedId(userId);
}
// =============================================
// === BENCHMARKS
@Benchmark
int fetchAllCampaign(int reps) throws AccessDeniedException {
VideoIqUser user = this.user;
String hashAdvertiserId = this.hashAdvertiserId;
int dummy = 0;
for(int i=0 ; i<reps ; i++){
dummy |= campaignMetadataService.fetchAllCampaigns(user, hashAdvertiserId).size();
}
return dummy;
}
}
当我们尝试运行它时
mvn exec:java -Dexec.mainClass="com.google.caliper.runner.CaliperMain" -Dexec.args="com.~~~.~~~.api.benchmark.CampaignBenchmark"
我们得到以下
WARNING: All illegal access operations will be denied in a future release
Experiment selection:
Benchmark Methods: [fetchAllCampaign]
Instruments: [allocation, runtime]
User parameters: {hashAdvertiserId=[485, 500], userId=[7ca8c319, 49191829]}
Virtual machines: [default]
Selection type: Full cartesian product
This selection yields 16 experiments.
Could not create an instance of the benchmark class following reasons:
1) Explicit bindings are required and com.~~~.~~~.api.service.campaign.CampaignMetadataService is not explicitly bound.
2) Explicit bindings are required and com.~~~.~~~.api.service.authorization.UserService is not explicitly bound.
问题是:我应该在什么时候进行注射,我该怎么做?我应该有一个包装类设置吗?
快速更新
我忘了提到它是 DropWizard (0.7.1) 应用程序的一部分。我们使用资源并将其注入环境
前任:
environment.jersey().register(injector.getInstance(CampaignManagementResource.class));
这些资源包含运行它们所需的服务,它们作为@Inject 包含在内,尽管我们从未在其他任何地方实际指定绑定。
@Inject
private CampaignMetadataService apiCampaignMetadataService;
我应该为 DropWizard 调整什么,或者我应该只是模拟服务?