Java Code Examples for com.google.common.base.Stopwatch#elapsed()

The following examples show how to use com.google.common.base.Stopwatch#elapsed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AggregateEngineRunner.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
@Override
public EngineStats run() {
	LOGGER.info("Running Aggregate Engine {}", description.getEngineName());
	Stopwatch sw = Stopwatch.createStarted();
	List<EngineStats> childStats = new ArrayList<>();
	for(EngineRunner child:children) {
		childStats.add(child.run());
	}
	sw.stop();
	
	return new EngineStats(
			description.getEngineName(), 
			sw.elapsed(TimeUnit.MILLISECONDS),
			childStats
		);
}
 
Example 2
Source File: TraceCollector.java    From glowroot with Apache License 2.0 6 votes vote down vote up
Trace getPartialTrace(int timeout, TimeUnit unit) throws InterruptedException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (stopwatch.elapsed(unit) < timeout) {
        for (Trace trace : traces) {
            if (trace.getHeader().getPartial()) {
                return trace;
            }
        }
        MILLISECONDS.sleep(10);
    }
    if (traces.isEmpty()) {
        throw new IllegalStateException("No trace was collected");
    } else {
        throw new IllegalStateException("Trace was collected but is not partial");
    }
}
 
Example 3
Source File: CFTagRecommender.java    From TagRec with GNU Affero General Public License v3.0 6 votes vote down vote up
private static List<Map<Integer, Double>> startBM25CreationForTagPrediction(BookmarkReader reader, int sampleSize, boolean userBased, boolean resBased, int beta, boolean ignoreResource) {
	int size = reader.getBookmarks().size();
	int trainSize = size - sampleSize;
	Stopwatch timer = new Stopwatch();
	timer.start();
	CFTagRecommender calculator = new CFTagRecommender(reader, trainSize, userBased, resBased, beta);
	timer.stop();
	long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);
	
	List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
	timer.reset();
	timer.start();
	for (int i = trainSize; i < size; i++) {
		Bookmark data = reader.getBookmarks().get(i);
		Map<Integer, Double> map = null;
		int resID = (ignoreResource ? -1 : data.getResourceID());
		map = calculator.getRankedTagList(data.getUserID(), resID, true);
		results.add(map);
		//System.out.println(data.getTags() + "|" + map.keySet());
	}
	timer.stop();
	long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
	
	timeString = PerformanceMeasurement.addTimeMeasurement(timeString, true, trainingTime, testTime, sampleSize);
	return results;
}
 
Example 4
Source File: EventLoopCloseable.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws Exception {
  try {
    Stopwatch watch = Stopwatch.createStarted();
    // this takes 1s to complete
    // known issue: https://github.com/netty/netty/issues/2545
    eventLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS);
    eventLoop.terminationFuture().sync();

    long elapsed = watch.elapsed(MILLISECONDS);
    if (elapsed > 1200) {
      logger.info("closed eventLoopGroups in " + elapsed + " ms");
    }
  } catch (final InterruptedException e) {
    logger.warn("Failure while shutting down bootstrap context event loops.", e);

    // Preserve evidence that the interruption occurred so that code higher up on the call stack can learn of the
    // interruption and respond to it if it wants to.
    Thread.currentThread().interrupt();
  }
}
 
Example 5
Source File: CheckUtil.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
private static <R, T extends DataObject> R readData(final DataBroker dataBroker, final LogicalDatastoreType ldt,
        final InstanceIdentifier<T> iid, final Function<T, R> function, final int timeout)
        throws InterruptedException, ExecutionException {
    AssertionError lastError = null;
    final Stopwatch sw = Stopwatch.createStarted();
    do {
        try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
            final Optional<T> data = tx.read(ldt, iid).get();
            if (data.isPresent()) {
                try {
                    return function.apply(data.get());
                } catch (final AssertionError e) {
                    lastError = e;
                    Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
                }
            }
        }
    } while (sw.elapsed(TimeUnit.SECONDS) <= timeout);
    throw lastError;
}
 
