Skip to content

Commit

Permalink
Introduced new Maven module: Insights.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 16, 2015
1 parent e5d3127 commit bb84706
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 22 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -85,6 +85,7 @@
<module>rapidoid-arr</module>
<module>rapidoid-dates</module>
<module>rapidoid-log</module>
<module>rapidoid-insights</module>
<module>rapidoid-var</module>
<module>rapidoid-anyobj</module>
<module>rapidoid-cls</module>
Expand Down
33 changes: 33 additions & 0 deletions rapidoid-insights/pom.xml
@@ -0,0 +1,33 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid</artifactId>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>rapidoid-insights</artifactId>
<packaging>jar</packaging>
<description>Rapidoid Insights</description>

<dependencies>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-u</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-log</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-test-commons</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,63 @@
package org.rapidoid;

import org.rapidoid.log.Log;

/*
* #%L
* rapidoid-insights
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

/**
* @author Nikolche Mihajlovski
* @since 4.1.0
*/
public abstract class AbstractInsightful implements Insightful {

private final String kind;

private final String name;

private final Thread creatorThread;

public AbstractInsightful(String kind, String name) {
this.creatorThread = Thread.currentThread();
this.kind = kind;
this.name = name;

Log.info("Creating object", "kind", kind, "name", name, "creatorThread", creatorThread.getName(), "class",
getClass().getSimpleName());

Insights.register(this);
}

@Override
public String getKind() {
return kind;
}

@Override
public String getName() {
return name;
}

@Override
public Thread getCreatorThread() {
return creatorThread;
}

}
35 changes: 35 additions & 0 deletions rapidoid-insights/src/main/java/org/rapidoid/Insightful.java
@@ -0,0 +1,35 @@
package org.rapidoid;

/*
* #%L
* rapidoid-insights
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

/**
* @author Nikolche Mihajlovski
* @since 4.1.0
*/
public interface Insightful {

String getKind();

String getName();

Thread getCreatorThread();

}
58 changes: 58 additions & 0 deletions rapidoid-insights/src/main/java/org/rapidoid/Insights.java
@@ -0,0 +1,58 @@
package org.rapidoid;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.rapidoid.util.U;

/*
* #%L
* rapidoid-insights
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

/**
* @author Nikolche Mihajlovski
* @since 4.1.0
*/
public class Insights {

private static final Map<String, List<Insightful>> RESOURCES = U.autoExpandingMap(ArrayList.class);

public static void register(Insightful resource) {
RESOURCES.get(resource.getKind()).add(resource);
}

public static String getInfo() {
return RESOURCES.toString();
}

public static String getCpuMemStats() {
Runtime rt = Runtime.getRuntime();

long totalMem = rt.totalMemory();
long maxMem = rt.maxMemory();
long freeMem = rt.freeMemory();
long usedMem = totalMem - freeMem;
int megs = 1024 * 1024;

String msg = "MEM [total=%s MB, used=%s MB, max=%s MB]";
return String.format(msg, totalMem / megs, usedMem / megs, maxMem / megs);
}

}
54 changes: 54 additions & 0 deletions rapidoid-insights/src/main/java/org/rapidoid/InsightsThread.java
@@ -0,0 +1,54 @@
package org.rapidoid;

import org.rapidoid.log.Log;
import org.rapidoid.util.U;

/*
* #%L
* rapidoid-insights
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

/**
* @author Nikolche Mihajlovski
* @since 4.1.0
*/
public class InsightsThread extends Thread {

public InsightsThread() {
super("stats");
}

private String lastStats;

@Override
public void run() {
Log.info("Starting Insights thread...");

while (!Thread.interrupted()) {
U.sleep(1000);
String stats = Insights.getCpuMemStats() + "\n" + Insights.getInfo();
if (!stats.equals(lastStats)) {
System.out.println(stats);
lastStats = stats;
}
}

Log.info("Stopped Insights thread.");
}

}
@@ -1,8 +1,8 @@
package org.rapidoid.measure;

import org.rapidoid.Insights;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.util.UTILS;

/*
* #%L
Expand Down Expand Up @@ -42,7 +42,7 @@ public void run() {
while (true) {
String info = statistics.info();
if (!lastInfo.equals(info)) {
System.out.println(UTILS.getCpuMemStats() + " " + info);
System.out.println(Insights.getCpuMemStats() + " " + info);
lastInfo = info;
}
Thread.sleep(1000);
Expand Down
7 changes: 1 addition & 6 deletions rapidoid-pool/pom.xml
Expand Up @@ -14,12 +14,7 @@
<dependencies>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-u</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-log</artifactId>
<artifactId>rapidoid-insights</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion rapidoid-u/src/main/java/org/rapidoid/lambda/Mapper.java
Expand Up @@ -2,7 +2,7 @@

/*
* #%L
* rapidoid-lambda
* rapidoid-u
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
Expand Down
15 changes: 2 additions & 13 deletions rapidoid-utils/src/main/java/org/rapidoid/util/UTILS.java
Expand Up @@ -48,6 +48,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.rapidoid.Insights;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
Expand Down Expand Up @@ -524,7 +525,7 @@ public static void benchmarkComplete(String name, int count, long startTime) {

String data = String.format("%s: %s in %s ms (%s/sec)", name, count, ms, avgs);

System.out.println(data + " | " + getCpuMemStats());
System.out.println(data + " | " + Insights.getCpuMemStats());
}

public static void benchmarkMT(int threadsN, final String name, final int count, final CountDownLatch outsideLatch,
Expand Down Expand Up @@ -560,18 +561,6 @@ public static void benchmarkMT(int threadsN, final String name, final int count,
benchmarkMT(threadsN, name, count, null, runnable);
}

public static String getCpuMemStats() {
Runtime rt = Runtime.getRuntime();
long totalMem = rt.totalMemory();
long maxMem = rt.maxMemory();
long freeMem = rt.freeMemory();
long usedMem = totalMem - freeMem;
int megs = 1024 * 1024;

String msg = "MEM [total=%s MB, used=%s MB, max=%s MB]";
return String.format(msg, totalMem / megs, usedMem / megs, maxMem / megs);
}

public static String urlDecode(String value) {
try {
return URLDecoder.decode(value, "UTF-8");
Expand Down

0 comments on commit bb84706

Please sign in to comment.