Reading Environment Variables in BenchmarkDotNet - benchmarkdotnet

I'm trying to load values from environment variables when running a BenchmarkDotNet project from the command line. I'm passing my environment variables using --envVars key:value. However, they're not being picked up by the benchmark.
Here's some sample code:
using BenchmarkDotNet.Attributes;
[MemoryDiagnoser]
[ReturnValueValidator(failOnError: true)]
public class RedisBenchmark
{
[GlobalSetup]
public void GlobalSetup()
{
_redisService.Servers = System.Environment.GetEnvironmentVariable(Constants.REDIS_SERVERS) ?? "";
if (string.IsNullOrEmpty(_redisService.Servers)) _redisService.Servers = "NO SERVERS SPECIFIED";
}
private static RedisService _redisService = new();
[Benchmark(Description = "Write and Read to Redis")]
public void WriteAndReadRedis() => _redisService.WriteAndRead();
}
Notice the "NO SERVERS SPECIFIED" string above. When my RedisService runs and outputs its connection string as the benchmark warms up, it outputs that string, so I know that GlobalSetup ran, but that it wasn't able to find or read the environment variable.
Is there something I'm missing?
Some background and discussion here as well:
https://github.com/dotnet/BenchmarkDotNet/issues/2156

As of this writing, there is a bug affecting the [ReturnValueValidator] and it looks like it skips running GlobalSetup. As a temporary fix you can remove the [ReturnValueValidator] and it should work.

Related

Getting a FileNotFoundException in VSCode, but not in JGrasp

