How to write JMH benchmarks in Kotlin without using melix/jmh-gradle-plugin? - kotlin

Kotlin benchmarks using plugin are written and run. As far as I understand, jmh is focused on writing benchmarks, and then building a jar application, and launching the application itself. I would like to know if it will be possible to use the power of jmh for use by analogy with unit tests, just like that part of the project that runs as a build task.
I use gradle for my kotlin projects. I expect to call benchmarks with a command like gradle lib:runBenchmark. The me.champeau.jmh plugin seems redundant to me, as there were legends (for example) that it is enough just to create a gradle task of the JavaExec type and run the org.openjdk.jmh.Main class passing certain parameters to it.
So far, I have managed to run benchmarks written in java in a similar way. But jmh stubbornly refuses to start seeing annotated classes written in kotlin.

Related

Differences in writing Kotlin/JVM and Kotlin/JS?

I read the Big Nerd Ranch guide to Kotlin and it talked in several places about Kotlin/Java interop, but never JS or native. I already had a solid background in Java, so I have gotten used to using Java classes in my Kotlin code. I am trying to write a Kotlin program which will be run on a site where most - if not all - functionality is written in JavaScript, and I am trying to understand how to write my code to make sure that it is interoperable. Will I be able to continue using Java classes in my Kotlin/JS code? What are the differences between writing Kotlin/JVM code and Kotlin/JS code? What should a (ex-) java programmer know when learning to interop with JS using Kotlin? If there are a few chapters on this in any good books written in the recent past, that would be helpful also.
As Steve already mentioned, you can't utilise java classes in Kotlin/JS.
Think of Kotlin/JS as Typescript.
It provides a different syntax to write code that ultimately compiles to JS.
Here are the notable differences of writing Kotlin/JS code vs Kotlin/JVM code
Kotlin/JS internally uses yarn for dependency management. This enables you to depend on any javascript module available on npmjs etc (see note below)
In addition to standard library, you can also leverage other kotlin-first frameworks such as kotlinx-serialization, ktor etc
Testing libraries will be JS specific. So instead of mockito / mockk / junit family, you'll need to get familiar with karma / mocha family.
There will be a difference in Coroutine capabilities - both in terms of the way one writes code and performance expectations.
I found reading about Kotlin Multiplatform helped clarify a lot about the capabilities of kotlin.
I know this was not specifically asked, but giving my 2cents to people considering Kotlin/JS (as of Sep'20)
Great if you're familiar with Kotlin and don't foresee too many third party dependencies apart from http i/o (ktor) , React ( kotlin-react) and basic html / css (covered by kotlin-styled).
Using JS modules as dependencies is not as straight forward as using JVM dependencies since there is no ready-made interop. One has to define javascript functions/classes in kotlin before using them (see here). So if you foresee leveraging a lot of existing javascript modules, it won't be an ideal way forward.
Great if you have a typical backend-frontend model where backend compiles to JVM and Frontend compiles to JS. You can leverage a common data model and http i/o framework across Backend and Frontend code (via Kotlin Multiplatform). I've found this to be a tremendous productivity boost!
Kotlin/JS compiles Kotlin code, including its own standard library, into Javascript code. At the end, that's all you have is Javascript. What you don't have is any connection to the Java Virtual Machine. Kotlin's standard library provides no magic to bridge Javascript code to the JVM so that it can utilize Java classes. So NO, you can't utilize Java classes in standard Kotlin/JS.

Where do I put tests in an fsharp project using Monodevelop?

I am doing an assignment (implementing an algorithm), and I wanted to try, this time, implementing tests first. However, I simply do not know where to put them! Do I need to create a new project?
All the tutorials I have found mentioned what to write, but not the general method of proceeding when building a test suite.
I expected Monodevelop to have some kind of predefined structure (like a big "add test suite" button), but I could not find anything for FSharp.
Monodevelop seems to have many tools to deal with tests in a clean and principled way (it does have a big "run tests" button), therefore I thought I would structure my project so that Monodevelop "sees" my tests, so that I could use the tools from the graphical interface. It seems the most common way to write tests is to use NUnit, what if I use something else, like FsCheck?
I have stumbled upon a Github project called FSharpKoans, it seems to suggest that I should create a project called "MySolution.Test" inside a solution, is this the standard way?
What should be the type of the project then, is it a separate console application?
Thanks.
Yes, creating a separate "X.Test" project is the standard way of adding tests in .NET, F# is no exception here.
For testing framework, pick one that Monodevelop supports well if what you're looking for is IDE integration - NUnit sounds like a safe choice. You could conceivably use any framework in F# tests, so I would think what Monodevelop supports should dictate your choice here.
FsCheck is not a testing framework, it's a library for property-based testing that can be used in conjunction with any testing framework. You might want to look into it as it advocates a particularly interesting approach to testing, but it's by no means the only way or the required way of doing testing in F#.

Can javascript generated from Kotlin sources be used back in JVM?

Is it possible to use js generated from Kotlin sources in JVM to manipulate java objects and its own js objects?
Kinda create dynamic environment for development in distributed environment.
So new versions of classes can be dynamically loaded/modified on running remote servers while development. And then after it's done release versions to be compiled statically for max performance.
How hard would it be to implement?
The current implementation of Kotlin does not have any support for invoking Java methods from JS-generated code. This could in theory be accomplished using Nashorn, but we (the Kotlin team) do not currently have any plans to work on that. If you're interested, you're welcome to contribute, but this would be a fairly long and difficult project.

How to test gradle build script

Is there some kind of tool for testing gradle scripts? Like JUnit is for testing Java code.
I would like to test my build.gradle script in way to see if it works properly, but would not like to do changes to the development repository (like commiting somethnig, adding test tags and so on).
Is there a way to do such thing, in lets call it, "transaction" manner, as if to do changes, see if it is doing as I intended and then rollback it.
Or do you recommend to mock my repository and all other needed data (something like staging only for my build script testing) and then do my things there?
What is the best practice for doing this?
Please take into consideration that I am gradle newbie. :)
Thanks in advance for any kind of advice/help.
mismas
There is no single purpose tool for this. OTOH there are various possibilities how to test your code. If your build logic is in buildSrc project - http://www.gradle.org/docs/current/userguide/organizing_build_logic.html#sec:build_sources - you can put your testing code there. Of course you have to decide how to test your code to avoid interaction like repository changes.
You can take a look at Gradle codebase. There are plenty of integration tests where you can find ideas. Aside from unittest it is likely you will want to use Gradle Tooling API to control build started from your tests.
More recent versions of Gradle ship with TestKit.

