Skip to content

Commit

Permalink
Merge pull request #18 from europeana/EA-3587-stress-test
Browse files Browse the repository at this point in the history
added dummy services for stress testing
  • Loading branch information
gsergiu authored Oct 13, 2023
2 parents 75a6a6b + 3c2e217 commit c351926
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package eu.europeana.api.translation.service.google;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;

public class DummyGLangDetectService extends GoogleLangDetectService {

public DummyGLangDetectService(String googleProjectId,
GoogleTranslationServiceClientWrapper clientWrapperBean) {
super(googleProjectId, clientWrapperBean);
}

@Override
public List<String> detectLang(List<String> texts, String langHint)
throws LanguageDetectionException {
String value = StringUtils.isNotBlank(langHint) ? langHint : "en";
ArrayList<String> ret = new ArrayList<>();

for (int i = 0; i < texts.size(); i++) {
ret.add(value);
}
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package eu.europeana.api.translation.service.google;

import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;

public class DummyGTranslateService extends GoogleTranslationService {

public DummyGTranslateService(String googleProjectId,
GoogleTranslationServiceClientWrapper clientWrapperBean) {
super(googleProjectId, clientWrapperBean);
}

@Override
public List<String> translate(List<String> text, String targetLanguage, String sourceLanguage)
throws TranslationException {
return translate(text, null);
}

@Override
public List<String> translate(List<String> text, String targetLanguage)
throws TranslationException {
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void init(GoogleTranslationServiceClientWrapper clientWrapper) {
}

@Override
public List<String> translate(List<String> texts, String targetLanguage)
public List<String> translate(List<String> text, String targetLanguage)
throws TranslationException {
return translate(texts, targetLanguage, null);
return translate(text, targetLanguage, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class GoogleTranslationServiceClientWrapper {

private static final String MOCK_CLIENT_PROJ_ID = "google-test";
public static final String MOCK_CLIENT_PROJ_ID = "google-test";

private final Logger logger = LogManager.getLogger(getClass());

Expand All @@ -27,8 +27,8 @@ public GoogleTranslationServiceClientWrapper(String projectId, boolean useHttpCl

private void initClient(String projectId, boolean useHttpClient) throws IOException {
// allow service mocking
final boolean initClientConnection = isMockService(projectId);
if(! initClientConnection) {
final boolean skipInitialization = isMockService(projectId);
if(skipInitialization) {
return;
}

Expand Down Expand Up @@ -59,7 +59,7 @@ private void initClient(String projectId, boolean useHttpClient) throws IOExcept
}

private boolean isMockService(String projectId) {
return !MOCK_CLIENT_PROJ_ID.equals(projectId);
return MOCK_CLIENT_PROJ_ID.equals(projectId);
}

public TranslationServiceClient getClient() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.europeana.api.translation.service.pangeanic;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;

public class DummyPangLangDetectService extends PangeanicLangDetectService {

public DummyPangLangDetectService() {
super(null);
}

@Override
public List<String> detectLang(List<String> texts, String langHint)
throws LanguageDetectionException {
String value = StringUtils.isNotBlank(langHint) ? langHint : "en";
ArrayList<String> ret = new ArrayList<>();

for (int i = 0; i < texts.size(); i++) {
ret.add(value);
}
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package eu.europeana.api.translation.service.pangeanic;

import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;

public class DummyPangTranslationService extends PangeanicTranslationService{

public DummyPangTranslationService() {
super(null, null);
}

@Override
public List<String> translate(List<String> text, String targetLanguage, String sourceLanguage)
throws TranslationException {
return translate(text, null);
}

@Override
public List<String> translate(List<String> text, String targetLanguage)
throws TranslationException {
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -47,6 +48,10 @@ public PangeanicLangDetectService(String endPoint) {
* @throws JSONException when there is a problem decoding the received token
*/
private void init() {
if(StringUtils.isBlank(getExternalServiceEndPoint())) {
return;
}

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(PangeanicTranslationUtils.MAX_CONNECTIONS);
cm.setDefaultMaxPerRoute(PangeanicTranslationUtils.MAX_CONNECTIONS_PER_ROUTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -57,6 +58,10 @@ public PangeanicTranslationService(String externalServiceEndpoint,
* @throws JSONException when there is a problem decoding the received token
*/
private void init() {
if(StringUtils.isBlank(getExternalServiceEndPoint())) {
return;
}

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(PangeanicTranslationUtils.MAX_CONNECTIONS);
cm.setDefaultMaxPerRoute(PangeanicTranslationUtils.MAX_CONNECTIONS_PER_ROUTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface BeanNames {
String BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER = "googleTranslationClientWrapper";
String BEAN_GOOGLE_TRANSLATION_SERVICE = "googleTranslationService";
String BEAN_GOOGLE_LANG_DETECT_SERVICE = "googleLangDetectService";
String BEAN_DUMMY_TRANSLATION_SERVICE = "dummyTranslationService";
String BEAN_DUMMY_LANG_DETECT_SERVICE = "dummyLangDetectService";

String BEAN_I18N_SERVICE = "i18nService";
String BEAN_CLIENT_DETAILS_SERVICE = "europeanaClientDetailsService";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.europeana.api.translation.config;

import static eu.europeana.api.translation.service.google.GoogleTranslationServiceClientWrapper.MOCK_CLIENT_PROJ_ID;
import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
Expand All @@ -8,6 +9,7 @@
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
Expand All @@ -16,25 +18,35 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import eu.europeana.api.commons.config.i18n.I18nService;
import eu.europeana.api.commons.config.i18n.I18nServiceImpl;
import eu.europeana.api.commons.oauth2.service.impl.EuropeanaClientDetailsService;
import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException;
import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException;
import eu.europeana.api.translation.service.google.DummyGLangDetectService;
import eu.europeana.api.translation.service.google.DummyGTranslateService;
import eu.europeana.api.translation.service.google.GoogleLangDetectService;
import eu.europeana.api.translation.service.google.GoogleTranslationService;
import eu.europeana.api.translation.service.google.GoogleTranslationServiceClientWrapper;
import eu.europeana.api.translation.service.pangeanic.DummyPangLangDetectService;
import eu.europeana.api.translation.service.pangeanic.DummyPangTranslationService;
import eu.europeana.api.translation.service.pangeanic.PangeanicLangDetectService;
import eu.europeana.api.translation.service.pangeanic.PangeanicTranslationService;

@Configuration()
public class TranslationApiAutoconfig implements ApplicationListener<ApplicationStartedEvent>{
@PropertySource("classpath:translation.properties")
@PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true)
public class TranslationApiAutoconfig implements ApplicationListener<ApplicationStartedEvent> {

@Value("${translation.dummy.services:false}")
private boolean useDummyServices;

private final TranslationConfig translationConfig;
TranslationServiceProvider translationServiceConfigProvider;
private final Logger logger = LogManager.getLogger(TranslationApiAutoconfig.class);


public TranslationApiAutoconfig(@Autowired TranslationConfig translationConfig) {
this.translationConfig = translationConfig;
Expand All @@ -61,39 +73,61 @@ public MessageSource getMessageSource() {
messageSource.setDefaultLocale(Locale.ENGLISH);
return messageSource;
}

/**
* Creates a new client wrapper that can send translation requests to Google Cloud Translate. Note that
* the client needs to be closed when it's not used anymore
* @throws IOException
* Creates a new client wrapper that can send translation requests to Google Cloud Translate. Note
* that the client needs to be closed when it's not used anymore
*
* @throws IOException
*/
@Bean(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER)
public GoogleTranslationServiceClientWrapper getGoogleTranslationServiceClientWrapper() throws IOException {
return new GoogleTranslationServiceClientWrapper(translationConfig.getGoogleTranslateProjectId(), translationConfig.useGoogleHttpClient());
public GoogleTranslationServiceClientWrapper getGoogleTranslationServiceClientWrapper()
throws IOException {
return new GoogleTranslationServiceClientWrapper(
translationConfig.getGoogleTranslateProjectId(), translationConfig.useGoogleHttpClient());
}

@Bean(BeanNames.BEAN_PANGEANIC_LANG_DETECT_SERVICE)
public PangeanicLangDetectService getPangeanicLangDetectService() {
return new PangeanicLangDetectService(translationConfig.getPangeanicDetectEndpoint());
if (useDummyServices) {
return new DummyPangLangDetectService();
} else {
return new PangeanicLangDetectService(translationConfig.getPangeanicDetectEndpoint());
}
}

@Bean(BeanNames.BEAN_PANGEANIC_TRANSLATION_SERVICE)
public PangeanicTranslationService getPangeanicTranslationService(
@Qualifier(BeanNames.BEAN_PANGEANIC_LANG_DETECT_SERVICE) PangeanicLangDetectService pangeanicLangDetectService) {
return new PangeanicTranslationService(translationConfig.getPangeanicTranslateEndpoint(),
pangeanicLangDetectService);
if (useDummyServices) {
return new DummyPangTranslationService();
} else {
return new PangeanicTranslationService(translationConfig.getPangeanicTranslateEndpoint(),
pangeanicLangDetectService);
}
}

@Bean(BeanNames.BEAN_GOOGLE_LANG_DETECT_SERVICE)
public GoogleLangDetectService getGoogleLangDetectService(
@Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper googleTranslationServiceClientWrapper) {
return new GoogleLangDetectService(translationConfig.getGoogleTranslateProjectId(), googleTranslationServiceClientWrapper);
if (useDummyServices) {
return new DummyGLangDetectService(MOCK_CLIENT_PROJ_ID,
googleTranslationServiceClientWrapper);
} else {
return new GoogleLangDetectService(translationConfig.getGoogleTranslateProjectId(),
googleTranslationServiceClientWrapper);
}
}

@Bean(BeanNames.BEAN_GOOGLE_TRANSLATION_SERVICE)
public GoogleTranslationService getGoogleTranslationService(
@Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper googleTranslationServiceClientWrapper) {
return new GoogleTranslationService(translationConfig.getGoogleTranslateProjectId(), googleTranslationServiceClientWrapper);
if (useDummyServices) {
return new DummyGTranslateService(MOCK_CLIENT_PROJ_ID, googleTranslationServiceClientWrapper);
} else {
return new GoogleTranslationService(translationConfig.getGoogleTranslateProjectId(),
googleTranslationServiceClientWrapper);
}
}

@Bean(BeanNames.BEAN_SERVICE_PROVIDER)
Expand All @@ -113,34 +147,41 @@ public void onApplicationEvent(ApplicationStartedEvent event) {
if (logger.isDebugEnabled()) {
printRegisteredBeans(event.getApplicationContext());
}


// load either normal or dummy services (used for stress testing)
loadServices(event);
}

private void loadServices(ApplicationStartedEvent event) {
try {
// verify required configurations for initialization of translation services
verifyMandatoryProperties(event.getApplicationContext());

// init translation services
initTranslationServices(event.getApplicationContext());
} catch (Exception e) {
// gracefully stop the application in case of configuration problems (code 1 means exception
// occured at startup)
logger.fatal(
"Stopping application. Translation Service initialization failed due to configuration errors!",
e);
System.exit(SpringApplication.exit(event.getApplicationContext(), () -> 1));
}
} catch (Exception e) {
// gracefully stop the application in case of configuration problems (code 1 means exception
// occured at startup)
logger.fatal(
"Stopping application. Translation Service initialization failed due to configuration errors!",
e);
System.exit(SpringApplication.exit(event.getApplicationContext(), () -> 1));
}
}

public void initTranslationServices(ApplicationContext ctx) throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException {
TranslationServiceProvider translationServiceProvider =
(TranslationServiceProvider) ctx.getBean(BeanNames.BEAN_SERVICE_PROVIDER);
translationServiceProvider.initTranslationServicesConfiguration();

public void initTranslationServices(ApplicationContext ctx)
throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException {
TranslationServiceProvider translationServiceProvider =
(TranslationServiceProvider) ctx.getBean(BeanNames.BEAN_SERVICE_PROVIDER);
translationServiceProvider.initTranslationServicesConfiguration();
}

public void verifyMandatoryProperties(ApplicationContext ctx) {
TranslationConfig translationConfig = (TranslationConfig) ctx.getBean(BeanNames.BEAN_TRANSLATION_CONFIG);
translationConfig.verifyRequiredProperties();
TranslationConfig translationConfig =
(TranslationConfig) ctx.getBean(BeanNames.BEAN_TRANSLATION_CONFIG);
translationConfig.verifyRequiredProperties();
}

private void printRegisteredBeans(ApplicationContext ctx) {
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
Expand Down
Loading

0 comments on commit c351926

Please sign in to comment.