Example 6
Source File: LazyPlatformMBeanServer.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void waitForContainerToCreatePlatformMBeanServer() throws Exception {
    synchronized (platformMBeanServerAvailability) {
        if (platformMBeanServerAvailable) {
            return;
        }
        Stopwatch stopwatch = Stopwatch.createStarted();
        // looping to guard against "spurious wakeup" from Object.wait()
        while (!platformMBeanServerAvailable) {
            long remaining = SECONDS.toMillis(60) - stopwatch.elapsed(MILLISECONDS);
            if (remaining < 1000) {
                // less that one second remaining
                break;
            }
            platformMBeanServerAvailability.wait(remaining);
        }
        if (!platformMBeanServerAvailable) {
            logger.error("platform mbean server was never created by container");
        }
    }
}
 
Example 7
Source File: MPResourceCalculator.java    From TagRec with GNU Affero General Public License v3.0 5 votes vote down vote up
public static BookmarkReader predictPopularResources(String filename, int trainSize, boolean writeTime) {
	Timer timerThread = new Timer();
	MemoryThread memoryThread = new MemoryThread();
	timerThread.schedule(memoryThread, 0, MemoryThread.TIME_SPAN);
	
	BookmarkReader reader = new BookmarkReader(trainSize, false);
	reader.readFile(filename);
	Stopwatch timer = new Stopwatch();
	timer.start();
	
	List<int[]> values = getPopularResources(reader, 20, trainSize);
	
	timer.stop();
	long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);
	timer.reset();
	timer.start();
	PredictionFileWriter writer = new PredictionFileWriter(reader, values);
	writer.writeResourcePredictionsToFile(filename + "_mp", trainSize, 0);
	timer.stop();
	long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
	timeString = PerformanceMeasurement.addTimeMeasurement(timeString, true, trainingTime, testTime, reader.getBookmarks().size() - trainSize);
	
	timeString = PerformanceMeasurement.addMemoryMeasurement(timeString, false, memoryThread.getMaxMemory());
	timerThread.cancel();
	if (writeTime) {
		Utilities.writeStringToFile("./data/metrics/" + filename + "_mp_TIME.txt", timeString);
	}
	return reader;
}
 
Example 8
Source File: SchemaUpgradeIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
static void updateSchemaWithRetry(com.datastax.driver.core.Session wrappedSession,
        String query) throws InterruptedException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (stopwatch.elapsed(SECONDS) < 60) {
        try {
            wrappedSession.execute(query);
            return;
        } catch (NoHostAvailableException e) {
        }
        SECONDS.sleep(1);
    }
    // try one last time and let exception bubble up
    wrappedSession.execute(query);
}
 
Example 9
Source File: TraceCollector.java    From glowroot with Apache License 2.0 5 votes vote down vote up
Trace getCompletedTrace(@Nullable String transactionType, @Nullable String transactionName,
        int timeout, TimeUnit unit) throws InterruptedException {
    if (transactionName != null) {
        checkNotNull(transactionType);
    }
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (stopwatch.elapsed(unit) < timeout) {
        for (Trace trace : traces) {
            if (!trace.getHeader().getPartial()
                    && (transactionType == null
                            || trace.getHeader().getTransactionType().equals(transactionType))
                    && (transactionName == null || trace.getHeader().getTransactionName()
                            .equals(transactionName))) {
                return trace;
            }
        }
        MILLISECONDS.sleep(10);
    }
    if (transactionName != null) {
        throw new IllegalStateException("No trace was collected for transaction type \""
                + transactionType + "\" and transaction name \"" + transactionName + "\"");
    } else if (transactionType != null) {
        throw new IllegalStateException(
                "No trace was collected for transaction type: " + transactionType);
    } else {
        throw new IllegalStateException("No trace was collected");
    }
}
 
Example 10
Source File: ParallelGZIPOutputStreamTest.java    From parallelgzip with Apache License 2.0 5 votes vote down vote up
private void testThreads(@Nonnull ByteArrayOutputBuffer out, @Nonnegative int nthreads) throws Exception {
    Random r = new Random();

    ThreadPoolExecutor executor = ParallelGZIPEnvironment.newThreadPoolExecutor(nthreads);
    try {
        for (int i = 0; i < 3; i++) {
            out.reset();
            // The randomness throws the perf results off a bit, but fuzzes the block sizes.
            byte[] data = new byte[256 * 1024 * 1024 + r.nextInt(1048576)];
            r.nextBytes(data);
            LOG.info("Data is " + data.length + " bytes.");
            {
                Stopwatch stopwatch = Stopwatch.createStarted();
                ParallelGZIPOutputStream gzip = new ParallelGZIPOutputStream(out, executor);
                gzip.write(data);
                gzip.close();
                long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
                LOG.info("nthreads=" + nthreads + "; parallel=" + elapsed);
                gzip = null;
            }
            ParallelGZIPInputStream in = new ParallelGZIPInputStream(out.toInput());
            byte[] copy = ByteStreams.toByteArray(in);
            assertArrayEquals(data, copy);
        }
    } finally {
        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);
    }
}
 
