Google Caliper: Trouble getting started (Java Benchmarking) - java

I am trying to use Google Caliper to benchmark some simple code. I am using the examples from their websites. Here's what I've done so far:
Downloaded the Caliper JAR and added it to my Netbeans project
After having difficulties, I downloaded JUnit.jar and hamcrest.jar. Still not working.
Here's my code:
import com.google.caliper.Benchmark;
public class Benchmark1 extends Benchmark {
public void timeNanoTime(int reps) {
for (int i = 0; i < reps; i++) {
System.nanoTime();
}
}
}
I am extending Benchmark because when I try to extend "SimpleBenchmark" like on their website it tells me it cannot find SimpleBenchmark. I then, in my main method, create a new Benchmark1() hoping something will happen. Nothing does. This is the code inside my main class.
Benchmark1 test = new Benchmark1();
test.timeNanoTime(10);
I know this is no doubt a simple error but I cannot, despite much Googling, figure out where I'm going wrong. The code compiles but does not run.
EDIT: I should say I'm running Netbeans on Windows 7 with Caliper 1.0

It's true; the documentation is woefully outdated and incomplete. I'm working on it. In the meantime, here's what will get your benchmark running.
Your main method should delegate to CaliperMain, not directly to the benchmark. Try
public static void main(String[] args) {
CaliperMain.main(Benchmark1.class, args);
}
Windows will be a problem. Particularly, issue 215 will be the biggest blocker.

You could switch over to Perfidix http://perfidix.org/
Perfidix has eclipse Integration and can be used like JUnit.
Another option would be JUnitbenchmarks http://labs.carrotsearch.com/junit-benchmarks.html
It's a really great framework for Junit 4+. It can even build html charts to compare results.

Related

Visual studio code not showing output, java programming

I'm learning to code Java...
I just installed the Java runtime environment and Visual Studio Code, and wrote this:
public class IterationDemo
{
public static void main(String[] args)
{
System.out.println('x');
}
}
However, I don't see any output, both under output or terminal. Only kind of a rectangle (▯) under terminal.
I've tried to find a solution googling, stack and so on but no answer yet...
what can I do?
Thanks!
OK; got solved on its own - I ended up clearing the cache and then the output came :)
Thanks myself!

Programmatically refreshing a file in an eclipse Java application

I have the following program which adds a method to itself when run. But I have to refresh it every time using the F5 button or the refresh option.
Is there a way I could code the refresh in the program itself so that it refreshes itself after the modification? The project I am working on is a Java application and not an eclipse plugin so as far as I know the refreshLocal() method can't be used.
public class Demo {
public static void main(String[] args) throws IOException, CoreException {
File file = new File("/home/kishan/workspace/Roast/src/Demo.java");
if (file.exists()) {
JavaClassSource javaClass = Roaster.parse(JavaClassSource.class,
file);
javaClass.addMethod().setPublic().setStatic(true)
.setName("newMethod").setReturnTypeVoid()
.setBody("System.out.println(\"newMethod created\");")
.addParameter("String[]", "stringArray");
FileWriter writer = new FileWriter(file);
writer.write(javaClass.toString());
writer.flush();
writer.close();
}
}
}
I have tried using the refreshLocal() method defined in the eclipse JDT but since my project is a Java application the ResourcePlugin.getWorkspace() method does not work giving me a "workspace closed" error. Any suggestion is appreciated.
You see, eclipse runs your Java class within its own dedicated JVM. Thus there is no direct programmatic way of enforcing a refresh within eclipse.
You could check this older question; maybe that could lead to a reasonable workarounds.
On the other hand you might step back and ask yourself why exactly you want to achieve that. Your workflow simply doesn't make much sense when looking at it; as in: when generating code that way, shouldn't that generated code better go in its own specific place?
If you intend to "generate" code frequently to then continue to use it in eclipse; well, that somehow smells like a strange idea.
Eclipse has "Refresh using native hooks or polling" which might might help.
You can find it under Window > Prefrences > General > Workspace.
See On Eclipse, what does "Preferences -> General -> Workspace -> Refresh using native hooks or polling" do?

How to use processing sketch in a web app