Ok, so this is what's going on. I'm trying to learn how to use vscode (switching over from jgrasp). I'm trying to run this old school assignment that requires the use of outside .txt files. The .txt files, as well as other classes that I have written are in the same folder and everything. When I try to run this program in JGrasp, it works fine. Though, in VSCode, I get an exception. Not sure what is going wrong here. Thanks Here is an example:
import java.io.*;
public class HangmanMain {
public static final String DICTIONARY_FILE = "dictionary.txt";
public static final boolean SHOW_COUNT = true; // show # of choices left
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Welcome to the cse143 hangman game.");
System.out.println();
// open the dictionary file and read dictionary into an ArrayList
Scanner input = new Scanner(new File(DICTIONARY_FILE));
List<String> dictionary = new ArrayList<String>();
while (input.hasNext()) {
dictionary.add(input.next().toLowerCase());
}
// set basic parameters
Scanner console = new Scanner(System.in);
System.out.print("What length word do you want to use? ");
int length = console.nextInt();
System.out.print("How many wrong answers allowed? ");
int max = console.nextInt();
System.out.println();
//The rest of the program is not shown. This was included just so you guys could see a little bit of it.
If you're not using a project, jGRASP makes the working directory for your program the same one that contains the source file. You are creating the file with a relative path, so it is assumed to be in the working directory. You can print new File(DICTIONARY_FILE).getAbsolutePath() to see where VSCode is looking (probably a separate "classes" directory) and move your data file there, or use an absolute path.

completion Candidates for positional parameter in picocli

I'm trying to provide completion for positional parameters.
Somewhere I found note that they are not very well supported, but currently I'm not able to find exact place in spec and I'm not sure what that really means.
In meantime I found CompletionCandidatesTest.java in sources which would suggest that they're supported in some fashion or at least prepared to support it.
That's why I would like to know if they work and if yes what I'm doing wrong.
Currently my code in Groovy looks like this:
package com.some.package
import picocli.CommandLine
import picocli.CommandLine.Command
#Command
class TjTest implements Runnable {
static class TjTestCandidates implements Iterable<String> {
#Override
Iterator<String> iterator() {
return Arrays.asList("aaaa", "bbbb", "cccc", "dddd", "eeeee", "ffff").iterator()
}
}
#CommandLine.Option(names = "-x", completionCandidates = TjTestCandidates)
String x;
#CommandLine.Parameters(completionCandidates = TjTestCandidates)
String param;
#Override
public void run() {
println "Start"
println x
println param;
println "Stop"
}
public static void main(String[] args) {
CommandLine.run(new TjTest(), args);
}
}
I performed required bash commands like this:
java -cp "picocli-3.9.5.jar;tj.jar" picocli.AutoComplete -f -n tjtest com.some.package.TjTest
. tjtest_completion
It works like a charm for an option. Unfortunately I was not able to make it work for parameter.
I was also trying to:
remove #Option and leave only #Parameters
put index in parameter
Your code looks fine. The current state (picocli 3.9.5) is that positional parameter completion works in JLine, but not in bash/zsh.
There is an outstanding todo item to fix this. Someone contributed a pull request to address this but it had an issue and has not been merged.
Contributions are welcome!

redissonClient.poll() only returning the first 8 characters of String type value

Currently using reddison, creating a redissonClient and trying to poll data from redis server. I can see the data in the redis db if I check via redis-cli but when I look at the string value in my java application it is always the first 8 characters of the string and no more. Not sure why it won't give me the whole value.
I've tried using the .peek() method as well and I see the same symptom in that I only get 8 characters of the string returned.
Here is the main part of the code I can provide more details as needed:
#Service
#Slf4j
public class RedisConsumer {
RedisConfig redisConfig;
//RQueue<String> redisQueue;
RBlockingQueue<String> redisQueue;
#Autowired
RedisConsumer(RedisConfig redisConfig) {
this.redisConfig = redisConfig;
}
public void pollAuditQueue() {
//Redisson
redisQueue.add("{JSON string here snipped out for brevity}");
String item = redisQueue.poll();
if (!Objects.isNull(item)) {
log.info("I found this item: " + item);
} else {
log.info("Nothing in queue...");
}
}
#PostConstruct
private void init() throws Exception {
RedissonClient redissonClient = redisConfig.redisson();
redisQueue = redissonClient.getBlockingQueue("test");
while(true) {
pollAuditQueue();
Thread.sleep(5000);
}
}
When I look at the print statement in my console I see:
I found this item: {"AuditEv
When I check the redis-cli I can see the whole value:
1) "\xfc\t{\"AuditEvent\":{\"timestamp\":\"2018-11-27 04:31:47.818000+0000\" snipped the rest out for brevity}"
Lastly if I check that the item was removed from Redis after being polled in the Java app I can confirm that it is.
Any help would be great since it's not throwing any specific error I'm not finding any resources online to help address it.
I've found one thing I didn't notice in my earlier testing. When I manually insert using the redis cli I was replicating what my first tests through Java did which put the \xfc\t at the front which can be seen in my sample above.
Just now when I used redisQueue.add from within my application I noticed in redis it has \xfc\x80\x90\x01 instead and those do return the entire string to me in my application. I assume then this has to do with memory allocation somehow? I'm marking the question as resolved as I am no longer experiencing the issue. If anyone can drop on comment on what those letter/numbers mean though it may be meaningful for anyone that reads this post later. Once I have researched it I will add that comment myself if no one has beat me to it!
Add encoding:
RMap map = redisson.getMap("SessionMap"); -->
RMap map = redisson.getMap("SessionMap", new StringCodec("UTF-8"));

intellij hotswap doesn't work: adding one local variable

I am using OracleJVM with Intellij remote debugging. I am not doing any DCEVM fancy stuff. My code:
public static String test() {
String data; //new code
if (some condition){
//...
data = "abc"; //new code
//...
}
}
After making the change, recompile the class and verifying 'hotswap' finished successfully, the static method is re-ran but debugger variables window says
Cannot find local variable 'data'

Do we need to create process() inside a new annotator?

Im creating an annotator called "NewAnnotator" and try to make it works in a pipeline with others annotators in ClearTK like:
SentenceAnnotator, PosTaggerAnnotator, etc. So I want to be able to run pipeline:
aggregate.add(SentenceAnnotator.getDescription());
aggregate.add(PosTaggerAnnotator.getDescription());
aggregate.add(NewAnnotator.getDescription());
// run the classification pipeline on the new texts
SimplePipeline.runPipeline(reader, aggregate.createAggregateDescription());
I wrote the code with no error, but when running it returns a lot of errors, which I think from this part in my NewAnnotator code:
public static AnalysisEngineDescription getDescription() throws ResourceInitializationException {
return AnalysisEngineFactory.createPrimitiveDescription(
NewAnnotator.class,
PARAM_POSTAG_MODEL_FILE,
ParamUtil.getParameterValue(PARAM_POSTAG_MODEL_FILE, "/somepath"));
}
public static final String PARAM_POSTAG_MODEL_FILE = ConfigurationParameterFactory.createConfigurationParameterName(
PosTaggerAnnotator.class,
"postagModelFile");
I almost copy this part from PosTaggerAnnotator, but it has no use in my NewAnnotator, I just add in so that I can use:
aggregate.add(NewAnnotator.getDescription());
because I don't know any other way to add to aggregate without .getDescription(); and I also don't know how to declare a correct getDescription() in my annotator, even it can works fine without it.
So please give me some advise here if you have experienced it! Thank you!
getDescription() is a convenience method to create a default description for your annotator. It uses AnalysisEngineFactory.createPrimitiveDescription(), to which you need to provide the right arguments, like this:
public static AnalysisEngineDescription getDescription() throws ResourceInitializationException {
return AnalysisEngineFactory.createPrimitiveDescription(
NewAnnotator.class,
first_parameter_name, first_parameter_value,
second_parameter_name, second_parameter_value,
... );
}
There are more examples in the uimaFIT codebase.

Resources