Example 11
Source File: PCCMockCommon.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory,
        final String localAddress) {
    final Stopwatch sw = Stopwatch.createStarted();
    TestingSessionListener listener;
    final InetAddress address = InetAddresses.forString(localAddress);
    while (sw.elapsed(TimeUnit.SECONDS) <= 60) {
        listener = factory.getSessionListenerByRemoteAddress(address);
        if (listener == null) {
            Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
        } else {
            return listener;
        }
    }
    throw new NullPointerException();
}
 
Example 12
Source File: SolverLoadTest.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoad() {
  RealMatrix symmetric = randomSymmetricMatrix(500);
  Stopwatch stopwatch = new Stopwatch().start();
  int iterations = 100;
  for (int i = 0; i < iterations; i++) {
    MatrixUtils.getSolver(symmetric);
  }
  stopwatch.stop();
  long elapsedMS = stopwatch.elapsed(TimeUnit.MILLISECONDS);
  log.info("{}ms elapsed", elapsedMS);
  assertTrue(elapsedMS < 300 * iterations);
}
 
Example 13
Source File: TaskLauncher.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
private TaskResultImpl getTaskResult(TaskContext context, Stopwatch taskStopwatchForFailures,
        SchedulerException exception) {
    taskLogger.getErrorSink().println(exception.getMessage());

    Map<String, byte[]> serializedVariables = extractVariablesFromContext(context);

    long elapsedTime = 0;
    if (taskStopwatchForFailures != null) {
        elapsedTime = taskStopwatchForFailures.elapsed(TimeUnit.MILLISECONDS);
    }
    TaskResultImpl result = new TaskResultImpl(taskId, exception, taskLogger.getLogs(), elapsedTime);

    result.setPropagatedVariables(serializedVariables);
    return result;
}
 
Example 14
Source File: FqNewsController.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
/**
* 查询FqNews首页
*/
@RequestMapping("collect")
@ResponseBody
public Object collect(@RequestParam(required = false) Integer loop) {
    BaseResult baseResult = new BaseResult();
    Stopwatch stopwatch = Stopwatch.createStarted();

    String result = null;
    int loopSize = 10,loopCount = 10,loopIndex = 0;
    if(loop != null){
        loopCount = loop;
    }
    String url = "https://3g.163.com/touch/reconstruct/article/list/BBM54PGAwangning/";
    String suffix = "-10.html";
    try {
        Date now = new Date();
        while (loopIndex < loopCount){
            String realUrl = url + loopSize*loopIndex+suffix;
            result = HttpClientUtil.getWebPage(realUrl);
            int index = result.indexOf(":");
            result = result.substring(index + 1, result.lastIndexOf("]") + 1);
            Console.log(result);
            List<NewsResponse> newsResponseList = JSON.parseArray(result, NewsResponse.class);
            for (NewsResponse newsResponse : newsResponseList) {
                if(newsResponse.getCommentCount() < 1000){
                    continue;
                }
                if(StringUtils.isEmpty(newsResponse.getUrl())){
                    continue;
                }
                String result2 = HttpClientUtil.getWebPage(newsResponse.getUrl());
                result2 = result2.substring(result2.indexOf("<article"), result2.lastIndexOf("</article>") + 10);
                result2 = result2.substring(0, result2.lastIndexOf("<div class=\"footer\">"));
                result2 += "</article>";
                result2 = result2.replaceAll("data-src","src");
                FqNews fqNews = new FqNews(newsResponse);
                fqNews.setGmtCreate(now);
                fqNews.setCommentCount(0);
                fqNews.setContent(result2);
                fqNewsService.insert(fqNews);
            }
            loopIndex++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    stopwatch.stop();
    long seconds = stopwatch.elapsed(TimeUnit.SECONDS);
    logger.info("新闻更新完毕,耗时{}秒",seconds);
    return baseResult;
}
 
Example 15
Source File: ImmutableRTreeTest.java    From bytebuffer-collections with Apache License 2.0 4 votes vote down vote up
public void showBenchmarksBoundWithLimits()
{
  //final int start = 1;
  final int start = 10000000;
  final int factor = 10;
  final int end = 10000000;
  //final int end = 10;

  for (int numPoints = start; numPoints <= end; numPoints *= factor) {
    try {
      BitmapFactory bf = new ConciseBitmapFactory();
      RTree tree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bf), bf);

      Stopwatch stopwatch = Stopwatch.createStarted();
      Random rand = new Random();
      for (int i = 0; i < numPoints; i++) {
        tree.insert(new float[]{(float) (rand.nextDouble() * 100), (float) (rand.nextDouble() * 100)}, i);
      }
      long stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);
      System.out.printf("[%,d]: insert = %,d ms%n", numPoints, stop);

      stopwatch.reset().start();
      ImmutableRTree searchTree = ImmutableRTree.newImmutableFromMutable(tree);
      stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);
      System.out.printf("[%,d]: size = %,d bytes%n", numPoints, searchTree.toBytes().length);
      System.out.printf("[%,d]: buildImmutable = %,d ms%n", numPoints, stop);

      stopwatch.reset().start();

      Iterable<ImmutableBitmap> points = searchTree.search(
          new RectangularBound(
              new float[]{40, 40},
              new float[]{60, 60},
              100
          )
      );

      Iterables.size(points);
      stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);

      System.out.printf("[%,d]: search = %,dms%n", numPoints, stop);

      stopwatch.reset().start();

      ImmutableBitmap finalSet = bf.union(points);

      stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);
      System.out.printf("[%,d]: union of %,d points in %,d ms%n", numPoints, finalSet.size(), stop);
    }
    catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
}
 
