How to use Caliper for benchmarking? - java

I am trying to figure out how to use Caliper to do benchmark testing in Eclipse and I am getting nowhere. I tried following the 26 minute tutorial found here: https://code.google.com/p/caliper/ but I get lost quickly. I have downloaded the Caliper jar file but I'm not sure what folder it should be in. I've also downloaded Maven for Eclipse plugin but I'm not even sure if that is necessary. Is it possible to install Caliper from the 'Install New Software..' option in the Help menu in Eclipse? I just want to be able to do very simple speed tests for some of the algorithms I've created for a Data Structures and Algorithms class I am taking.

This answer is now obsolete. Caliper has worked in Windows for more than a year, at least: https://code.google.com/p/caliper/issues/detail?id=167
Caliper doesn't work in Windows. See this case. You need to use version 0.5-rc1, which has other issues but is still pretty okay and is missing a lot of features, but it does work in Windows.
If you know how to use Maven, add this pom snippet to your pom.xml.
<dependency>
<groupId>com.google.caliper</groupId>
<artifactId>caliper</artifactId>
<version>0.5-rc1</version>
</dependency>
If you want to learn maven, first read this: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Convert your project to a maven project (Right click on project -> Configure -> Convert to Maven Project)
If you don't know how to use Maven (here is a guide to how to do this with pictures):
Download the 0.5-rc1 jar
Right click on the project you want to use and choose Build Path -> Configure Build Path
Add it to your libraries tab using Add External Jar
Once you've done that, you can start writing benchmarks. Here is an example of a benchmark I wrote for a different Stack Overflow question.

Here is how you set up a working Caliper class using the latest version of Caliper as of this writing, caliper-1.0-beta2 . As far as I can tell this procedure isn't documented anywhere outside of inline comments in the Caliper code files.
First install caliper-1.0-beta2 in pom.xml or by downloading the jar file. Then make a file like this:
import com.google.caliper.Benchmark;
import com.google.caliper.runner.CaliperMain;
public class DemoBenchmark {
public static void main(String[] args) {
CaliperMain.main(DemoBenchmark.class, args);
}
#Benchmark
void timeStringBuilder(int reps) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < reps; i++) {
sb.setLength(0);
sb.append("hello world");
}
}
}
Run that file, and Caliper will do the benchmark for you.

I have tried all the suggested solutions on Windows, however without success.
I always encountered the following error:
This selection yields 4 experiments.
ERROR: Trial failed to complete (its results will not be included in the run):
The worker exited without producing data. It has likely crashed. Inspect C:\Users\Piotr\AppData\Local\Temp\1583760443321-0\trial-1.log to see any worker output.
I was able to solve it by adding the #VmOptions annotation to a custom benchmark class.
Here is the full configuration:
#VmOptions("-XX:-TieredCompilation")
public class CaliperBenchmark {
#BeforeExperiment
void setUp() {
// set up
}
#Benchmark
void boxed() {
// test
}
}
Maven: com.google.caliper caliper 1.0-beta-2 compile
Main class:
public class Main {
public static void main(String[] args) {
CaliperMain.main(CaliperBenchmark.class, args);
}
}
Command line: mvn exec:java -Dexec.mainClass="com.google.caliper.runner.CaliperMain" -Dexec.args="org.CaliperBenchmark"

Related

Java Error in SizeOf library

I saw this link which uses Instrumentation to calculate the size of objects during runtime. I decided to try this library since It can be really helpful in determining the size of big data structures.
So I wrote the following code in a new project named TrySizeOf using NetBeans IDE:
package trysizeof;
import net.sourceforge.sizeof.SizeOf;
public class TrySizeOf {
public static void main(String[] args) {
String s = "abc";
System.out.println(SizeOf.deepSizeOf(s));
}
}
After that I created a folder named lib inside my project, and placed SizeOf.jar in it. Then under Project->Properties->Run I placed the following parameter:
-javaagent:/home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
However, when I attempt to run my project I get the following error:
Error occurred during initialization of VM
Error opening zip file or JAR manifest missing : /home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
agent library failed to init: instrument
Can any one help me with this? or maybe explain what did I do wrong?
UPDATE:
I found the error, it was a problem with the upper and lower cases. When I started the project for the first time I made a little upper and lower case typo. When I paid attention to it I fixed it, but I kept getting the same error (However the path was fixed). Now, I tried using clean and build and the project worked. It was that when I do a clean and build manifest file gets updated which is used to specify the path of premain method. When I tried clean and build the file got updated and the project finally worked. Thanks for the comment about a typo in my post, which somehow made me think about this.

How to get maven to compile a .java file that uses com.apple.eawt.Application