Good day, i have a Processing sketch that i want to use in a web application
i am using jsp and servlets in my web app with tomcat as a server. I am using netbeans and i tried using < applet > tag but i can't get it to work, please help.
CODE:
import processing.core.*;
public class MyProcessingSketch extends PApplet {
public static void main(String args[]) {
PApplet.main(new String[] { "MyProcessingSketch" });
}
public void setup() {
}
#Override
public void draw() {
background (200,0,0);
}
public void settings(){
size(600,240);
}
public void mousePressed(){
exit();
}
}
Applets are not really supported anymore... But you might try p5js. Your HTML page would look like this:
<html>
<script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.6/p5.js"></script>
<script>
function setup() {
createCanvas(600, 240);
background(200,0,0);
}
function draw() {
// ...
}
</script>
Like the other answer says, applets are pretty much dead. They currently require you to have a paid signed certificate or for your users to change their security settings. And even then they show a bunch of scary warning dialogs, and it's just a pain in the neck for everybody. Chrome has dropped support for applets, and they'll be deprecated in the next version of Java.
If you're using eclipse, you've got three options:
Deploy as a runnable jar.
Deploy as a packaged executable.
Deploy using webstart.
None of these are embedding an applet in a webpage.
However, if you're using the Processing editor, you can use Processing.js to write the same Processing code but have it deployed as JavaScript, which you can embed in a webpage. Processing.js does the translation for you, so you don't have to change your code into JavaScript code.
You can also use p5.js, but that will require you to completely rewrite your syntax into JavaScript syntax.
In either case, you'll no longer be able to use Java libraries in your code. You'll have to find a JavaScript library that does the same things and use that instead. If you really need to use the Java libraries, then you have to go with deploying using one of the first three options.

How to use the method getHealth() in Bukkit using Heroes

Basically I have started updating a lot of Heroes spells to 1.7.2 and this update broke the .getHealth() and .getMaxHealth(). I am trying to fix it but I do not know how to. If anyone has some advice or samples I will be in debt. I will place some code where I use the .getHealth() method.
this is the link of the error: http://puu.sh/7BrEP.png. It is saying this method is ambigous for that type.
public void tickHero(Hero hero) {
if ( hero.getPlayer().getHealth() - damage > 1) {
addSpellTarget(hero.getPlayer(), plugin.getCharacterManager().getHero(caster));
damageEntity(hero.getPlayer(), caster, damage, DamageCause.MAGIC);
//hero.getPlayer().damage(damage, caster);
}
}
As of 1.7.2, there are two getHealth() and getMaxHealth() methods. This is becaue of the way Bukkit handled Minecraft changing the way entity health is stored in 1.6. You can read more about this here.
If you aren't using any NMS code, you should use the bukkit.jar in your build path as opposed to craftbukkit.jar. This should resolve your issue easily enough.
If you do need NMS code, you need to have both bukkit.jar AND craftbukkit.jar in your build path. Furthermore, you have to have bukkit.jar above craftbukkit.jar in the build path for it to work.

Missing methods on JXTA 2.7

I have started reading how to use JXTA from Practical JXTA II for an application i want to make. Although the code provided (examples) doesn't work with JXTA 2.7 . More specifically
Tools class of Z_Tools_And_Others doesn't compile since TheRendezVous class doesn't have getConnectedPeers() and getConnectedRendezVous() which exist in 2.6 . Therefor i cant continue since Tools class is used in most of the examples . Anyone familiar with this got any suggestions ? Is it better to use 2.6 for learning purposes and then move to 2.7 ?
Thanks.
Sorry for the late reply: the files for Practical JXTA II are available from here.
I am pretty sure you are trying to use 2.6 code with 2.7. Let me know if you still encounter an issue with the practical jxta II examples.
package Examples.Z_Tools_And_Others;
public class Tools {
public static void popConnectedRendezvous(RendezVousService TheRendezVous, String Name) {
Enumeration<ID> TheList = TheRendezVous.getConnectedRendezVous();
int Count = 0;
while (TheList.hasMoreElements()) {
Count = Count + 1;
PopInformationMessage(Name, "Connected to rendezvous:\n\n"
+ TheList.nextElement().toString());
}
if (Count==0) {
PopInformationMessage(Name, "No rendezvous connected to this rendezvous!");
}
}
TheList isnt working .The method getConnectedRendezVous is removed in 2.7 . The one that is replacing it is returning a List .That creates more errors later . Should i change the code so that it works for a List ?I thought i could use a listIterator to replace .nextElement() Same problem exists with popConnectedPeers(RendezVousService TheRendezVous, String Name) of same class.
Also line 166 in
package Examples.K_Service;
public class _710_Astrology_Service_Example implements Service, Runnable
Result.setCompat(StdPeerGroup.STD_COMPAT);
STD_COMPAT is removed . I replaced it with this :
ModuleImplAdvertisement ad =StdPeerGroup.getDefaultModuleImplAdvertisement();
Result.setCompat(ad.getCompat());
No idea if its good or not..code compiles tho :P

Categories