Example 16
Source File: BenchmarkSSTableWrite.java    From bboxdb with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws StorageManagerException, RejectedException, InterruptedException, BBoxDBException {

		final List<Long> elapsedBenchmarks = new ArrayList<>();

		final TupleStoreManagerRegistry storageRegistry = new TupleStoreManagerRegistry();
		storageRegistry.init();

		// Delete the old table
		storageRegistry.deleteTable(TEST_RELATION);

		// Create a new table
		final TupleStoreConfiguration tupleStoreConfiguration = TupleStoreConfigurationBuilder.create().build();
		storageRegistry.createTable(TEST_RELATION, tupleStoreConfiguration);

		// Assure table is created successfully
		final TupleStoreManager storageManager = storageRegistry.getTupleStoreManager(TEST_RELATION);

		for(int iter = 0; iter < 100; iter++) {
			final Stopwatch watch = Stopwatch.createStarted();

			int MAX_TUPLES = 100000;
			int SPECIAL_TUPLE = MAX_TUPLES / 2;

			for(int i = 0; i < MAX_TUPLES; i++) {
				final double d1 = ThreadLocalRandom.current().nextDouble();
				final double d2 = ThreadLocalRandom.current().nextDouble();
				final double d3 = ThreadLocalRandom.current().nextDouble();

				final Hyperrectangle hyperrectangle = new Hyperrectangle(d1, d1 + 10.0, d2, d2 + 10.0, d3, d3 + 10.0);

				final Tuple createdTuple = new Tuple(Integer.toString(i), hyperrectangle, Integer.toString(i).getBytes());
				storageManager.put(createdTuple);

				if(i == SPECIAL_TUPLE) {
					storageManager.delete(Integer.toString(SPECIAL_TUPLE), createdTuple.getVersionTimestamp() + 1);
				}
			}

			final long elapsed = watch.elapsed(TimeUnit.MILLISECONDS);
			elapsedBenchmarks.add(elapsed);

			System.out.format("Iteartion %d, Elapsed: %d%n", iter, elapsed);
		}

		final long max = elapsedBenchmarks.stream().mapToLong(l -> l).max().orElse(0);
		final long min = elapsedBenchmarks.stream().mapToLong(l -> l).min().orElse(0);
		final double avg = elapsedBenchmarks.stream().mapToLong(l -> l).average().orElse(0);

		System.out.format("Max %d, Min %d, Avg %f%n", max, min, avg);
	}
 
