org.sakaiproject.nakamura.lite.soak.mongo.CreateUsersAndGroupsSoak.java Source code

Java tutorial

Introduction

Here is the source code for org.sakaiproject.nakamura.lite.soak.mongo.CreateUsersAndGroupsSoak.java

Source

/*
 * Licensed to the Sakai Foundation (SF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.sakaiproject.nakamura.lite.soak.mongo;

import java.io.IOException;
import java.util.Map;

import org.sakaiproject.nakamura.api.lite.ClientPoolException;
import org.sakaiproject.nakamura.api.lite.Configuration;
import org.sakaiproject.nakamura.api.lite.StorageClientException;
import org.sakaiproject.nakamura.api.lite.StorageClientUtils;
import org.sakaiproject.nakamura.api.lite.accesscontrol.AccessDeniedException;
import org.sakaiproject.nakamura.lite.ConfigurationImpl;
import org.sakaiproject.nakamura.lite.soak.AbstractSoakController;
import org.sakaiproject.nakamura.lite.soak.authorizable.CreateUsersAndGroupsClient;
import org.sakaiproject.nakamura.lite.storage.StorageClientPool;
import org.sakaiproject.nakamura.lite.storage.mongo.MongoClientPool;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

public class CreateUsersAndGroupsSoak extends AbstractSoakController {
    private int totalUsers;
    private StorageClientPool clientPool;
    private Configuration configuration;

    public CreateUsersAndGroupsSoak(int totalUsers, StorageClientPool clientPool, Configuration configuration) {
        super(totalUsers);
        this.clientPool = clientPool;
        this.configuration = configuration;
        this.totalUsers = totalUsers;
    }

    protected Runnable getRunnable(int nthreads)
            throws ClientPoolException, StorageClientException, AccessDeniedException {
        int usersPerThread = totalUsers / nthreads;
        return new CreateUsersAndGroupsClient(usersPerThread, clientPool, configuration);
    }

    public static void main(String[] argv) throws ClientPoolException, StorageClientException,
            AccessDeniedException, ClassNotFoundException, IOException {

        int totalUsers = 1000;
        int nthreads = 10;

        if (argv.length > 0) {
            nthreads = StorageClientUtils.getSetting(Integer.valueOf(argv[0]), nthreads);
        }
        if (argv.length > 1) {
            totalUsers = StorageClientUtils.getSetting(Integer.valueOf(argv[1]), totalUsers);
        }
        ConfigurationImpl configuration = new ConfigurationImpl();
        Map<String, Object> properties = Maps.newHashMap();
        properties.put("keyspace", "n");
        properties.put("acl-column-family", "ac");
        properties.put("authorizable-column-family", "au");
        properties.put("content-column-family", "cn");
        configuration.activate(properties);

        CreateUsersAndGroupsSoak createUsersAndGroupsSoak = new CreateUsersAndGroupsSoak(totalUsers,
                getClientPool(configuration), configuration);
        createUsersAndGroupsSoak.launchSoak(nthreads);
    }

    protected static StorageClientPool getClientPool(Configuration configuration) throws ClassNotFoundException {
        MongoClientPool clientPool = new MongoClientPool();
        try {
            clientPool.bindConfiguration(configuration);
            clientPool.activate(ImmutableMap.of(MongoClientPool.PROP_MONGO_URI,
                    (Object) "mongodb://127.0.0.1/?maxpoolsize=1000", MongoClientPool.PROP_MONGO_DB,
                    (Object) "smc_soaktests"));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return clientPool;
    }
}