From a different question here I found a code snippet to handle the open file event in my java app. However, even though I'm building my app in a Mac OS X environment and my IDE can find the com.aple.eawt.* classes (in rt.jar in the Oracle SDK I'm using), maven does not find them. I've looked for clues about what I need to do in pom.xml to hint to maven about the classpath, but really the advice seems to boil down to:
1) Add whatever jar you need into a project repo
2) or use bad form and link to the specific jar.
Since I'm trying to build this app in a way that it will still run on other platforms, I'd like to solve this without having to bundle the whole Apple-specific runtime into the build.
Thanks to Tunaki's link above, the answer is really that simple (I'd tried adding the Apple jar to my pom and that didn't work).
<dependency>
<groupId>com.yuvimasory</groupId>
<artifactId>orange-extensions</artifactId>
<version>1.3.0</version>
</dependency>
Further, in my main class, my code had to look a little different, since the orange-extension version of OpenFilesEvent.getFiles() returns a collection of Object rather than of File:
if (System.getProperty("os.name").contains("OS X")){
com.apple.eawt.Application a = com.apple.eawt.Application.getApplication();
a.setOpenFileHandler(new com.apple.eawt.OpenFilesHandler() {
#Override
public void openFiles(com.apple.eawt.AppEvent.OpenFilesEvent e) {
for (Object oFile : e.getFiles()){
if (oFile instanceof File) {
File file = (File) oFile;
// do the actual open logic here
} else {
__l.warn("The OS just told us to open a file but handed us "+oFile.toString());
}
}
}
});

Multiple main-methods for testing purposes in Netbeans 7.4 (project from Netbeans 7.2.1)

I recently switched from my older Netbeans version 7.2.1 to 7.4. I am working on a bigger project which uses only one main-entry point of course. However, for testing purposes I am using a second class which also contains a main-method. In my older Netbeans version I was able to Shift+F6 (Run File) and it did what it says: It runs the file because if has a valid main-method. With the never version of the IDE the program keeps telling me, that there is no main-method. This main-method is anything but special and the autocheck does not warn me either (Why wouldn't it? It is totally valid and worked in version 7.2.1).
Here is my testing class definition for the sake of completeness:
package Tests;
// various imports from surrounding project or external packages
public class TEST001 {
// variables and methods for further testing
public static void main(String[] args) throws IOException{
// [...]
}
}
Now, are there incompabilities between projects of Netbeans 7.2.1 to these of version 7.4 which might have caused this?
Or do I have to check a special option somewhere to allow the handling of multiple main-entry points? Which seems unlikely because running a file instead of the project seems to be permanent feature with its own user controls.
Or is this simply a bug?
Thank you for your suggestions.

Intellij IDEA 12 Can not compile simple code

I'm new to Java, I'm trying to compile the simplest code: hello world,
package com.ninet.first;
public class Test {
public static void main(String[] param) {
System.out.println("test");
}
}
when i run it, it says:
COMPILATION COMPLETED WITH 1 ERROR 0 WARNINGS
ERROR: FAILED TO CREATE A SELECTOR
no class is created.
i have followed couple video tutorials how to use intellij, did everything as in the
tutorials, tried to re install IDE many times, no changes!!
Project SDK are set, I'm using 1.6
By the way i have no problems in Eclipse.
I can also compile and run my classes in command line.
Take a look at IDEA-98407, does that solve your problem? Quote:
Then you'll have to turn off Settings | Compiler | Use external build.
Please check this Knowledge Base document for the networking related issues.
Also check my answer here.
Compiler works in a separate process, hence the requirement for the network connection for the IPC, however it's not normal that your firewall is blocking connections on local interface (127.0.0.1) by default.

Can't compile an application using Eclipse, Maven, and the Android Plugin for Maven

I'm trying to create an Android application in Eclipse using the Maven plugin and the m2eclipse-android-plugin as well. Things were going "ok" until recently. I'm using Helios on Ubuntu and have the latest JDK (removed the default one installed by Ubuntu).
The project references two libraries that I've also created. One is an Android specific utility project and generates the .apklib (successfully). The other library is a more general purpose set of utilities not specific to Android which produces a JAR file. Both of these projects are also built using the Maven plugin for Eclipse. In addition, I've verified that both the .apklib and .jar files are in the local repository and both included all of the generated class files as would be expected.
When it goes to build the .apk file, I'm getting a "cannot find symbol" on a class in my Android project where the symbol is a class from the non-Android utility JAR file. For some completely bizarre reason, the class file cannot be found inside the JAR file. I verified that, in fact, the JAR file is in my local maven repository and that the class file is in the JAR file. I've also run the maven install command with debugging on, copied the command line that gets fed into the Java compiler. When I execute that command in a console, I receive the SAME error (indicating that it's a Java compiler error and not a Maven error).
Has anyone else run into this type of situation before? It's extraordinarily strange and I've completely combed the command line for potential issues and, best as I can tell, everything seems correct.
Well, through what appears to be trial and error I seem to have fixed the problem. I had a file that looked "similar" to this:
import Test.TestObserver;
import com.myself.ImportedClassThatCouldntBeFound;
class Test extends ImportedClassThatCouldntBeFound {
public interface TestObserver {
public void event ();
}
public void addObserver (TestObserver observer) {
...
}
}
public class AnotherTest {
private Test test = new Test ();
public void blah () {
this.test.addObserver (new TestObserver () {
public void event () {
...
}
});
}
}
The problem happened at the TOP of the file. For some reason, Eclipse imported the inner interface!
When I "REMOVED" that import, and then changed AnotherTest to:
public class AnotherTest {
private Test test = new Test ();
public void blah () {
this.test.addObserver (new Test.TestObserver () {
public void event () {
...
}
});
}
}
it compiled correctly! I even verified it by putting the import BACK into the file and removing the fully declared interface name and it caused it to fail again! It's definitely one of the craziest compiler issues I've ever seen and once I get back the FOUR HOURS of my life that I lost researching this, I'll do more investigation into why this is occurring.
This will be the first time I do this on StackOverflow, but I'm going to mark this as the solution because it most definitely was the issue. However, it definitely requires more research (at least on my part) to try and understand what was causing the compiler to become so confused.
edited this to make it apparent that the class that had the inner interface was extending the class that could not be found when compiled
To me, it looks like a problem caused (ultimately) by putting two top-level classes into a single source code file. This is generally thought to be bad practice.
It is not clear whether the compilation error is mandated by the JLS, whether it is a bug in the Java compiler. But either way, the best fix is to not put multiple classes into one source file.

Categories