Example 17
Source File: N4JSBuildTypeTrackingBuilder.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected IProject[] build(int kind, @SuppressWarnings("rawtypes") Map args, IProgressMonitor monitor)
		throws CoreException {
	Stopwatch stopwatch = Stopwatch.createStarted();
	IProject project = getProject();
	try {
		RaceDetectionHelper.log("About to build %s", project);

		SubMonitor builderMonitor = toBuilderMonitor(monitor, 1100);

		/*
		 * Make sure that announced changes to the libraries have been actually processed
		 */
		externalLibraryBuildJobProvider.buildExternalProjectsNow(builderMonitor.split(50));

		IProject[] result = super.build(kind, args, builderMonitor.split(1000, SubMonitor.SUPPRESS_SETTASKNAME));
		/*
		 * Here we suffer from a race between the builder and the listener to project changes. The listener is
		 * supposed to update the project description on change. The build needs to return the references as
		 * configured in the project description.
		 *
		 * We cannot sync the builder with the listener due to limitations of the eclipse resource model thus we
		 * will bypass this mechanism and obtain the dependencies directly from the project.
		 *
		 * Dynamic references have been superseded in Eclipse Photon anyways :(
		 */
		List<IProject> dependencies = projectDependencyStrategy != null
				? projectDependencyStrategy.getProjectDependencies(project, true)
				: null;
		if (dependencies == null) {
			RaceDetectionHelper.log("Returning project results since dependencies cannot be determined");
			RaceDetectionHelper.log("%s depends on %s", project, Arrays.toString(result));
			return result;
		}
		/*
		 * And merge them with the static project references that are persisted to disc.
		 */
		IProject[] staticReferences = project.getDescription().getReferencedProjects();
		if (dependencies.isEmpty()) {
			RaceDetectionHelper.log("Returning static project results since dependencies are empty");
			RaceDetectionHelper.log("%s depends on %s", project, Arrays.toString(result));
			return staticReferences;
		}
		Set<IProject> asSet = Sets.newLinkedHashSet(FluentIterable.from(dependencies).append(staticReferences));
		result = asSet.toArray(new IProject[0]);
		RaceDetectionHelper.log("Returning computed results");
		RaceDetectionHelper.log("%s depends on %s", project, Arrays.toString(result));
		return result;
	} finally {
		stopwatch.stop();
		if (LOGGER.isDebugEnabled()) {
			final String msg = "Building " + project.getName() + " took " + stopwatch.elapsed(TimeUnit.SECONDS)
					+ " seconds";
			LOGGER.debug(msg);
		}
	}

}
 
Example 18
Source File: DownstreamServiceImpl.java    From glowroot with Apache License 2.0 4 votes vote down vote up
private AgentResponse runOnCluster(String agentId, CentralRequest centralRequest)
        throws Exception {
    int timeoutSeconds;
    switch (centralRequest.getMessageCase()) {
        case HEADER_REQUEST:
        case ENTRIES_REQUEST:
        case MAIN_THREAD_PROFILE_REQUEST:
        case AUX_THREAD_PROFILE_REQUEST:
        case FULL_TRACE_REQUEST:
            timeoutSeconds = 5;
            break;
        case HEAP_DUMP_REQUEST:
            timeoutSeconds = 300;
            break;
        default:
            timeoutSeconds = 60;
    }
    // retry up to 5 seconds on shutting-down response to give agent time to reconnect to
    // another cluster node
    Stopwatch stopwatch = Stopwatch.createStarted();
    while (stopwatch.elapsed(SECONDS) < 5) {
        java.util.Optional<AgentResult> optional = connectedAgents.execute(agentId,
                timeoutSeconds, new SendDownstreamFunction(centralRequest, timeoutSeconds));
        if (!optional.isPresent()) {
            throw new AgentNotConnectedException();
        }
        AgentResult result = optional.get();
        Optional<AgentResponse> value = result.value();
        if (value.isPresent()) {
            AgentResponse response = value.get();
            if (response
                    .getMessageCase() == AgentResponse.MessageCase.UNKNOWN_REQUEST_RESPONSE) {
                throw new AgentUnsupportedOperationException();
            }
            if (response.getMessageCase() == AgentResponse.MessageCase.EXCEPTION_RESPONSE) {
                throw new AgentException();
            }
            return response;
        } else if (result.timeout()) {
            throw new TimeoutException();
        } else if (result.interrupted()) {
            // this should not happen
            throw new RuntimeException(
                    "Glowroot central thread was interrupted while waiting for agent response");
        }
        // only other case is shutting-down response
        checkState(result.shuttingDown());
        MILLISECONDS.sleep(100);
    }
    // received shutting-down response for 5+ seconds
    throw new AgentNotConnectedException();
}
 