How to build/test Scala without IDE dependence?

I'm well into learning Scala now and enjoying it very much; I hope to start future projects in it, rather than Java. What I'm enjoying less is the (relatively) poor IDE support. I've found both IDEA and Eclipse with the Scala Plugin (including nightly builds) to be a bit unreliable or difficult to use - I want something I can always depend on. E.g. yesterday I couldn't get a fresh install of eclipse+plugin to run my tests at all, or even open an editor window!
I'm considering hopping between Eclipse/IDEA depending on which suits the task at hand best and more importantly cutting my dependence on the IDE for building and running tests (ScalaTest). This is non-trivial for me since I've grown up on Java in Eclipse; leaving Eclipse SVN to use GIT was initially a big deal. Given that I only have time to learn one tool, should it be Ant, Maven, buildr, sbt,.... ? How do other people work?
I have used both Maven and sbt with Scala and found both of them pretty easy to use.
However, sbt feel much more closer to Scala as its build files are written in Scala itself (as opposed to XML in Maven) and sbt feature a build REPL, has continuous compilation and testing etc.
So I would advise you to use sbt for a simple Scala project.
But in case you want to create standard Java projects like WAR, EJB etc, I feel like Maven has a better support for them.
Also Maven has an enormous plugin ecosystem which enables you to do virtually everything, code coverage, reporting, code standard checking, documentation generation, and a lot more.
I'd use SBT with IDEA. Though I haven't tried it, I know there's some support for integration of SBT and IDEA.
Anyway, SBT is a great basis for all Scala building&testing needs.
EDIT: Uuuups. Sorry. Actually I misread your question and only commented on IDEs. When it comes to building the project I use make ;) because it is well integrated into Vim
while I wouldn't want to do ANY Java Project without IDE, I'm currently doing a fairly large project just in Vim. I know that's a quite a "stone-age approach" but it works just fine.
On one hand I was fed up by the buggy/slow/lacking Scala support of all IDEs. I tried Scala, netbeans and IDEA and found working with them (in Scala) rather painful.
On the other hand Scala has some properties that help when working with a simple Editor: you can have more classes in one file so I usually define a whole package in one file which again doesn't grow too large, since Scala class are usually very (or even extremely) small. So I usually have only two or three files open at a time and hence don't need a package/file management.
I couldn't get code folding by languages tags to work but folding by indentation works just fine if you stick to Scala's indentation conventions.
One thing that doesn't work is auto-completion. But then again, this makes me write more loosely coupled objects ;)
Buildr supports Scala and Java as first class languages, with support for ScalaCheck and ScalaSpecs, and of course also has enough plugins to make it a good contender for Maven.
Just adding my two cents. I think you should give Netbeans a try too. Its supposed to be the most Scala-friendly among the three major players (Eclipse, Idea and Netbeans).
I have been having a lot of fun with Netbeans 6.8 and scala-2.8.0.r22602-b20100720020114
It is fair to say that I wouldn't be programming Scala now if it hadn't been for the Netbeans plugin. I have spent a fair bit of time trying to get it to work as smoothly with the others, but without much success.
I use Netbeans for developing and Ant for standalone build. Info on configuring Ant for Scala here:
http://scriptlandia.blogspot.com/2007/04/how-to-compile-and-run-scala-program.html