Search in sources :

Example 46 with StopWatch

use of org.springframework.util.StopWatch in project engine by craftercms.

the class GraphQLFactoryImpl method getInstance.

/**
 * {@inheritDoc}
 */
public GraphQL getInstance(SiteContext siteContext) {
    StopWatch watch = new StopWatch("GraphQL Schema");
    watch.start("Schema Build");
    GraphQLSchema schema = buildSchema(siteContext);
    watch.stop();
    if (logger.isTraceEnabled()) {
        logger.trace(watch.prettyPrint());
    }
    return GraphQL.newGraphQL(schema).build();
}
Also used : StopWatch(org.springframework.util.StopWatch)

Example 47 with StopWatch

use of org.springframework.util.StopWatch in project engine by craftercms.

the class ContentTypeBasedDataFetcher method doGet.

/**
 * {@inheritDoc}
 */
@Override
public Object doGet(final DataFetchingEnvironment env) {
    Field field = env.getMergedField().getSingleField();
    String fieldName = field.getName();
    // Get arguments for pagination & sorting
    int offset = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_OFFSET)).orElse(0);
    int limit = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_LIMIT)).orElse(defaultLimit);
    String sortBy = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_BY)).orElse(defaultSortField);
    String sortOrder = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_ORDER)).orElse(defaultSortOrder);
    List<String> queryFieldIncludes = new LinkedList<>();
    // Add content-type to includes, we might need it for a GraphQL TypeResolver
    queryFieldIncludes.add(QUERY_FIELD_NAME_CONTENT_TYPE);
    List<Map<String, Object>> items = new LinkedList<>();
    Map<String, Object> result = new HashMap<>(2);
    result.put(FIELD_NAME_ITEMS, items);
    // Setup the ES query
    SearchSourceBuilder source = new SearchSourceBuilder();
    BoolQueryBuilder query = boolQuery();
    source.query(query).from(offset).size(limit).sort(sortBy, SortOrder.fromString(sortOrder));
    StopWatch watch = new StopWatch(field.getName() + " - " + field.getAlias());
    watch.start("build filters");
    // Filter by the content-type
    switch(fieldName) {
        case FIELD_NAME_CONTENT_ITEMS:
            query.filter(existsQuery(QUERY_FIELD_NAME_CONTENT_TYPE));
            break;
        case FIELD_NAME_PAGES:
            query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_PAGE));
            break;
        case FIELD_NAME_COMPONENTS:
            query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_COMPONENT));
            break;
        default:
            // Get the content-type name from the field name
            query.filter(termQuery(QUERY_FIELD_NAME_CONTENT_TYPE, getOriginalName(fieldName)));
            break;
    }
    // Check the selected fields to build the ES query
    Optional<Field> itemsField = field.getSelectionSet().getSelections().stream().map(f -> (Field) f).filter(f -> f.getName().equals(FIELD_NAME_ITEMS)).findFirst();
    if (itemsField.isPresent()) {
        List<Selection> selections = itemsField.get().getSelectionSet().getSelections();
        selections.forEach(selection -> processSelection(StringUtils.EMPTY, selection, query, queryFieldIncludes, env));
    }
    // Only fetch the selected fields for better performance
    source.fetchSource(queryFieldIncludes.toArray(new String[0]), new String[0]);
    watch.stop();
    logger.debug("Executing query: {}", source);
    watch.start("searching items");
    SearchResponse response = elasticsearch.search(new SearchRequest().source(source));
    watch.stop();
    watch.start("processing items");
    result.put(FIELD_NAME_TOTAL, response.getHits().totalHits);
    if (response.getHits().totalHits > 0) {
        for (SearchHit hit : response.getHits().getHits()) {
            items.add(fixItems(hit.getSourceAsMap()));
        }
    }
    watch.stop();
    if (logger.isTraceEnabled()) {
        logger.trace(watch.prettyPrint());
    }
    return result;
}
Also used : ObjectValue(graphql.language.ObjectValue) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) LoggerFactory(org.slf4j.LoggerFactory) FragmentSpread(graphql.language.FragmentSpread) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) StringUtils(org.apache.commons.lang3.StringUtils) ElasticsearchWrapper(org.craftercms.search.elasticsearch.ElasticsearchWrapper) LinkedHashMap(java.util.LinkedHashMap) Selection(graphql.language.Selection) VariableReference(graphql.language.VariableReference) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) DataFetcher(graphql.schema.DataFetcher) LinkedList(java.util.LinkedList) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) MapUtils(org.apache.commons.collections.MapUtils) ObjectField(graphql.language.ObjectField) StopWatch(org.springframework.util.StopWatch) Field(graphql.language.Field) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Argument(graphql.language.Argument) List(java.util.List) StringValue(graphql.language.StringValue) ArrayValue(graphql.language.ArrayValue) IntValue(graphql.language.IntValue) SortOrder(org.elasticsearch.search.sort.SortOrder) Optional(java.util.Optional) FragmentDefinition(graphql.language.FragmentDefinition) Required(org.springframework.beans.factory.annotation.Required) InlineFragment(graphql.language.InlineFragment) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collections(java.util.Collections) SchemaUtils(org.craftercms.engine.graphql.SchemaUtils) BooleanValue(graphql.language.BooleanValue) SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Selection(graphql.language.Selection) LinkedList(java.util.LinkedList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) StopWatch(org.springframework.util.StopWatch) SearchResponse(org.elasticsearch.action.search.SearchResponse) ObjectField(graphql.language.ObjectField) Field(graphql.language.Field) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 48 with StopWatch