Example 19
Source File: BLLCalculator.java    From TagRec with GNU Affero General Public License v3.0 4 votes vote down vote up
private static List<Map<Integer, Double>> startActCreation(BookmarkReader reader, int sampleSize, boolean sorting, boolean userBased, boolean resBased, double dVal,
		int beta, CalculationType cType, Double lambda) {
	int size = reader.getBookmarks().size();
	int trainSize = size - sampleSize;
	
	Stopwatch timer = new Stopwatch();
	timer.start();
	BLLCalculator calculator = new BLLCalculator(reader, trainSize, dVal, beta, userBased, resBased, cType, lambda);
	timer.stop();
	long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);
	List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
	if (trainSize == size) {
		trainSize = 0;
	}
	
	timer.reset();
	timer.start();
	// List<List<Bookmark>> userBookmarks = Utilities.getBookmarks(reader.getBookmarks().subList(0, trainSize), false); // only when using items from train-set
	for (int i = trainSize; i < size; i++) { // the test-set
		Bookmark data = reader.getBookmarks().get(i);
		int userID = data.getUserID();
		int resID = data.getResourceID();
		
		if (cType == CalculationType.MUSIC) {
			if (i > 0) {
				data = reader.getBookmarks().get(i - 1); // last item from test set
				//data = Utilities.getLastBookmarkOfUser(userBookmarks, userID); // last item of train-set
				if (data.getUserID() == userID) {
					resID = data.getResourceID();
				} else {
					resID = -1;
				}
			}
		}
		Map<Integer, Double> map = calculator.getRankedTagList(userID, resID, sorting, cType);
		results.add(map);
	}
	timer.stop();
	long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
	
	timeString = PerformanceMeasurement.addTimeMeasurement(timeString, true, trainingTime, testTime, sampleSize);
	return results;
}
 
Example 20
Source File: MalletCalculator.java    From TagRec with GNU Affero General Public License v3.0 4 votes vote down vote up
private static List<Map<Integer, Double>> startLdaCreation(BookmarkReader reader, int sampleSize, boolean sorting, int numTopics, boolean userBased, boolean resBased, boolean topicCreation) {
	int size = reader.getBookmarks().size();
	int trainSize = size - sampleSize;
	//int oldTrainSize = trainSize;
	
	Stopwatch timer = new Stopwatch();
	timer.start();
	MalletCalculator userCalc = null;
	List<Map<Integer, Integer>> userMaps = null;
	if (userBased) {
		userMaps = Utilities.getUserMaps(reader.getBookmarks().subList(0, trainSize));
		userCalc = new MalletCalculator(userMaps, numTopics);
		userCalc.predictValuesProbs(topicCreation);
		System.out.println("User-Training finished");
	}
	MalletCalculator resCalc = null;
	List<Map<Integer, Integer>> resMaps = null;
	if (resBased) {
		resMaps = Utilities.getResMaps(reader.getBookmarks().subList(0, trainSize));
		resCalc = new MalletCalculator(resMaps, numTopics);
		resCalc.predictValuesProbs(topicCreation);
		System.out.println("Res-Training finished");
	}
	List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
	if (topicCreation) {
		trainSize = 0;
	}
       timer.stop();
       long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);

	timer.reset();
	timer.start();
	int mpCount = 0;
	for (int i = trainSize; i < size; i++) { // the test set
		Bookmark data = reader.getBookmarks().get(i);
		int userID = data.getUserID();
		int resID = data.getResourceID();

		Map<Integer, Double> userPredMap = null;
		if (userCalc != null) {
			userPredMap = userCalc.getValueProbsForID(userID, topicCreation);
		}
		Map<Integer, Double> resPredMap = null;
		if (resCalc != null) {
			//if (i > oldTrainSize) {
			//	System.out.println("Test-Set");
			//}
			resPredMap = resCalc.getValueProbsForID(resID, topicCreation);
			if (topicCreation && resPredMap == null) {
				resPredMap = resCalc.getMostPopularTopics();
				mpCount++;
			}
		}
		Map<Integer, Double> map = getRankedTagList(reader, userPredMap, resPredMap, sorting);
		results.add(map);
	}
	timer.stop();
	long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
	
	timeString = PerformanceMeasurement.addTimeMeasurement(timeString, true, trainingTime, testTime, (topicCreation ? size : sampleSize));
	System.out.println("MpCount: " + mpCount);
	return results;
}