Search This Blog

Showing posts with label console. Show all posts
Showing posts with label console. Show all posts

Saturday, August 17, 2019

OpenAF-console color formatting

When using the OpenAF-console if the terminal is ANSI capable some of the output will be "colored" to improve readability:

But the same on dark mode might be harder to read:

The current colors used in the OpenAF are defined in the internal variable "__colorFormat":
> __colorFormat
{
  "key": "BOLD,BLACK",
  "number": "GREEN",
  "string": "CYAN",
  "boolean": "RED",
  "default": "YELLOW"
}
You can check all the available attributes & colors executing "help ansiColor". You can change any color directly on the __colorFormat map. If you want to make your changes permanent you can add them to the ~/.openaf-console_profile file.
So let's change and see the diference:
> __colorFormat.key = "BOLD,WHITE"
Much easier to read. Of course, it all depends on your personal preferences:
{
  "key": "UNDERLINE,WHITE",
  "number": "BG_WHITE,GREEN",
  "string": "BG_WHITE,BLUE",
  "boolean": "BG_WHITE,RED",
  "default": "BG_WHITE,YELLOW"
}

Thursday, August 15, 2019

Timing executions on OpenAF console

When profilling your script or functions you created it's usually helpfull to quickly understand how many ms or seconds it took to execute. Using the OpenAF console you can use the "time" command to provide you the execution time of every console line executed:

> time
-- Timing of commands is enabled.
> sleep(500)
-- Elapsed time: 0.51s

Analysing results

Consider the following example:

> var o = io.listFiles("/usr/bin")
-- Elapsed time: 0.045s
> var o = io.listFiles("/usr/bin")
-- Elapsed time: 0.028s
> var o = io.listFiles("/usr/bin")
-- Elapsed time: 0.024s

"Why the first test was slower than the other two tests?" Whenever analysing execution time you need to consider several factors among them:

  • OpenAF uses the Rhino javascript engine that, by default in OpenAF, will interpret the javascript code and then compile it "on-the-fly" to java bytecode. Subsequent calls will then use the compiled code.
  • The are always several levels of caching. In this case the operating system might cache the results of listing details about a folder so subsequent calls will be faster.
  • It's Java underneath so the Java Gargabe Collector might influence results.
  • The current machine load can also influence the execution time.

Adivces

The generic advices, given the factors that might influence execution, are:

  • Always time execution more than one time. Exit and reopen the openaf-console, if needed, to understand the time it takes to "heat up".
  • Keep an eye on the current machine load. Make sure you only make comparitions with similar general loads.
  • Make sure there is enough memory for the openaf-console Java process and the execution you are profiling.

If you want to keep an eye on the memory you can also execute the "one-line":

> ow.loadFormat();
> watch var rm = java.lang.Runtime.getRuntime(); templify("{{free}}/{{total}} ({{max}})", { free: ow.format.toBytesAbbreviation(rm.freeMemory()), total: ow.format.toBytesAbbreviation(rm.totalMemory()), max: ow.format.toBytesAbbreviation(rm.maxMemory()) })

Executing now the same calls you can now see the memory evolution ("free/total (max)"):

> var o = io.listFiles("/usr/bin")
-- Elapsed time: 0.033s
[ "48.2 MB/59.5 MB (879 MB)" ]
> var o = io.listFiles("/usr/bin")
-- Elapsed time: 0.024s
[ "45.2 MB/59.5 MB (879 MB)" ]
> var o = io.listFiles("/usr/bin")
-- Elapsed time: 0.022s
[ "42.1 MB/59.5 MB (879 MB)" ]

How to turn it off

To turn off the timming of command executions and the memory evolution watch command just execute:

> time
-- Timing of commands is disabled.
> watch off
>

You can also add time specific points in an existing script using functions like now(). For even more functionality the ow.test library provides more tools for reporting

Sunday, August 4, 2019

Increasing the history size for openaf-console

The default history size on the openaf-console is around 500 lines or commands. But there are some users that require a little more for their uses. So can an user increase the history size for openaf-console? Yes, you just set it on the openaf-console_profile file.

To check the current size just open an openaf-console and execute:
> con.getConsoleReader().getHistory().getMaxSize()
500

To change this you just need to execute:
con.getConsoleReader().getHistory().setMaxSize(1000);

But it order to make this change permanent you need to execute this setMaxSize every time openaf-console starts. An easy way is to use the openaf-console_profile file. This file, in Unix/Mac, is located at ~/.openaf-console_profile and in Windows its located at c:\users\[you user]\.openaf-console_profile. It executes like an openaf script every time the openaf-console is started.

That means that you have to be careful on what you add to this file as you also have to be careful in increase the history size. The default is set to keep the performance at "satisfactory" levels so do lower your maximum if you notice an non satisfactory performance downgrade.

The same applies to the near by .openaf_profile file. It will get executed every time a openaf script is executed (and keep in mind that the openaf-console is just another openaf script).

Saturday, August 3, 2019

Receiving keyboard input

In some scripts you might need to request input from an user or you just want to pause and wait for the user to press any key (the famous "Press any key to continue" sentence). In OpenAF you can do this easily with the Console plugin. Here is a quick example:
plugin("Console");

var con = new Console();
var login = con.readLinePrompt("Please enter your login   : ");
var pass  = con.readLinePrompt("Please enter your password: ", "*");

printnl("Press any key to continue... "); 
print("(you pressed char '" + con.readCharB() + "')\n");

print("Login: " + login);
print("Pass : " + pass);

So the readLinePrompt function will actually read an entire line, with any "prompt" you want and return you what was written. If you need to hide what the user is writing (e.g. password) you can also provide the character to use for that.

The readCharB will wait for the user to hit any key and then return you the ascii code of the charactered hit on the keyboard. You can cycle between readCharB waiting for a specific char, for example. You can also use readChar that let's you pass, as an argument, the allowed characters set. And if just want to check if any character was hit on the keyboard without waiting for it you can use readCharNB.

Using arrays with parallel

OpenAF is a mix of Javascript and Java, but "pure" javascript isn't "thread-safe" in the Java world. Nevertheless be...