use of org.springframework.util.StopWatch in project gocd by gocd.

the class UserSqlMapDaoCachingTest method enabledUserCacheShouldBeThreadSafe.

@Test
@Timeout(60)
public void enabledUserCacheShouldBeThreadSafe() throws Exception {
    ThreadSafetyChecker threadSafetyChecker = new ThreadSafetyChecker(10000);
    threadSafetyChecker.addOperation(new ThreadSafetyChecker.Operation() {

        @Override
        public void execute(int runIndex) {
            StopWatch stopWatch = new StopWatch("enabledUserCount");
            stopWatch.start("enabledUserCount");
            userDao.enabledUserCount();
            stopWatch.stop();
        // System.out.println(stopWatch.shortSummary());
        }
    });
    threadSafetyChecker.addOperation(new ThreadSafetyChecker.Operation() {

        @Override
        public void execute(int runIndex) {
            StopWatch stopWatch = new StopWatch("saveOrUpdate");
            stopWatch.start("saveOrUpdate");
            userDao.saveOrUpdate(new User("some-random-user " + runIndex));
            stopWatch.stop();
        // System.out.println(stopWatch.shortSummary());
        }
    });
    threadSafetyChecker.addOperation(new ThreadSafetyChecker.Operation() {

        @Override
        public void execute(int runIndex) {
            StopWatch stopWatch = new StopWatch("enableUsers");
            stopWatch.start("enableUsers");
            userDao.enableUsers(Arrays.asList("some-random-user " + runIndex));
            stopWatch.stop();
        // System.out.println(stopWatch.shortSummary());
        }
    });
    threadSafetyChecker.run(250);
}
Also used : User(com.thoughtworks.go.domain.User) StopWatch(org.springframework.util.StopWatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 49 with StopWatch

use of org.springframework.util.StopWatch in project spring-framework by spring-projects.

the class PerformanceMonitorInterceptor method invokeUnderTrace.

@Override
protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable {
    String name = createInvocationTraceName(invocation);
    StopWatch stopWatch = new StopWatch(name);
    stopWatch.start(name);
    try {
        return invocation.proceed();
    } finally {
        stopWatch.stop();
        writeToLog(logger, stopWatch.shortSummary());
    }
}
Also used : StopWatch(org.springframework.util.StopWatch)

Example 50 with StopWatch

use of org.springframework.util.StopWatch in project spring-framework by spring-projects.

the class IntroductionBenchmarkTests method timeManyInvocations.

@Test
public void timeManyInvocations() {
    StopWatch sw = new StopWatch();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.setProxyTargetClass(false);
    pf.addAdvice(new SimpleCounterIntroduction());
    ITestBean proxy = (ITestBean) pf.getProxy();
    Counter counter = (Counter) proxy;
    sw.start(INVOCATIONS + " invocations on proxy, not hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        proxy.getAge();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on proxy, hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        counter.getCount();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on target");
    for (int i = 0; i < INVOCATIONS; i++) {
        target.getAge();
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StopWatch(org.springframework.util.StopWatch) Test(org.junit.jupiter.api.Test)

Aggregations

StopWatch (org.springframework.util.StopWatch)112 Test (org.junit.Test)44 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)12 ArrayList (java.util.ArrayList)9 Test (org.junit.jupiter.api.Test)9 ITestBean (org.springframework.tests.sample.beans.ITestBean)9 TestBean (org.springframework.tests.sample.beans.TestBean)9 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 List (java.util.List)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)6 ApplicationMap (com.navercorp.pinpoint.web.applicationmap.ApplicationMap)5 Ignore (org.junit.Ignore)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)5 Range (com.navercorp.pinpoint.web.vo.Range)4 Dataset (org.apache.jena.query.Dataset)4