What you need is already available to script this:
* Do short tests: parameter --autoclose N to close after N minutes (the 1st minute has no fee)
* Generate a different config for each test: just generate the JSON config, it's a piece of standard JSON
* Read the hashrate: the JSON output is perfect, or use --log then parse the file
and repeat repeat until you get the optimum.